Skip to content

Commit 19e7408

Browse files
authored
Merge pull request #3 from h2akim/master
LineItem extra field for FulfillmentOrder usage
2 parents 0d4a758 + 7b95770 commit 19e7408

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

src/Enum/Fields/LineItemFields.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class LineItemFields extends AbstractObjectEnum
2424
const TAXABLE = 'taxable';
2525
const TAX_LINES = 'tax_lines';
2626
const TOTAL_DISCOUNT = 'total_discount';
27+
28+
// Extra fields for Fulfillment Order usage
29+
const SHOP_ID = 'shop_id';
30+
const FULFILLMENT_ORDER_ID = 'fulfillment_order_id';
31+
const LINE_ITEM_ID = 'line_item_id';
32+
const INVENTORY_ITEM_ID = 'inventory_item_id';
2733

2834
public function getFieldTypes()
2935
{
@@ -49,6 +55,10 @@ public function getFieldTypes()
4955
'tax_lines' => 'TaxLine[]',
5056
'total_discount' => 'string',
5157
'discount_allocations' => 'DiscountAllocation[]',
58+
'shop_id' => 'integer',
59+
'fulfillment_order_id' => 'integer',
60+
'line_item_id' => 'integer',
61+
'inventory_item_id' => 'integer',
5262
];
5363
}
5464
}

src/Object/AbstractObject.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ abstract class AbstractObject implements \JsonSerializable
4444

4545
protected $types = array();
4646

47+
protected $returnedData = array();
48+
4749
public function __construct()
4850
{
4951
$enum = static::getFieldsEnum();
@@ -73,7 +75,7 @@ public function &__set($key, $value)
7375
}
7476
if (!is_null($value) && !$this->isValidValue($key, $value)) {
7577
throw new \InvalidArgumentException(
76-
"Invalid type for property '{$key}', should be a ".$this->types[$key]." received ".print_r($value,1)
78+
"Invalid type for property '{$key}', should be a ".$this->types[$key]." received ".print_r($value, 1)
7779
);
7880
}
7981
$this->data[$key] = $value;
@@ -84,7 +86,6 @@ public function &__set($key, $value)
8486
public function setDataWithoutValidation($data)
8587
{
8688
foreach ($data as $key => $value) {
87-
8889
}
8990
$this->clearHistory();
9091
}
@@ -107,6 +108,7 @@ public function setData($data)
107108
$type = $this->types[$key];
108109
$value = $this->castToType($value, $type);
109110
$this->{$key} = $value;
111+
$this->returnedData[] = $key;
110112
}
111113
}
112114

@@ -154,7 +156,8 @@ function ($data) use ($type) {
154156
$obj = new $className();
155157
$obj->setData($data);
156158
return $obj;
157-
}, $value
159+
},
160+
$value
158161
);
159162
} else {
160163
$className = '\\Shopify\\Object\\'.$type;
@@ -169,20 +172,20 @@ public function isValidValue($key, $value)
169172
{
170173
$type = $this->types[$key];
171174
switch ($type) {
172-
case 'string':
173-
return is_string($value) || is_integer($value) || is_bool($value);
174-
case 'integer':
175-
return is_numeric($value);
176-
case 'boolean':
177-
return is_bool($value);
178-
case 'array':
179-
return is_array($value);
180-
case 'object':
181-
return is_object($value);
182-
case 'float':
183-
return is_float($value);
184-
case 'DateTime':
185-
return is_a($value, \DateTime::class);
175+
case 'string':
176+
return is_string($value) || is_integer($value) || is_bool($value);
177+
case 'integer':
178+
return is_numeric($value);
179+
case 'boolean':
180+
return is_bool($value);
181+
case 'array':
182+
return is_array($value);
183+
case 'object':
184+
return is_object($value);
185+
case 'float':
186+
return is_float($value);
187+
case 'DateTime':
188+
return is_a($value, \DateTime::class);
186189
}
187190
if (substr($type, -2) == '[]') {
188191
foreach ($value as $obj) {
@@ -205,7 +208,8 @@ public function exportData()
205208
$results[$field] = array_map(
206209
function ($obj) {
207210
return $obj->exportData();
208-
}, $value
211+
},
212+
$value
209213
);
210214
} elseif (is_a($value, AbstractObject::class)) {
211215
$results[$field] = $value->exportData();
@@ -238,7 +242,8 @@ public function cast($type, $value)
238242
$value = array_map(
239243
function ($data) use ($type) {
240244
return $this->instantiate($type, $data);
241-
}, $value
245+
},
246+
$value
242247
);
243248
} else {
244249
$value = $this->instantiate($type, $value);
@@ -269,4 +274,16 @@ public function jsonSerialize()
269274
{
270275
return $this->data;
271276
}
277+
278+
public function onlyReturnedData()
279+
{
280+
$keys = array_keys($this->data);
281+
foreach ($keys as $key) {
282+
if (!in_array($key, $this->returnedData)) {
283+
unset($this->data[$key]);
284+
}
285+
}
286+
287+
return $this;
288+
}
272289
}

0 commit comments

Comments
 (0)