From 23da2c7bcd5e967ed62378fa60efef977df1c0ae Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 27 Nov 2018 14:29:25 +0200 Subject: [PATCH 1/2] New method for Attributes trait, compability with new response for /orders --- src/Lib/Traits/InteractsWithAttributes.php | 21 +++++++++++++++++++++ src/Resources/Shop/OrdersResource.php | 9 ++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Lib/Traits/InteractsWithAttributes.php b/src/Lib/Traits/InteractsWithAttributes.php index df17254d..94e2dac7 100644 --- a/src/Lib/Traits/InteractsWithAttributes.php +++ b/src/Lib/Traits/InteractsWithAttributes.php @@ -79,6 +79,27 @@ public function getFirstValueByAttributeCode(string $code) return $value; } + /** + * The method should get rid of addition conditions in templates + * + * @param string $code + * @param bool $asTitle By default we should display always Title field, + * but it should be also possible to use value + * @return string|null + */ + public function getFirstValueByAttributeCodeAsString(string $code, $asTitle = true) + { + $attributeValue = $this->getFirstValueByAttributeCode($code); + + if (!$attributeValue) { + return null; + } + + $field = $asTitle ? 'title' : 'value'; + + return $attributeValue->$field; + } + /** * @param Attribute[] $attributes */ diff --git a/src/Resources/Shop/OrdersResource.php b/src/Resources/Shop/OrdersResource.php index 84cdc9de..6058a636 100644 --- a/src/Resources/Shop/OrdersResource.php +++ b/src/Resources/Shop/OrdersResource.php @@ -10,6 +10,7 @@ namespace SphereMall\MS\Resources\Shop; use SphereMall\MS\Entities\Order; +use SphereMall\MS\Lib\Makers\ObjectMaker; use SphereMall\MS\Lib\Makers\OrdersMaker; use SphereMall\MS\Lib\Shop\OrderFinalized; use SphereMall\MS\Resources\Resource; @@ -91,10 +92,12 @@ private function getOrderByParam($uriAppend) $params = $this->getQueryParams(); $response = $this->handler->handle('GET', false, $uriAppend, $params); - $orderCollection = $this->make($response); - if ($orderCollection) { + if ($response->getData()) { + $maker = empty($response->getData()[0]['relationships']) ? new ObjectMaker() : new OrdersMaker(); + /** @var $order Order */ + $order = $this->make($response, false, $maker); $orderFinalized = new OrderFinalized($this->client); - $orderFinalized->setOrderData($orderCollection[0]); + $orderFinalized->setOrderData($order); return $orderFinalized; } From bc818d7dcf27a100c6306ea0ab7dea4f143a6f55 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 27 Nov 2018 14:31:48 +0200 Subject: [PATCH 2/2] Return string instead of null --- src/Lib/Traits/InteractsWithAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lib/Traits/InteractsWithAttributes.php b/src/Lib/Traits/InteractsWithAttributes.php index 94e2dac7..2655f0ac 100644 --- a/src/Lib/Traits/InteractsWithAttributes.php +++ b/src/Lib/Traits/InteractsWithAttributes.php @@ -92,7 +92,7 @@ public function getFirstValueByAttributeCodeAsString(string $code, $asTitle = tr $attributeValue = $this->getFirstValueByAttributeCode($code); if (!$attributeValue) { - return null; + return ""; } $field = $asTitle ? 'title' : 'value';