1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Cart; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Common\Address; |
9
|
|
|
use Commercetools\Core\Model\Common\Resource; |
10
|
|
|
use Commercetools\Core\Model\Common\Money; |
11
|
|
|
use Commercetools\Core\Model\Common\TaxedPrice; |
12
|
|
|
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference; |
13
|
|
|
use Commercetools\Core\Model\CustomField\CustomFieldObject; |
14
|
|
|
use Commercetools\Core\Model\Common\DateTimeDecorator; |
15
|
|
|
use Commercetools\Core\Model\Payment\PaymentInfo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @package Commercetools\Core\Model\Cart |
19
|
|
|
* @link https://dev.commercetools.com/http-api-projects-carts.html#cart |
20
|
|
|
* @method string getId() |
21
|
|
|
* @method Cart setId(string $id = null) |
22
|
|
|
* @method int getVersion() |
23
|
|
|
* @method Cart setVersion(int $version = null) |
24
|
|
|
* @method DateTimeDecorator getCreatedAt() |
25
|
|
|
* @method Cart setCreatedAt(\DateTime $createdAt = null) |
26
|
|
|
* @method DateTimeDecorator getLastModifiedAt() |
27
|
|
|
* @method Cart setLastModifiedAt(\DateTime $lastModifiedAt = null) |
28
|
|
|
* @method string getCustomerId() |
29
|
|
|
* @method Cart setCustomerId(string $customerId = null) |
30
|
|
|
* @method string getCustomerEmail() |
31
|
|
|
* @method Cart setCustomerEmail(string $customerEmail = null) |
32
|
|
|
* @method LineItemCollection getLineItems() |
33
|
|
|
* @method Cart setLineItems(LineItemCollection $lineItems = null) |
34
|
|
|
* @method CustomLineItemCollection getCustomLineItems() |
35
|
|
|
* @method Cart setCustomLineItems(CustomLineItemCollection $customLineItems = null) |
36
|
|
|
* @method Money getTotalPrice() |
37
|
|
|
* @method Cart setTotalPrice(Money $totalPrice = null) |
38
|
|
|
* @method TaxedPrice getTaxedPrice() |
39
|
|
|
* @method Cart setTaxedPrice(TaxedPrice $taxedPrice = null) |
40
|
|
|
* @method string getCartState() |
41
|
|
|
* @method Cart setCartState(string $cartState = null) |
42
|
|
|
* @method Address getShippingAddress() |
43
|
|
|
* @method Cart setShippingAddress(Address $shippingAddress = null) |
44
|
|
|
* @method Address getBillingAddress() |
45
|
|
|
* @method Cart setBillingAddress(Address $billingAddress = null) |
46
|
|
|
* @method string getInventoryMode() |
47
|
|
|
* @method Cart setInventoryMode(string $inventoryMode = null) |
48
|
|
|
* @method CustomerGroupReference getCustomerGroup() |
49
|
|
|
* @method Cart setCustomerGroup(CustomerGroupReference $customerGroup = null) |
50
|
|
|
* @method string getCountry() |
51
|
|
|
* @method Cart setCountry(string $country = null) |
52
|
|
|
* @method ShippingInfo getShippingInfo() |
53
|
|
|
* @method Cart setShippingInfo(ShippingInfo $shippingInfo = null) |
54
|
|
|
* @method DiscountCodeInfoCollection getDiscountCodes() |
55
|
|
|
* @method Cart setDiscountCodes(DiscountCodeInfoCollection $discountCodes = null) |
56
|
|
|
* @method CustomFieldObject getCustom() |
57
|
|
|
* @method Cart setCustom(CustomFieldObject $custom = null) |
58
|
|
|
* @method PaymentInfo getPaymentInfo() |
59
|
|
|
* @method Cart setPaymentInfo(PaymentInfo $paymentInfo = null) |
60
|
|
|
* @method string getTaxMode() |
61
|
|
|
* @method Cart setTaxMode(string $taxMode = null) |
62
|
|
|
* @method string getAnonymousId() |
63
|
|
|
* @method Cart setAnonymousId(string $anonymousId = null) |
64
|
|
|
* @method string getLocale() |
65
|
|
|
* @method CartReference getReference() |
66
|
|
|
*/ |
67
|
|
|
class Cart extends Resource |
68
|
|
|
{ |
69
|
|
|
const TAX_MODE_PLATFORM = 'Platform'; |
70
|
|
|
const TAX_MODE_EXTERNAL = 'External'; |
71
|
|
|
const TAX_MODE_DISABLED = 'Disabled'; |
72
|
85 |
|
|
73
|
|
|
public function fieldDefinitions() |
74
|
|
|
{ |
75
|
85 |
|
return [ |
76
|
85 |
|
'id' => [static::TYPE => 'string'], |
77
|
|
|
'version' => [static::TYPE => 'int'], |
78
|
85 |
|
'createdAt' => [ |
79
|
85 |
|
static::TYPE => '\DateTime', |
80
|
|
|
static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator' |
81
|
|
|
], |
82
|
85 |
|
'lastModifiedAt' => [ |
83
|
85 |
|
static::TYPE => '\DateTime', |
84
|
|
|
static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator' |
85
|
85 |
|
], |
86
|
85 |
|
'customerId' => [static::TYPE => 'string'], |
87
|
85 |
|
'customerEmail' => [static::TYPE => 'string'], |
88
|
85 |
|
'lineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\LineItemCollection'], |
89
|
85 |
|
'customLineItems' => [static::TYPE => '\Commercetools\Core\Model\Cart\CustomLineItemCollection'], |
90
|
85 |
|
'totalPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\Money'], |
91
|
85 |
|
'taxedPrice' => [static::TYPE => '\Commercetools\Core\Model\Common\TaxedPrice'], |
92
|
85 |
|
'cartState' => [static::TYPE => 'string'], |
93
|
85 |
|
'shippingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
94
|
85 |
|
'billingAddress' => [static::TYPE => '\Commercetools\Core\Model\Common\Address'], |
95
|
85 |
|
'inventoryMode' => [static::TYPE => 'string'], |
96
|
85 |
|
'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'], |
97
|
85 |
|
'country' => [static::TYPE => 'string'], |
98
|
85 |
|
'shippingInfo' => [static::TYPE => '\Commercetools\Core\Model\Cart\ShippingInfo'], |
99
|
85 |
|
'discountCodes' => [static::TYPE => '\Commercetools\Core\Model\Cart\DiscountCodeInfoCollection'], |
100
|
85 |
|
'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'], |
101
|
85 |
|
'paymentInfo' => [static::TYPE => '\Commercetools\Core\Model\Payment\PaymentInfo'], |
102
|
85 |
|
'taxMode' => [static::TYPE => 'string'], |
103
|
|
|
'anonymousId' => [static::TYPE => 'string'], |
104
|
|
|
'locale' => [static::TYPE => 'string'], |
105
|
|
|
]; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
4 |
|
public function getLineItemCount() |
109
|
4 |
|
{ |
110
|
3 |
|
$count = 0; |
111
|
2 |
|
if ($this->getLineItems() instanceof LineItemCollection) { |
112
|
|
|
foreach ($this->getLineItems() as $lineItem) { |
113
|
|
|
$count+= $lineItem->getQuantity(); |
114
|
4 |
|
} |
115
|
|
|
} |
116
|
|
|
return $count; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function setLocale($locale) |
120
|
|
|
{ |
121
|
|
|
$locale = \Locale::canonicalize($locale); |
122
|
|
|
parent::setLocale($locale); |
|
|
|
|
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function toJson() |
131
|
|
|
{ |
132
|
|
|
$data = parent::toArray(); |
|
|
|
|
133
|
|
|
$data['locale'] = str_replace('_', '-', $data['locale']); |
134
|
|
|
|
135
|
|
|
return $data; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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: