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
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Commercetools\Core\Model\Common |
30
|
|
|
* @link https://dev.commercetools.com/http-api-types.html#reference |
31
|
|
|
* @method string getTypeId() |
32
|
|
|
* @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
|
254 |
|
public function fieldDefinitions() |
47
|
|
|
{ |
48
|
254 |
|
$fieldDefinitions = parent::fieldDefinitions(); |
49
|
254 |
|
$fieldDefinitions[static::OBJ] = [static::TYPE => static::TYPE_CLASS]; |
50
|
|
|
|
51
|
254 |
|
return $fieldDefinitions; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $data |
56
|
|
|
* @param Context|callable $context |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
56 |
View Code Duplication |
public static function fromArray(array $data, $context = null) |
|
|
|
|
60
|
|
|
{ |
61
|
56 |
|
if (get_called_class() == 'Commercetools\Core\Model\Common\Reference' && isset($data[static::TYPE_ID])) { |
62
|
4 |
|
$className = static::referenceType($data[static::TYPE_ID]); |
63
|
4 |
|
if (class_exists($className)) { |
64
|
4 |
|
return new $className($data, $context); |
65
|
|
|
} |
66
|
|
|
} |
67
|
52 |
|
return new static($data, $context); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
protected static function referenceType($typeId) |
71
|
|
|
{ |
72
|
|
|
$types = [ |
73
|
4 |
|
CartReference::TYPE_CART => '\Commercetools\Core\Model\Cart\CartReference', |
74
|
4 |
|
CartDiscountReference::TYPE_CART_DISCOUNT => '\Commercetools\Core\Model\CartDiscount\CartDiscountReference', |
75
|
4 |
|
CategoryReference::TYPE_CATEGORY => '\Commercetools\Core\Model\Category\CategoryReference', |
76
|
4 |
|
ChannelReference::TYPE_CHANNEL => '\Commercetools\Core\Model\Channel\ChannelReference', |
77
|
4 |
|
CustomerReference::TYPE_CUSTOMER => '\Commercetools\Core\Model\Customer\CustomerReference', |
78
|
4 |
|
CustomObjectReference::TYPE_CUSTOM_OBJECT => '\Commercetools\Core\Model\CustomObject\CustomObjectReference', |
79
|
4 |
|
CustomerGroupReference::TYPE_CUSTOMER_GROUP => |
80
|
4 |
|
'\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference', |
81
|
4 |
|
DiscountCodeReference::TYPE_DISCOUNT_CODE => '\Commercetools\Core\Model\DiscountCode\DiscountCodeReference', |
82
|
4 |
|
CustomObjectReference::TYPE_CUSTOM_OBJECT => '\Commercetools\Core\Model\CustomObject\CustomObjectReference', |
83
|
4 |
|
PaymentReference::TYPE_PAYMENT => '\Commercetools\Core\Model\Payment\PaymentReference', |
84
|
4 |
|
ProductReference::TYPE_PRODUCT => '\Commercetools\Core\Model\Product\ProductReference', |
85
|
4 |
|
ProductDiscountReference::TYPE_PRODUCT_DISCOUNT => |
86
|
4 |
|
'\Commercetools\Core\Model\ProductDiscount\ProductDiscountReference', |
87
|
4 |
|
ProductTypeReference::TYPE_PRODUCT_TYPE => '\Commercetools\Core\Model\ProductType\ProductTypeReference', |
88
|
4 |
|
OrderReference::TYPE_ORDER => '\Commercetools\Core\Model\Order\OrderReference', |
89
|
4 |
|
ShippingMethodReference::TYPE_SHIPPING_METHOD => |
90
|
4 |
|
'\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference', |
91
|
4 |
|
StateReference::TYPE_STATE => '\Commercetools\Core\Model\State\StateReference', |
92
|
4 |
|
TaxCategoryReference::TYPE_TAX_CATEGORY => '\Commercetools\Core\Model\TaxCategory\TaxCategoryReference', |
93
|
4 |
|
TypeReference::TYPE_TYPE => '\Commercetools\Core\Model\Type\TypeReference', |
94
|
4 |
|
ZoneReference::TYPE_ZONE => '\Commercetools\Core\Model\Zone\ZoneReference', |
95
|
|
|
]; |
96
|
4 |
|
return isset($types[$typeId]) ? $types[$typeId] : '\Commercetools\Core\Model\Common\Reference'; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.