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