|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 27.01.15, 14:53 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Model\Common; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Model\Cart\CartReference; |
|
10
|
|
|
use Commercetools\Core\Model\CartDiscount\CartDiscountReference; |
|
11
|
|
|
use Commercetools\Core\Model\Category\CategoryReference; |
|
12
|
|
|
use Commercetools\Core\Model\Channel\ChannelReference; |
|
13
|
|
|
use Commercetools\Core\Model\Customer\CustomerReference; |
|
14
|
|
|
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference; |
|
15
|
|
|
use Commercetools\Core\Model\CustomObject\CustomObjectReference; |
|
16
|
|
|
use Commercetools\Core\Model\DiscountCode\DiscountCodeReference; |
|
17
|
|
|
use Commercetools\Core\Model\Order\OrderReference; |
|
18
|
|
|
use Commercetools\Core\Model\Payment\PaymentReference; |
|
19
|
|
|
use Commercetools\Core\Model\Product\ProductReference; |
|
20
|
|
|
use Commercetools\Core\Model\ProductDiscount\ProductDiscountReference; |
|
21
|
|
|
use Commercetools\Core\Model\ProductType\ProductTypeReference; |
|
22
|
|
|
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference; |
|
23
|
|
|
use Commercetools\Core\Model\State\StateReference; |
|
24
|
|
|
use Commercetools\Core\Model\TaxCategory\TaxCategoryReference; |
|
25
|
|
|
use Commercetools\Core\Model\Type\TypeReference; |
|
26
|
|
|
use Commercetools\Core\Model\Zone\ZoneReference; |
|
27
|
235 |
|
|
|
28
|
|
|
/** |
|
29
|
235 |
|
* @package Commercetools\Core\Model\Common |
|
30
|
235 |
|
* @link https://dev.commercetools.com/http-api-types.html#reference |
|
31
|
|
|
* @method string getTypeId() |
|
32
|
235 |
|
* @method string getId() |
|
33
|
|
|
* @method Reference setTypeId(string $typeId = null) |
|
34
|
|
|
* @method Reference setId(string $id = null) |
|
35
|
|
|
* @method JsonObject getObj() |
|
36
|
|
|
* @method Reference setObj(JsonObject $obj = null) |
|
37
|
|
|
* @method string getKey() |
|
38
|
|
|
* @method Reference setKey(string $key = null) |
|
39
|
|
|
*/ |
|
40
|
|
|
class Reference extends ResourceIdentifier |
|
41
|
|
|
{ |
|
42
|
|
|
const OBJ = 'obj'; |
|
43
|
|
|
|
|
44
|
|
|
const TYPE_CLASS = '\Commercetools\Core\Model\Common\JsonObject'; |
|
45
|
|
|
|
|
46
|
|
|
public function fieldDefinitions() |
|
47
|
|
|
{ |
|
48
|
|
|
$fieldDefinitions = parent::fieldDefinitions(); |
|
49
|
|
|
$fieldDefinitions[static::OBJ] = [static::TYPE => static::TYPE_CLASS]; |
|
50
|
|
|
|
|
51
|
|
|
return $fieldDefinitions; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $data |
|
56
|
|
|
* @param Context|callable $context |
|
57
|
|
|
* @return static |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function fromArray(array $data, $context = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if (get_called_class() == 'Commercetools\Core\Model\Common\Reference' && isset($data[static::TYPE_ID])) { |
|
62
|
|
|
$className = static::referenceType($data[static::TYPE_ID]); |
|
63
|
|
|
if (class_exists($className)) { |
|
64
|
|
|
return new $className($data, $context); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
return new static($data, $context); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected static function referenceType($typeId) |
|
71
|
|
|
{ |
|
72
|
|
|
$types = [ |
|
73
|
|
|
CartReference::TYPE_CART => '\Commercetools\Core\Model\Cart\CartReference', |
|
74
|
|
|
CartDiscountReference::TYPE_CART_DISCOUNT => '\Commercetools\Core\Model\CartDiscount\CartDiscountReference', |
|
75
|
|
|
CategoryReference::TYPE_CATEGORY => '\Commercetools\Core\Model\Category\CategoryReference', |
|
76
|
|
|
ChannelReference::TYPE_CHANNEL => '\Commercetools\Core\Model\Channel\ChannelReference', |
|
77
|
|
|
CustomerReference::TYPE_CUSTOMER => '\Commercetools\Core\Model\Customer\CustomerReference', |
|
78
|
|
|
CustomObjectReference::TYPE_CUSTOM_OBJECT => '\Commercetools\Core\Model\CustomObject\CustomObjectReference', |
|
79
|
|
|
CustomerGroupReference::TYPE_CUSTOMER_GROUP => |
|
80
|
|
|
'\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference', |
|
81
|
|
|
DiscountCodeReference::TYPE_DISCOUNT_CODE => '\Commercetools\Core\Model\DiscountCode\DiscountCodeReference', |
|
82
|
|
|
CustomObjectReference::TYPE_CUSTOM_OBJECT => '\Commercetools\Core\Model\CustomObject\CustomObjectReference', |
|
83
|
|
|
PaymentReference::TYPE_PAYMENT => '\Commercetools\Core\Model\Payment\PaymentReference', |
|
84
|
|
|
ProductReference::TYPE_PRODUCT => '\Commercetools\Core\Model\Product\ProductReference', |
|
85
|
|
|
ProductDiscountReference::TYPE_PRODUCT_DISCOUNT => |
|
86
|
|
|
'\Commercetools\Core\Model\ProductDiscount\ProductDiscountReference', |
|
87
|
|
|
ProductTypeReference::TYPE_PRODUCT_TYPE => '\Commercetools\Core\Model\ProductType\ProductTypeReference', |
|
88
|
|
|
OrderReference::TYPE_ORDER => '\Commercetools\Core\Model\Order\OrderReference', |
|
89
|
|
|
ShippingMethodReference::TYPE_SHIPPING_METHOD => |
|
90
|
|
|
'\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference', |
|
91
|
|
|
StateReference::TYPE_STATE => '\Commercetools\Core\Model\State\StateReference', |
|
92
|
|
|
TaxCategoryReference::TYPE_TAX_CATEGORY => '\Commercetools\Core\Model\TaxCategory\TaxCategoryReference', |
|
93
|
|
|
TypeReference::TYPE_TYPE => '\Commercetools\Core\Model\Type\TypeReference', |
|
94
|
|
|
ZoneReference::TYPE_ZONE => '\Commercetools\Core\Model\Zone\ZoneReference', |
|
95
|
|
|
]; |
|
96
|
|
|
return isset($types[$typeId]) ? $types[$typeId] : '\Commercetools\Core\Model\Common\Reference'; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|