Commit 32696c91 authored by 王源's avatar 王源 🎧

[功能]添加shopify集合相关类文件

parent 0c883be6
...@@ -8,7 +8,13 @@ ...@@ -8,7 +8,13 @@
namespace Meibuyu\Micro\Shopify; namespace Meibuyu\Micro\Shopify;
use Exception;
use Meibuyu\Micro\Shopify\lib\AbstractShopify; use Meibuyu\Micro\Shopify\lib\AbstractShopify;
use Meibuyu\Micro\Shopify\lib\Collect;
use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection;
use Meibuyu\Micro\Shopify\lib\Metafield;
use Meibuyu\Micro\Shopify\lib\SmartCollection;
use Meibuyu\Micro\Shopify\lib\Webhook; use Meibuyu\Micro\Shopify\lib\Webhook;
/** /**
...@@ -16,20 +22,46 @@ use Meibuyu\Micro\Shopify\lib\Webhook; ...@@ -16,20 +22,46 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
* @package Meibuyu\Shopify * @package Meibuyu\Shopify
* *
* @property-read Webhook $Webhook * @property-read Webhook $Webhook
* @property-read Collect $Collect
* @property-read Collection $Collection
* @property-read CustomCollection $CustomCollection
* @property-read SmartCollection $SmartCollection
* @property-read Metafield $Metafield
*
* @method Webhook Webhook(integer $id = null)
* @method Collection Collection(integer $id = null)
* @method CustomCollection CustomCollection(integer $id = null)
* @method SmartCollection SmartCollection(integer $id = null)
* @method Metafield Metafield(integer $id = null)
*
*/ */
class ShopifyApp class ShopifyApp
{ {
protected $resources = [ protected $resources = [
'Webhook', 'Webhook',
'Collect',
'Collection',
'CustomCollection',
'SmartCollection',
'Metafield',
]; ];
protected $childResources = array(
'Fulfillment' => 'Order',
'FulfillmentEvent' => 'Fulfillment',
'OrderRisk' => 'Order',
'ProductImage' => 'Product',
'ProductVariant' => 'Product',
'DiscountCode' => 'PriceRule',
'Refund' => 'Order',
'Transaction' => 'Order',
);
public $config = []; public $config = [];
public $defaultApiVersion = '2020-07'; public $defaultApiVersion = '2020-07';
public $timeAllowedForEachApiCall = .5;
/** /**
* ShopifyApp constructor. * ShopifyApp constructor.
* @param array $config * @param array $config
...@@ -51,15 +83,34 @@ class ShopifyApp ...@@ -51,15 +83,34 @@ class ShopifyApp
* 返回AbstractShopify实例 * 返回AbstractShopify实例
* @param string $className 实现的类名 * @param string $className 实现的类名
* @return AbstractShopify * @return AbstractShopify
* @throws \Exception * @throws Exception
*/ */
public function __get($className) public function __get($className)
{
return $this->$className();
}
/**
* 返回AbstractShopify实例
* @param string $className 实现的类名
* @param $arguments
* @return AbstractShopify
* @throws Exception
*/
public function __call($className, $arguments)
{ {
if (!in_array($className, $this->resources)) { if (!in_array($className, $this->resources)) {
throw new \Exception("未知方法 $className"); if (isset($this->childResources[$className])) {
$message = "$className 是属于 {$this->childResources[$className]} 的子集, 无法直接访问";
} else {
$message = "未知类 $className";
}
throw new Exception($message);
} }
$resourceID = !empty($arguments) ? $arguments[0] : null;
$resourceClassName = __NAMESPACE__ . "\\lib\\$className"; $resourceClassName = __NAMESPACE__ . "\\lib\\$className";
return new $resourceClassName($this->config); return new $resourceClassName($this->config, $resourceID);
} }
public function setApiUrl() public function setApiUrl()
......
...@@ -8,16 +8,22 @@ ...@@ -8,16 +8,22 @@
namespace Meibuyu\Micro\Shopify\lib; namespace Meibuyu\Micro\Shopify\lib;
use Exception;
use Meibuyu\Micro\Tools\HttpRequestJson; use Meibuyu\Micro\Tools\HttpRequestJson;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
abstract class AbstractShopify abstract class AbstractShopify
{ {
/**
* 资源id
* @var int
*/
public $id = null;
protected $httpHeaders = []; protected $httpHeaders = [];
protected $resourceUrl; protected $resourceUrl;
protected $resourceKey; protected $resourceKey;
protected $pluralizeKey; protected $pluralizeKey;
...@@ -27,86 +33,103 @@ abstract class AbstractShopify ...@@ -27,86 +33,103 @@ abstract class AbstractShopify
*/ */
public $countEnabled = true; public $countEnabled = true;
/** // 子集资源
* List of custom GET / POST / PUT / DELETE actions protected $childResource = [];
*
* Custom actions can be used without calling the get(), post(), put(), delete() methods directly // 自定义请求方法
* @example: ['enable', 'disable', 'remove','default' => 'makeDefault']
* Methods can be called like enable(), disable(), remove(), makeDefault() etc.
* If any array item has an associative key => value pair, value will be considered as the method name
* and key will be the associated path to be used with the action.
*
* @var array $customGetActions
* @var array $customPostActions
* @var array $customPutActions
* @var array $customDeleteActions
*/
protected $customGetActions = []; protected $customGetActions = [];
protected $customPostActions = []; protected $customPostActions = [];
protected $customPutActions = []; protected $customPutActions = [];
protected $customDeleteActions = []; protected $customDeleteActions = [];
private $config;
/** /**
* AbstractShopify constructor. * AbstractShopify constructor.
* @param $config * @param $config
* @throws \Exception * @param null $id
* @param string $parentResourceUrl
* @throws Exception
*/ */
public function __construct($config) public function __construct($config, $id = null, $parentResourceUrl = null)
{ {
$this->config = $config;
$this->id = $id;
$this->pluralizeKey = $this->resourceKey . 's'; $this->pluralizeKey = $this->resourceKey . 's';
$this->resourceUrl = $config['api_url'] . $this->pluralizeKey; $this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : '');
if (isset($config['api_password'])) { if (isset($config['api_password'])) {
$this->httpHeaders['X-Shopify-Access-Token'] = $config['api_password']; $this->httpHeaders['X-Shopify-Access-Token'] = $config['api_password'];
} elseif (!isset($config['api_password'])) { } elseif (!isset($config['api_password'])) {
throw new \Exception("请设置api_password值"); throw new Exception("请设置api_password值");
} }
} }
/**
* 调用子集资源
* @param $childName
* @return mixed
*/
public function __get($childName)
{
return $this->$childName();
}
/** /**
* 调用自定义方法 * 调用自定义方法
* @param $name * @param $name
* @param $arguments * @param $arguments
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
$actionMaps = array( if (ctype_upper($name[0])) {
'post' => 'customPostActions', $childKey = array_search($name, $this->childResource);
'put' => 'customPutActions', if ($childKey === false) {
'get' => 'customGetActions', throw new Exception(" $name 不属于 {$this->getResourceName()}");
'delete' => 'customDeleteActions', }
); $childClassName = !is_numeric($childKey) ? $childKey : $name;
//Get the array key for the action in the actions array $childClass = __NAMESPACE__ . "\\" . $childClassName;
foreach ($actionMaps as $httpMethod => $actionArrayKey) { $resourceID = !empty($arguments) ? $arguments[0] : null;
$actionKey = array_search($name, $this->$actionArrayKey); return new $childClass($this->config, $resourceID, $this->resourceUrl);
if ($actionKey !== false) break;
}
if ($actionKey === false) {
throw new \Exception("No action named $name is defined for " . $this->getResourceName());
}
//If any associative key is given to the action, then it will be considered as the method name,
//otherwise the action name will be the method name
$customAction = !is_numeric($actionKey) ? $actionKey : $name;
//Get the first argument if provided with the method call
$methodArgument = !empty($arguments) ? $arguments[0] : [];
//Url parameters
$urlParams = [];
//Data body
$dataArray = [];
//Consider the argument as url parameters for get and delete request
//and data array for post and put request
if ($httpMethod == 'post' || $httpMethod == 'put') {
$dataArray = $methodArgument;
} else { } else {
$urlParams = $methodArgument; $actionMaps = [
} 'post' => 'customPostActions',
$url = $this->generateUrl($urlParams, null, $customAction); 'put' => 'customPutActions',
if ($httpMethod == 'post' || $httpMethod == 'put') { 'get' => 'customGetActions',
return $this->$httpMethod($dataArray, $url, false); 'delete' => 'customDeleteActions',
} else { ];
return $this->$httpMethod($dataArray, $url); //Get the array key for the action in the actions array
foreach ($actionMaps as $httpMethod => $actionArrayKey) {
$actionKey = array_search($name, $this->$actionArrayKey);
if ($actionKey !== false) break;
}
if ($actionKey === false) {
throw new Exception("No action named $name is defined for " . $this->getResourceName());
}
//If any associative key is given to the action, then it will be considered as the method name,
//otherwise the action name will be the method name
$customAction = !is_numeric($actionKey) ? $actionKey : $name;
//Get the first argument if provided with the method call
$methodArgument = !empty($arguments) ? $arguments[0] : [];
//Url parameters
$urlParams = [];
//Data body
$dataArray = [];
//Consider the argument as url parameters for get and delete request
//and data array for post and put request
if ($httpMethod == 'post' || $httpMethod == 'put') {
$dataArray = $methodArgument;
} else {
$urlParams = $methodArgument;
}
$url = $this->generateUrl($urlParams, null, $customAction);
if ($httpMethod == 'post' || $httpMethod == 'put') {
return $this->$httpMethod($dataArray, $url, false);
} else {
return $this->$httpMethod($dataArray, $url);
}
} }
} }
...@@ -117,7 +140,17 @@ abstract class AbstractShopify ...@@ -117,7 +140,17 @@ abstract class AbstractShopify
public function generateUrl($urlParams = [], $id = null, $customAction = null) public function generateUrl($urlParams = [], $id = null, $customAction = null)
{ {
return $this->resourceUrl . ($id ? "/$id" : '') . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : ''); $resourceUrl = $this->resourceUrl;
if ($id) {
if ($this->id) {
if ($id !== $this->id) {
$resourceUrl = str_replace($this->id, $id, $this->resourceUrl);
}
} else {
$resourceUrl = $this->resourceUrl . "/$id";
}
}
return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
} }
/** /**
...@@ -125,15 +158,13 @@ abstract class AbstractShopify ...@@ -125,15 +158,13 @@ abstract class AbstractShopify
* @param null $url * @param null $url
* @param null $dataKey * @param null $dataKey
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function get($urlParams = [], $url = null, $dataKey = null) public function get($urlParams = [], $url = null, $dataKey = null)
{ {
if (!$url) $url = $this->generateUrl($urlParams); if (!$url) $url = $this->generateUrl($urlParams);
$response = HttpRequestJson::get($url, $this->httpHeaders); $response = HttpRequestJson::get($url, $this->httpHeaders);
if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
if (!$dataKey) $dataKey = $this->pluralizeKey;
return $this->processResponse($response, $dataKey); return $this->processResponse($response, $dataKey);
} }
...@@ -142,7 +173,7 @@ abstract class AbstractShopify ...@@ -142,7 +173,7 @@ abstract class AbstractShopify
* @param $id * @param $id
* @param array $urlParams * @param array $urlParams
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function show($id, $urlParams = []) public function show($id, $urlParams = [])
{ {
...@@ -155,12 +186,12 @@ abstract class AbstractShopify ...@@ -155,12 +186,12 @@ abstract class AbstractShopify
* 获取数量 * 获取数量
* @param array $urlParams * @param array $urlParams
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function count($urlParams = []) public function count($urlParams = [])
{ {
if (!$this->countEnabled) { if (!$this->countEnabled) {
throw new \Exception("当前类{$this->getResourceName()}不支持count()方法"); throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
} }
$url = $this->generateUrl($urlParams, null, 'count'); $url = $this->generateUrl($urlParams, null, 'count');
return $this->get([], $url, 'count'); return $this->get([], $url, 'count');
...@@ -171,7 +202,7 @@ abstract class AbstractShopify ...@@ -171,7 +202,7 @@ abstract class AbstractShopify
* @param null $url * @param null $url
* @param bool $wrapData * @param bool $wrapData
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function post($dataArray, $url = null, $wrapData = true) public function post($dataArray, $url = null, $wrapData = true)
{ {
...@@ -187,7 +218,7 @@ abstract class AbstractShopify ...@@ -187,7 +218,7 @@ abstract class AbstractShopify
* @param null $url * @param null $url
* @param bool $wrapData * @param bool $wrapData
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function put($id, $dataArray, $url = null, $wrapData = true) public function put($id, $dataArray, $url = null, $wrapData = true)
{ {
...@@ -202,9 +233,9 @@ abstract class AbstractShopify ...@@ -202,9 +233,9 @@ abstract class AbstractShopify
* @param array $urlParams * @param array $urlParams
* @param null $url * @param null $url
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function delete($id, $urlParams = [], $url = null) public function delete($id = null, $urlParams = [], $url = null)
{ {
if (!$url) $url = $this->generateUrl($urlParams, $id); if (!$url) $url = $this->generateUrl($urlParams, $id);
$response = HttpRequestJson::delete($url, $this->httpHeaders); $response = HttpRequestJson::delete($url, $this->httpHeaders);
...@@ -214,7 +245,7 @@ abstract class AbstractShopify ...@@ -214,7 +245,7 @@ abstract class AbstractShopify
protected function wrapData($dataArray, $dataKey = null) protected function wrapData($dataArray, $dataKey = null)
{ {
if (!$dataKey) $dataKey = $this->resourceKey; if (!$dataKey) $dataKey = $this->resourceKey;
return array($dataKey => $dataArray); return [$dataKey => $dataArray];
} }
protected function castString($array) protected function castString($array)
...@@ -235,7 +266,7 @@ abstract class AbstractShopify ...@@ -235,7 +266,7 @@ abstract class AbstractShopify
* @param ResponseInterface $response * @param ResponseInterface $response
* @param null $dataKey * @param null $dataKey
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function processResponse($response, $dataKey = null) public function processResponse($response, $dataKey = null)
{ {
...@@ -244,7 +275,7 @@ abstract class AbstractShopify ...@@ -244,7 +275,7 @@ abstract class AbstractShopify
$content = json_decode($content, true); $content = json_decode($content, true);
if (isset($content['errors'])) { if (isset($content['errors'])) {
throw new \Exception($this->castString($content['errors']), $httpCode); throw new Exception($this->castString($content['errors']), $httpCode);
} }
if ($dataKey && isset($content[$dataKey])) { if ($dataKey && isset($content[$dataKey])) {
return $content[$dataKey]; return $content[$dataKey];
......
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/24
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
class Collect extends AbstractShopify
{
protected $resourceKey = 'collect';
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/24
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
/**
* Class Collection
* @package Meibuyu\Micro\Shopify\lib
*
* @property-read Metafield $Metafield
*
* @method Metafield Metafield(integer $id = null)
*/
class Collection extends AbstractShopify
{
protected $resourceKey = 'collection';
protected $childResource = [
'Metafield',
];
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/22
* Time: 16:14
*/
namespace Meibuyu\Micro\Shopify\lib;
/**
* Class CustomCollection
* @package Meibuyu\Micro\Shopify\lib
*
* @property-read Metafield $Metafield
*
* @method Metafield Metafield(integer $id = null)
*/
class CustomCollection extends AbstractShopify
{
protected $resourceKey = 'custom_collection';
protected $childResource = [
'Metafield',
];
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/26
* Time: 15:50
*/
namespace Meibuyu\Micro\Shopify\lib;
class Metafield extends AbstractShopify
{
protected $resourceKey = 'metafield';
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/8/24
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
/**\
* Class SmartCollection
* @package Meibuyu\Micro\Shopify\lib
*
* @property-read Metafield $Metafield
*
* @method Metafield Metafield(integer $id = null)
*/
class SmartCollection extends AbstractShopify
{
protected $resourceKey = 'smart_collection';
protected $childResource = [
'Metafield',
];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment