Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource/Object specific data key support #296

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ The custom methods are specific to some resources which may not be available for
$fo = $client->Order("1234567890")->FulfillmentOrder()->get();

// Requesting assigned fulfillment orders (with status fulfillment_requested)
$shopify->AssignedFulfillmentOrder()->get(["assignment_status" => "fulfillment_requested"]);
$fo = $client->AssignedFulfillmentOrder()->get(["assignment_status" => "fulfillment_requested"]);

// Creating a FulfilmentRequest
// Follow instructions to get partial fulfilments
Expand Down
5 changes: 5 additions & 0 deletions lib/AssignedFulfillmentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ class AssignedFulfillmentOrder extends ShopifyResource
* @inheritDoc
*/
protected $resourceKey = 'assigned_fulfillment_order';

/**
* @inheritDoc
*/
protected $dataKey = 'fulfillment_orders';
}
29 changes: 25 additions & 4 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ abstract class ShopifyResource
*/
protected $childResource = array();

/**
* If the dataKey is different from the resourceKey (base key name used in API results)
*
* @var boolean
*/
protected $dataKey = null;

/**
* If search is enabled for the resource
*
Expand Down Expand Up @@ -309,6 +316,21 @@ protected function getResourcePath()
return $this->pluralizeKey();
}

/**
* Get the working dataKey for the resource (if any)
*
* For situations where the resource returns results using a different keyname.
* (e.g. AssignedFulfillmentOrder responds using 'fulfillment_orders')
*/
protected function getDataKey(string $dataKey = null)
{
// Passthrough
if ($dataKey) return $dataKey;

// Return the dataKey if set
return $this->dataKey ?: ($this->id ? $this->resourceKey : $this->pluralizeKey());
}

/**
* Generate the custom url for api request based on the params and custom action (if any)
*
Expand Down Expand Up @@ -342,10 +364,7 @@ public function get($urlParams = array(), $url = null, $dataKey = null)

$response = HttpRequestJson::get($url, $this->httpHeaders);

if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey();

return $this->processResponse($response, $dataKey);

return $this->processResponse($response, $this->getDataKey($dataKey));
}

/**
Expand Down Expand Up @@ -540,6 +559,8 @@ public function processResponse($responseArray, $dataKey = null)
throw new ApiException($message, CurlRequest::$lastHttpCode);
}

$dataKey = $this->getDataKey($dataKey);

if ($dataKey && isset($responseArray[$dataKey])) {
return $responseArray[$dataKey];
} else {
Expand Down