1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Cart; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Common\Context; |
9
|
|
|
use Commercetools\Core\Model\Common\JsonObject; |
10
|
|
|
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft; |
11
|
|
|
use Commercetools\Core\Model\Common\Address; |
12
|
|
|
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @package Commercetools\Core\Model\Cart |
16
|
|
|
* @link https://dev.commercetools.com/http-api-projects-carts.html#cartdraft |
17
|
|
|
* @method string getCurrency() |
18
|
|
|
* @method string getCustomerId() |
19
|
|
|
* @method string getCountry() |
20
|
|
|
* @method string getInventoryMode() |
21
|
|
|
* @method CartDraft setCurrency(string $currency = null) |
22
|
|
|
* @method CartDraft setCustomerId(string $customerId = null) |
23
|
|
|
* @method CartDraft setCountry(string $country = null) |
24
|
|
|
* @method CartDraft setInventoryMode(string $inventoryMode = null) |
25
|
|
|
* @method CustomFieldObjectDraft getCustom() |
26
|
|
|
* @method CartDraft setCustom(CustomFieldObjectDraft $custom = null) |
27
|
|
|
* @method string getCustomerEmail() |
28
|
|
|
* @method CartDraft setCustomerEmail(string $customerEmail = null) |
29
|
|
|
* @method LineItemDraftCollection getLineItems() |
30
|
|
|
* @method CartDraft setLineItems(LineItemDraftCollection $lineItems = null) |
31
|
|
|
* @method Address getShippingAddress() |
32
|
|
|
* @method CartDraft setShippingAddress(Address $shippingAddress = null) |
33
|
|
|
* @method Address getBillingAddress() |
34
|
|
|
* @method CartDraft setBillingAddress(Address $billingAddress = null) |
35
|
|
|
* @method ShippingMethodReference getShippingMethod() |
36
|
|
|
* @method CartDraft setShippingMethod(ShippingMethodReference $shippingMethod = null) |
37
|
|
|
* @method CustomLineItemDraftCollection getCustomLineItems() |
38
|
|
|
* @method CartDraft setCustomLineItems(CustomLineItemDraftCollection $customLineItems = null) |
39
|
|
|
* @method string getTaxMode() |
40
|
|
|
* @method CartDraft setTaxMode(string $taxMode = null) |
41
|
|
|
* @method string getAnonymousId() |
42
|
|
|
* @method CartDraft setAnonymousId(string $anonymousId = null) |
43
|
|
|
* @method string getLocale() |
44
|
|
|
*/ |
45
|
|
|
class CartDraft extends JsonObject |
46
|
72 |
|
{ |
47
|
|
|
public function fieldDefinitions() |
48
|
|
|
{ |
49
|
72 |
|
return [ |
50
|
72 |
|
'currency' => [static::TYPE => 'string'], |
51
|
72 |
|
'customerId' => [static::TYPE => 'string'], |
52
|
72 |
|
'customerEmail' => [static::TYPE => 'string'], |
53
|
72 |
|
'country' => [static::TYPE => 'string'], |
54
|
72 |
|
'inventoryMode' => [static::TYPE => 'string'], |
55
|
72 |
|
'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\LineItemDraftCollection'], |
56
|
72 |
|
'customLineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\CustomLineItemDraftCollection'], |
57
|
72 |
|
'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
58
|
72 |
|
'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
59
|
72 |
|
'shippingMethod' => [static::TYPE => '\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference'], |
60
|
72 |
|
'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'], |
61
|
72 |
|
'taxMode' => [static::TYPE => 'string'], |
62
|
|
|
'anonymousId' => [static::TYPE => 'string'], |
63
|
|
|
'locale' => [static::TYPE => 'string'], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $currency |
69
|
|
|
* @param Context|callable $context |
70
|
72 |
|
* @return CartDraft |
71
|
|
|
*/ |
72
|
72 |
|
public static function ofCurrency($currency, $context = null) |
73
|
72 |
|
{ |
74
|
|
|
$draft = static::of($context); |
75
|
|
|
return $draft->setCurrency($currency); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setLocale($locale) |
79
|
|
|
{ |
80
|
|
|
$locale = \Locale::canonicalize($locale); |
81
|
|
|
parent::setLocale($locale); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function toJson() |
90
|
|
|
{ |
91
|
|
|
$data = parent::toArray(); |
|
|
|
|
92
|
|
|
$data['locale'] = str_replace('_', '-', $data['locale']); |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: