From 87c97604e5c8a42e4b332dd91d89636d64404131 Mon Sep 17 00:00:00 2001 From: Hakim Razalan Date: Thu, 15 Sep 2022 13:57:53 +0800 Subject: [PATCH 1/4] LineItemFields: add extra fields for fulfillment order usage --- src/Enum/Fields/LineItemFields.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Enum/Fields/LineItemFields.php b/src/Enum/Fields/LineItemFields.php index 3390337..5e77333 100644 --- a/src/Enum/Fields/LineItemFields.php +++ b/src/Enum/Fields/LineItemFields.php @@ -24,6 +24,12 @@ class LineItemFields extends AbstractObjectEnum const TAXABLE = 'taxable'; const TAX_LINES = 'tax_lines'; const TOTAL_DISCOUNT = 'total_discount'; + + // Extra fields for Fulfillment Order usage + const SHOP_ID = 'shop_id'; + const FULFILLMENT_ORDER_ID = 'fulfillment_order_id'; + const line_item_id = 'line_item_id'; + const INVENTORY_ITEM_ID = 'inventory_item_id'; public function getFieldTypes() { @@ -49,6 +55,10 @@ public function getFieldTypes() 'tax_lines' => 'TaxLine[]', 'total_discount' => 'string', 'discount_allocations' => 'DiscountAllocation[]', + 'shop_id' => 'integer', + 'fulfillment_order_id' => 'integer', + 'line_item_id' => 'integer', + 'inventory_item_id' => 'integer', ]; } } From fae8c7656ba75993483a85549fb41622db88fad7 Mon Sep 17 00:00:00 2001 From: Hakim Razalan Date: Fri, 16 Sep 2022 07:07:18 +0800 Subject: [PATCH 2/4] add ability to return only returned data --- src/Object/AbstractObject.php | 55 +++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/Object/AbstractObject.php b/src/Object/AbstractObject.php index 8e63eae..b71457e 100644 --- a/src/Object/AbstractObject.php +++ b/src/Object/AbstractObject.php @@ -44,6 +44,8 @@ abstract class AbstractObject implements \JsonSerializable protected $types = array(); + protected $returnedData = array(); + public function __construct() { $enum = static::getFieldsEnum(); @@ -73,7 +75,7 @@ public function &__set($key, $value) } if (!is_null($value) && !$this->isValidValue($key, $value)) { throw new \InvalidArgumentException( - "Invalid type for property '{$key}', should be a ".$this->types[$key]." received ".print_r($value,1) + "Invalid type for property '{$key}', should be a ".$this->types[$key]." received ".print_r($value, 1) ); } $this->data[$key] = $value; @@ -84,7 +86,6 @@ public function &__set($key, $value) public function setDataWithoutValidation($data) { foreach ($data as $key => $value) { - } $this->clearHistory(); } @@ -107,6 +108,7 @@ public function setData($data) $type = $this->types[$key]; $value = $this->castToType($value, $type); $this->{$key} = $value; + $this->returnedData[] = $key; } } @@ -154,7 +156,8 @@ function ($data) use ($type) { $obj = new $className(); $obj->setData($data); return $obj; - }, $value + }, + $value ); } else { $className = '\\Shopify\\Object\\'.$type; @@ -169,20 +172,20 @@ public function isValidValue($key, $value) { $type = $this->types[$key]; switch ($type) { - case 'string': - return is_string($value) || is_integer($value) || is_bool($value); - case 'integer': - return is_numeric($value); - case 'boolean': - return is_bool($value); - case 'array': - return is_array($value); - case 'object': - return is_object($value); - case 'float': - return is_float($value); - case 'DateTime': - return is_a($value, \DateTime::class); + case 'string': + return is_string($value) || is_integer($value) || is_bool($value); + case 'integer': + return is_numeric($value); + case 'boolean': + return is_bool($value); + case 'array': + return is_array($value); + case 'object': + return is_object($value); + case 'float': + return is_float($value); + case 'DateTime': + return is_a($value, \DateTime::class); } if (substr($type, -2) == '[]') { foreach ($value as $obj) { @@ -205,7 +208,8 @@ public function exportData() $results[$field] = array_map( function ($obj) { return $obj->exportData(); - }, $value + }, + $value ); } elseif (is_a($value, AbstractObject::class)) { $results[$field] = $value->exportData(); @@ -238,7 +242,8 @@ public function cast($type, $value) $value = array_map( function ($data) use ($type) { return $this->instantiate($type, $data); - }, $value + }, + $value ); } else { $value = $this->instantiate($type, $value); @@ -269,4 +274,16 @@ public function jsonSerialize() { return $this->data; } + + public function onlyReturnedData() + { + $keys = array_keys($this->data); + foreach ($keys as $key) { + if (in_array($key, $this->returnedData)) { + unset($this->data[$key]); + } + } + + return $this; + } } From 24ae45e39865427df65585be9a4d1d439949a2a0 Mon Sep 17 00:00:00 2001 From: Hakim Razalan Date: Fri, 16 Sep 2022 10:55:57 +0800 Subject: [PATCH 3/4] fix onlyReturnedData --- src/Object/AbstractObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Object/AbstractObject.php b/src/Object/AbstractObject.php index b71457e..33c1e20 100644 --- a/src/Object/AbstractObject.php +++ b/src/Object/AbstractObject.php @@ -279,7 +279,7 @@ public function onlyReturnedData() { $keys = array_keys($this->data); foreach ($keys as $key) { - if (in_array($key, $this->returnedData)) { + if (!in_array($key, $this->returnedData)) { unset($this->data[$key]); } } From 16b8a93fd8a1e0035e180149f74e87d39558d51f Mon Sep 17 00:00:00 2001 From: Hakim Razalan Date: Mon, 19 Sep 2022 08:38:55 +0800 Subject: [PATCH 4/4] fix const case --- src/Enum/Fields/LineItemFields.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enum/Fields/LineItemFields.php b/src/Enum/Fields/LineItemFields.php index 5e77333..32e771a 100644 --- a/src/Enum/Fields/LineItemFields.php +++ b/src/Enum/Fields/LineItemFields.php @@ -28,7 +28,7 @@ class LineItemFields extends AbstractObjectEnum // Extra fields for Fulfillment Order usage const SHOP_ID = 'shop_id'; const FULFILLMENT_ORDER_ID = 'fulfillment_order_id'; - const line_item_id = 'line_item_id'; + const LINE_ITEM_ID = 'line_item_id'; const INVENTORY_ITEM_ID = 'inventory_item_id'; public function getFieldTypes()