1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Identity; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Identity\HasFullNameEmbeddableInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Objects\Identity\FullNameEmbeddableInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\AbstractEmbeddableObject; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
|
13
|
|
|
class FullNameEmbeddable extends AbstractEmbeddableObject implements FullNameEmbeddableInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The title or Honorific Prefix, for example Mr, Dr |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $title; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The first or given name |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $firstName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* An array of middle names |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $middleNames; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The last or surname |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $lastName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The honorific suffix, for example Jr |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $suffix; |
49
|
|
|
|
50
|
2 |
|
public function __construct(string $title, string $firstName, array $middleNames, string $lastName, string $suffix) |
51
|
|
|
{ |
52
|
2 |
|
$this->setTitle($title); |
53
|
2 |
|
$this->setFirstName($firstName); |
54
|
2 |
|
$this->setMiddleNames($middleNames); |
55
|
2 |
|
$this->setLastName($lastName); |
56
|
2 |
|
$this->setSuffix($suffix); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $title |
61
|
|
|
* |
62
|
|
|
* @return FullNameEmbeddableInterface |
63
|
|
|
*/ |
64
|
|
|
private function setTitle(string $title): FullNameEmbeddableInterface |
65
|
|
|
{ |
66
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
67
|
|
|
'title', |
68
|
|
|
$this->title, |
69
|
|
|
$title |
70
|
|
|
); |
71
|
|
|
$this->title = $title; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $firstName |
78
|
|
|
* |
79
|
|
|
* @return FullNameEmbeddableInterface |
80
|
|
|
*/ |
81
|
|
|
private function setFirstName(string $firstName): FullNameEmbeddableInterface |
82
|
|
|
{ |
83
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
84
|
|
|
'firstName', |
85
|
|
|
$this->firstName, |
86
|
|
|
$firstName |
87
|
|
|
); |
88
|
|
|
$this->firstName = $firstName; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $middleNames |
95
|
|
|
* |
96
|
|
|
* @return FullNameEmbeddableInterface |
97
|
|
|
*/ |
98
|
|
|
private function setMiddleNames(array $middleNames): FullNameEmbeddableInterface |
99
|
|
|
{ |
100
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
101
|
|
|
'middleNames', |
102
|
|
|
$this->middleNames, |
103
|
|
|
$middleNames |
104
|
|
|
); |
105
|
|
|
$this->middleNames = $middleNames; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $lastName |
112
|
|
|
* |
113
|
|
|
* @return FullNameEmbeddableInterface |
114
|
|
|
*/ |
115
|
|
|
private function setLastName(string $lastName): FullNameEmbeddableInterface |
116
|
|
|
{ |
117
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
118
|
|
|
'lastName', |
119
|
|
|
$this->lastName, |
120
|
|
|
$lastName |
121
|
|
|
); |
122
|
|
|
$this->lastName = $lastName; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $suffix |
129
|
|
|
* |
130
|
|
|
* @return FullNameEmbeddableInterface |
131
|
|
|
*/ |
132
|
|
|
private function setSuffix(string $suffix): FullNameEmbeddableInterface |
133
|
|
|
{ |
134
|
|
|
$this->notifyEmbeddablePrefixedProperties( |
135
|
|
|
'suffix', |
136
|
|
|
$this->suffix, |
137
|
|
|
$suffix |
138
|
|
|
); |
139
|
|
|
$this->suffix = $suffix; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ClassMetadata $metadata |
146
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
147
|
|
|
*/ |
148
|
|
|
public static function loadMetadata(ClassMetadata $metadata): void |
149
|
|
|
{ |
150
|
|
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
151
|
|
|
MappingHelper::setSimpleFields( |
152
|
|
|
[ |
153
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE => MappingHelper::TYPE_STRING, |
154
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_FIRSTNAME => MappingHelper::TYPE_STRING, |
155
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_MIDDLENAMES => MappingHelper::TYPE_ARRAY, |
156
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_LASTNAME => MappingHelper::TYPE_STRING, |
157
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_SUFFIX => MappingHelper::TYPE_STRING, |
158
|
|
|
], |
159
|
|
|
$builder |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $properties |
165
|
|
|
* |
166
|
|
|
* @return FullNameEmbeddableInterface |
167
|
|
|
*/ |
168
|
2 |
|
public static function create(array $properties): FullNameEmbeddableInterface |
169
|
|
|
{ |
170
|
2 |
|
if (FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE === key($properties)) { |
171
|
2 |
|
return new self( |
172
|
2 |
|
$properties[FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE], |
173
|
2 |
|
$properties[FullNameEmbeddableInterface::EMBEDDED_PROP_FIRSTNAME], |
174
|
2 |
|
$properties[FullNameEmbeddableInterface::EMBEDDED_PROP_MIDDLENAMES], |
175
|
2 |
|
$properties[FullNameEmbeddableInterface::EMBEDDED_PROP_LASTNAME], |
176
|
2 |
|
$properties[FullNameEmbeddableInterface::EMBEDDED_PROP_SUFFIX] |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new self(...array_values($properties)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the full name as a single string |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
2 |
|
public function getFormatted(): string |
189
|
|
|
{ |
190
|
2 |
|
return $this->format( |
191
|
|
|
[ |
192
|
2 |
|
$this->getTitle(), |
193
|
2 |
|
$this->getFirstName(), |
194
|
2 |
|
$this->format($this->middleNames), |
195
|
2 |
|
$this->getLastName(), |
196
|
2 |
|
$this->getSuffix(), |
197
|
|
|
] |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Convert the array into a single space separated list |
203
|
|
|
* |
204
|
|
|
* @param array $items |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
private function format(array $items): string |
209
|
|
|
{ |
210
|
|
|
return trim( |
211
|
|
|
implode( |
212
|
|
|
' ', |
213
|
|
|
array_map( |
214
|
|
|
'trim', |
215
|
|
|
array_filter( |
216
|
|
|
$items |
217
|
|
|
) |
218
|
|
|
) |
219
|
|
|
) |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
2 |
|
public function getTitle(): string |
227
|
|
|
{ |
228
|
2 |
|
return $this->title ?? ''; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
2 |
|
public function getFirstName(): string |
235
|
|
|
{ |
236
|
2 |
|
return $this->firstName ?? ''; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
2 |
|
public function getLastName(): string |
243
|
|
|
{ |
244
|
2 |
|
return $this->lastName ?? ''; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
2 |
|
public function getSuffix(): string |
251
|
|
|
{ |
252
|
2 |
|
return $this->suffix ?? ''; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function __toString(): string |
256
|
|
|
{ |
257
|
|
|
return (string)print_r( |
258
|
|
|
[ |
259
|
|
|
'fullNameEmbeddabled' => [ |
260
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE => $this->getTitle(), |
261
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_FIRSTNAME => $this->getFirstName(), |
262
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_MIDDLENAMES => $this->getMiddleNames(), |
263
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_LASTNAME => $this->getLastName(), |
264
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_SUFFIX => $this->getSuffix(), |
265
|
|
|
], |
266
|
|
|
], |
267
|
|
|
true |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
2 |
|
public function getMiddleNames(): array |
275
|
|
|
{ |
276
|
2 |
|
return $this->middleNames ?? []; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
protected function getPrefix(): string |
280
|
|
|
{ |
281
|
|
|
return HasFullNameEmbeddableInterface::PROP_FULL_NAME_EMBEDDABLE; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|