1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\CartDiscount; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Common\JsonObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Commercetools\Core\Model\CartDiscount |
12
|
|
|
* @link https://dev.commercetools.com/http-api-projects-cartDiscounts.html#cartdiscounttarget |
13
|
|
|
* @method string getType() |
14
|
|
|
* @method CartDiscountTarget setType(string $type = null) |
15
|
|
|
* @method string getPredicate() |
16
|
|
|
* @method CartDiscountTarget setPredicate(string $predicate = null) |
17
|
|
|
*/ |
18
|
|
|
class CartDiscountTarget extends JsonObject |
19
|
|
|
{ |
20
|
|
|
const TARGET_TYPE = ''; |
21
|
|
|
const TYPE_LINE_ITEMS = 'lineItems'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
43 |
|
public function __construct(array $data = [], $context = null) |
27
|
|
|
{ |
28
|
43 |
|
if (static::TARGET_TYPE != '' && !isset($data[static::TYPE])) { |
29
|
6 |
|
$data[static::TYPE] = static::TARGET_TYPE; |
30
|
|
|
} |
31
|
43 |
|
parent::__construct($data, $context); |
32
|
43 |
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
40 |
|
public function fieldDefinitions() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
40 |
|
'type' => [static::TYPE => 'string'], |
39
|
40 |
|
'predicate' => [static::TYPE => 'string'], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
7 |
|
public static function fromArray(array $data, $context = null) |
47
|
|
|
{ |
48
|
7 |
|
if (get_called_class() === CartDiscountTarget::class && isset($data[static::TYPE])) { |
|
|
|
|
49
|
6 |
|
$className = static::targetType($data[static::TYPE]); |
50
|
6 |
|
if (class_exists($className)) { |
51
|
6 |
|
return new $className($data, $context); |
52
|
|
|
} |
53
|
|
|
} |
54
|
1 |
|
return new static($data, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
protected static function targetType($typeId) |
58
|
|
|
{ |
59
|
|
|
$types = [ |
60
|
6 |
|
LineItemsTarget::TARGET_TYPE => LineItemsTarget::class, |
61
|
6 |
|
CustomLineItemsTarget::TARGET_TYPE => CustomLineItemsTarget::class, |
62
|
6 |
|
ShippingCostTarget::TARGET_TYPE => ShippingCostTarget::class, |
63
|
6 |
|
MultiBuyLineItemsTarget::TARGET_TYPE => MultiBuyLineItemsTarget::class, |
64
|
|
|
]; |
65
|
6 |
|
return isset($types[$typeId]) ? $types[$typeId] : CartDiscountTarget::class; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.