Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/Enum/Fields/LineItemFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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',
];
}
}
55 changes: 36 additions & 19 deletions src/Object/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ abstract class AbstractObject implements \JsonSerializable

protected $types = array();

protected $returnedData = array();

public function __construct()
{
$enum = static::getFieldsEnum();
Expand Down Expand Up @@ -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;
Expand All @@ -84,7 +86,6 @@ public function &__set($key, $value)
public function setDataWithoutValidation($data)
{
foreach ($data as $key => $value) {

}
$this->clearHistory();
}
Expand All @@ -107,6 +108,7 @@ public function setData($data)
$type = $this->types[$key];
$value = $this->castToType($value, $type);
$this->{$key} = $value;
$this->returnedData[] = $key;
}
}

Expand Down Expand Up @@ -154,7 +156,8 @@ function ($data) use ($type) {
$obj = new $className();
$obj->setData($data);
return $obj;
}, $value
},
$value
);
} else {
$className = '\\Shopify\\Object\\'.$type;
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}