1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\User\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Sylius\Component\Resource\Model\SoftDeletableTrait; |
17
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Michał Marcinkowski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Customer implements CustomerInterface, GroupableInterface |
23
|
|
|
{ |
24
|
|
|
use SoftDeletableTrait, TimestampableTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var mixed |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var UserInterface |
33
|
|
|
*/ |
34
|
|
|
protected $user; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $email; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $emailCanonical; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $firstName; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $lastName; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \DateTime |
58
|
|
|
*/ |
59
|
|
|
protected $birthday; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $gender = CustomerInterface::UNKNOWN_GENDER; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Collection|GroupInterface[] |
68
|
|
|
*/ |
69
|
|
|
protected $groups; |
70
|
|
|
|
71
|
|
|
public function __construct() |
72
|
|
|
{ |
73
|
|
|
$this->groups = new ArrayCollection(); |
74
|
|
|
$this->createdAt = new \DateTime(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getId() |
81
|
|
|
{ |
82
|
|
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getUser() |
89
|
|
|
{ |
90
|
|
|
return $this->user; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function setUser(UserInterface $user = null) |
97
|
|
|
{ |
98
|
|
|
if ($this->user !== $user) { |
99
|
|
|
$this->user = $user; |
100
|
|
|
$this->assignCustomer($user); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function hasUser() |
108
|
|
|
{ |
109
|
|
|
return null !== $this->user; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getEmail() |
116
|
|
|
{ |
117
|
|
|
return $this->email; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function setEmail($email) |
124
|
|
|
{ |
125
|
|
|
$this->email = $email; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function getEmailCanonical() |
132
|
|
|
{ |
133
|
|
|
return $this->emailCanonical; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function setEmailCanonical($emailCanonical) |
140
|
|
|
{ |
141
|
|
|
$this->emailCanonical = $emailCanonical; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getFullName() |
148
|
|
|
{ |
149
|
|
|
return sprintf('%s %s', $this->firstName, $this->lastName); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function getFirstName() |
156
|
|
|
{ |
157
|
|
|
return $this->firstName; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function setFirstName($firstName) |
164
|
|
|
{ |
165
|
|
|
$this->firstName = $firstName; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function getLastName() |
172
|
|
|
{ |
173
|
|
|
return $this->lastName; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function setLastName($lastName) |
180
|
|
|
{ |
181
|
|
|
$this->lastName = $lastName; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function getBirthday() |
188
|
|
|
{ |
189
|
|
|
return $this->birthday; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function setBirthday(\DateTime $birthday = null) |
196
|
|
|
{ |
197
|
|
|
$this->birthday = $birthday; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function getGender() |
204
|
|
|
{ |
205
|
|
|
return $this->gender; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function setGender($gender) |
212
|
|
|
{ |
213
|
|
|
$this->gender = $gender; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
|
|
public function isMale() |
220
|
|
|
{ |
221
|
|
|
return CustomerInterface::MALE_GENDER === $this->gender; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function isFemale() |
228
|
|
|
{ |
229
|
|
|
return CustomerInterface::FEMALE_GENDER === $this->gender; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
|
|
public function getGroups() |
236
|
|
|
{ |
237
|
|
|
return $this->groups; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
|
|
public function hasGroup($name) |
244
|
|
|
{ |
245
|
|
|
return in_array($name, $this->getGroupNames(), true); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
|
|
public function getGroupNames() |
252
|
|
|
{ |
253
|
|
|
$names = array(); |
254
|
|
|
foreach ($this->groups as $group) { |
255
|
|
|
$names[] = $group->getName(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $names; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
|
|
public function addGroup(GroupInterface $group) |
265
|
|
|
{ |
266
|
|
|
if (!$this->groups->contains($group)) { |
267
|
|
|
$this->groups->add($group); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritdoc} |
273
|
|
|
*/ |
274
|
|
|
public function removeGroup(GroupInterface $group) |
275
|
|
|
{ |
276
|
|
|
if ($this->groups->contains($group)) { |
277
|
|
|
$this->groups->removeElement($group); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function __toString() |
282
|
|
|
{ |
283
|
|
|
return (string) $this->getEmail(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param UserInterface $user |
288
|
|
|
*/ |
289
|
|
|
protected function assignCustomer(UserInterface $user = null) |
290
|
|
|
{ |
291
|
|
|
if (null !== $user) { |
292
|
|
|
$user->setCustomer($this); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|