|
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 http://dev.commercetools.com/http-api-projects-me-carts.html#mycartdraft |
|
17
|
|
|
* @method string getCurrency() |
|
18
|
|
|
* @method MyCartDraft setCurrency(string $currency = null) |
|
19
|
|
|
* @method string getCustomerEmail() |
|
20
|
|
|
* @method MyCartDraft setCustomerEmail(string $customerEmail = null) |
|
21
|
|
|
* @method string getCountry() |
|
22
|
|
|
* @method MyCartDraft setCountry(string $country = null) |
|
23
|
|
|
* @method string getInventoryMode() |
|
24
|
|
|
* @method MyCartDraft setInventoryMode(string $inventoryMode = null) |
|
25
|
|
|
* @method MyLineItemDraftCollection getLineItems() |
|
26
|
|
|
* @method MyCartDraft setLineItems(MyLineItemDraftCollection $lineItems = null) |
|
27
|
|
|
* @method Address getShippingAddress() |
|
28
|
|
|
* @method MyCartDraft setShippingAddress(Address $shippingAddress = null) |
|
29
|
|
|
* @method Address getBillingAddress() |
|
30
|
|
|
* @method MyCartDraft setBillingAddress(Address $billingAddress = null) |
|
31
|
|
|
* @method ShippingMethodReference getShippingMethod() |
|
32
|
|
|
* @method MyCartDraft setShippingMethod(ShippingMethodReference $shippingMethod = null) |
|
33
|
|
|
* @method CustomFieldObjectDraft getCustom() |
|
34
|
|
|
* @method MyCartDraft setCustom(CustomFieldObjectDraft $custom = null) |
|
35
|
|
|
* @method string getLocale() |
|
36
|
|
|
*/ |
|
37
|
|
|
class MyCartDraft extends JsonObject |
|
38
|
9 |
|
{ |
|
39
|
|
|
public function fieldDefinitions() |
|
40
|
|
|
{ |
|
41
|
9 |
|
return [ |
|
42
|
9 |
|
'currency' => [static::TYPE => 'string'], |
|
43
|
9 |
|
'customerEmail' => [static::TYPE => 'string'], |
|
44
|
9 |
|
'country' => [static::TYPE => 'string'], |
|
45
|
9 |
|
'inventoryMode' => [static::TYPE => 'string'], |
|
46
|
9 |
|
'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\MyLineItemDraftCollection'], |
|
47
|
9 |
|
'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
|
48
|
9 |
|
'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
|
49
|
9 |
|
'shippingMethod' => [static::TYPE => '\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference'], |
|
50
|
|
|
'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'], |
|
51
|
|
|
'locale' => [static::TYPE => 'string'], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $currency |
|
57
|
|
|
* @param Context|callable $context |
|
58
|
9 |
|
* @return CartDraft |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public static function ofCurrency($currency, $context = null) |
|
61
|
9 |
|
{ |
|
62
|
|
|
$draft = static::of($context); |
|
63
|
|
|
return $draft->setCurrency($currency); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setLocale($locale) |
|
67
|
|
|
{ |
|
68
|
|
|
$locale = \Locale::canonicalize($locale); |
|
69
|
|
|
parent::setLocale($locale); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function toJson() |
|
78
|
|
|
{ |
|
79
|
|
|
$data = parent::toArray(); |
|
|
|
|
|
|
80
|
|
|
$data['locale'] = str_replace('_', '-', $data['locale']); |
|
81
|
|
|
|
|
82
|
|
|
return $data; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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: