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