1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Customer; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Common\AddressCollection; |
9
|
|
|
use Commercetools\Core\Model\Common\Resource; |
10
|
|
|
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference; |
11
|
|
|
use Commercetools\Core\Model\CustomField\CustomFieldObject; |
12
|
|
|
use Commercetools\Core\Model\Common\DateTimeDecorator; |
13
|
|
|
use Commercetools\Core\Model\Common\DateDecorator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @package Commercetools\Core\Model\Customer |
17
|
|
|
* @link https://dev.commercetools.com/http-api-projects-customers.html#customer |
18
|
|
|
* @method string getId() |
19
|
|
|
* @method Customer setId(string $id = null) |
20
|
|
|
* @method int getVersion() |
21
|
|
|
* @method Customer setVersion(int $version = null) |
22
|
|
|
* @method string getCustomerNumber() |
23
|
|
|
* @method Customer setCustomerNumber(string $customerNumber = null) |
24
|
|
|
* @method DateTimeDecorator getCreatedAt() |
25
|
|
|
* @method Customer setCreatedAt(\DateTime $createdAt = null) |
26
|
|
|
* @method DateTimeDecorator getLastModifiedAt() |
27
|
|
|
* @method Customer setLastModifiedAt(\DateTime $lastModifiedAt = null) |
28
|
|
|
* @method string getEmail() |
29
|
|
|
* @method Customer setEmail(string $email = null) |
30
|
|
|
* @method string getFirstName() |
31
|
|
|
* @method Customer setFirstName(string $firstName = null) |
32
|
|
|
* @method string getLastName() |
33
|
|
|
* @method Customer setLastName(string $lastName = null) |
34
|
|
|
* @method string getPassword() |
35
|
|
|
* @method Customer setPassword(string $password = null) |
36
|
|
|
* @method string getMiddleName() |
37
|
|
|
* @method Customer setMiddleName(string $middleName = null) |
38
|
|
|
* @method string getTitle() |
39
|
|
|
* @method Customer setTitle(string $title = null) |
40
|
|
|
* @method DateDecorator getDateOfBirth() |
41
|
|
|
* @method Customer setDateOfBirth(\DateTime $dateOfBirth = null) |
42
|
|
|
* @method string getCompanyName() |
43
|
|
|
* @method Customer setCompanyName(string $companyName = null) |
44
|
|
|
* @method string getVatId() |
45
|
|
|
* @method Customer setVatId(string $vatId = null) |
46
|
|
|
* @method AddressCollection getAddresses() |
47
|
|
|
* @method Customer setAddresses(AddressCollection $addresses = null) |
48
|
|
|
* @method string getDefaultShippingAddressId() |
49
|
|
|
* @method Customer setDefaultShippingAddressId(string $defaultShippingAddressId = null) |
50
|
|
|
* @method string getDefaultBillingAddressId() |
51
|
|
|
* @method Customer setDefaultBillingAddressId(string $defaultBillingAddressId = null) |
52
|
|
|
* @method bool getIsEmailVerified() |
53
|
|
|
* @method Customer setIsEmailVerified(bool $isEmailVerified = null) |
54
|
|
|
* @method string getExternalId() |
55
|
|
|
* @method Customer setExternalId(string $externalId = null) |
56
|
|
|
* @method CustomerGroupReference getCustomerGroup() |
57
|
|
|
* @method Customer setCustomerGroup(CustomerGroupReference $customerGroup = null) |
58
|
|
|
* @method CustomFieldObject getCustom() |
59
|
|
|
* @method Customer setCustom(CustomFieldObject $custom = null) |
60
|
|
|
* @method string getLocale() |
61
|
|
|
* @method CustomerReference getReference() |
62
|
|
|
*/ |
63
|
|
|
class Customer extends Resource |
64
|
83 |
|
{ |
65
|
|
|
public function fieldDefinitions() |
66
|
|
|
{ |
67
|
83 |
|
return [ |
68
|
83 |
|
'id' => [static::TYPE => 'string'], |
69
|
83 |
|
'version' => [static::TYPE => 'int'], |
70
|
|
|
'customerNumber' => [static::TYPE => 'string'], |
71
|
83 |
|
'createdAt' => [ |
72
|
83 |
|
static::TYPE => '\DateTime', |
73
|
|
|
static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator' |
74
|
|
|
], |
75
|
83 |
|
'lastModifiedAt' => [ |
76
|
83 |
|
static::TYPE => '\DateTime', |
77
|
|
|
static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator' |
78
|
83 |
|
], |
79
|
83 |
|
'email' => [static::TYPE => 'string'], |
80
|
83 |
|
'firstName' => [static::TYPE => 'string'], |
81
|
83 |
|
'lastName' => [static::TYPE => 'string'], |
82
|
83 |
|
'password' => [static::TYPE => 'string'], |
83
|
83 |
|
'middleName' => [static::TYPE => 'string'], |
84
|
|
|
'title' => [static::TYPE => 'string'], |
85
|
83 |
|
'dateOfBirth' => [ |
86
|
83 |
|
static::TYPE => '\DateTime', |
87
|
|
|
static::DECORATOR => '\Commercetools\Core\Model\Common\DateDecorator' |
88
|
83 |
|
], |
89
|
83 |
|
'companyName' => [static::TYPE => 'string'], |
90
|
83 |
|
'vatId' => [static::TYPE => 'string'], |
91
|
83 |
|
'addresses' => [static::TYPE => '\Commercetools\Core\Model\Common\AddressCollection'], |
92
|
83 |
|
'defaultShippingAddressId' => [static::TYPE => 'string'], |
93
|
83 |
|
'defaultBillingAddressId' => [static::TYPE => 'string'], |
94
|
83 |
|
'isEmailVerified' => [static::TYPE => 'bool'], |
95
|
83 |
|
'externalId' => [static::TYPE => 'string'], |
96
|
83 |
|
'customerGroup' => [static::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'], |
97
|
|
|
'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'], |
98
|
|
|
'locale' => [static::TYPE => 'string'], |
99
|
|
|
]; |
100
|
25 |
|
} |
101
|
|
|
|
102
|
25 |
|
public function getDefaultShippingAddress() |
103
|
24 |
|
{ |
104
|
|
|
if (!is_null($this->getAddresses())) { |
105
|
1 |
|
return $this->getAddresses()->getById($this->getDefaultShippingAddressId()); |
106
|
|
|
} |
107
|
|
|
return null; |
108
|
25 |
|
} |
109
|
|
|
|
110
|
25 |
|
public function getDefaultBillingAddress() |
111
|
24 |
|
{ |
112
|
|
|
if (!is_null($this->getAddresses())) { |
113
|
1 |
|
return $this->getAddresses()->getById($this->getDefaultBillingAddressId()); |
114
|
|
|
} |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setLocale($locale) |
119
|
|
|
{ |
120
|
|
|
$locale = \Locale::canonicalize($locale); |
121
|
|
|
parent::setLocale($locale); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function toJson() |
130
|
|
|
{ |
131
|
|
|
$data = parent::toArray(); |
|
|
|
|
132
|
|
|
$data['locale'] = str_replace('_', '-', $data['locale']); |
133
|
|
|
|
134
|
|
|
return $data; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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: