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
|
|
|
/** |
49
|
|
|
* @param ClassMetadata $metadata |
50
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
51
|
|
|
*/ |
52
|
|
|
public static function loadMetadata(ClassMetadata $metadata): void |
53
|
|
|
{ |
54
|
|
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
55
|
|
|
MappingHelper::setSimpleFields( |
56
|
|
|
[ |
57
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE => MappingHelper::TYPE_STRING, |
58
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_FIRSTNAME => MappingHelper::TYPE_STRING, |
59
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_MIDDLENAMES => MappingHelper::TYPE_JSON, |
60
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_LASTNAME => MappingHelper::TYPE_STRING, |
61
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_SUFFIX => MappingHelper::TYPE_STRING, |
62
|
|
|
], |
63
|
|
|
$builder |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the full name as a single string |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
public function getFormatted(): string |
73
|
|
|
{ |
74
|
1 |
|
return $this->format( |
75
|
|
|
[ |
76
|
1 |
|
$this->getTitle(), |
77
|
1 |
|
$this->getFirstName(), |
78
|
1 |
|
$this->format($this->middleNames), |
79
|
1 |
|
$this->getLastName(), |
80
|
1 |
|
$this->getSuffix(), |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Convert the array into a single space separated list |
87
|
|
|
* |
88
|
|
|
* @param array $items |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
private function format(array $items): string |
93
|
|
|
{ |
94
|
|
|
return trim( |
95
|
|
|
implode( |
96
|
|
|
' ', |
97
|
|
|
array_map( |
98
|
|
|
'trim', |
99
|
|
|
array_filter( |
100
|
|
|
$items |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
1 |
|
public function getTitle(): string |
111
|
|
|
{ |
112
|
1 |
|
return $this->title ?? ''; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $title |
117
|
|
|
* |
118
|
|
|
* @return FullNameEmbeddableInterface |
119
|
|
|
*/ |
120
|
1 |
|
public function setTitle(string $title): FullNameEmbeddableInterface |
121
|
|
|
{ |
122
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
123
|
1 |
|
'title', |
124
|
1 |
|
$this->title, |
125
|
1 |
|
$title |
126
|
|
|
); |
127
|
1 |
|
$this->title = $title; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
public function getFirstName(): string |
136
|
|
|
{ |
137
|
1 |
|
return $this->firstName ?? ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $firstName |
142
|
|
|
* |
143
|
|
|
* @return FullNameEmbeddableInterface |
144
|
|
|
*/ |
145
|
1 |
|
public function setFirstName(string $firstName): FullNameEmbeddableInterface |
146
|
|
|
{ |
147
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
148
|
1 |
|
'firstName', |
149
|
1 |
|
$this->firstName, |
150
|
1 |
|
$firstName |
151
|
|
|
); |
152
|
1 |
|
$this->firstName = $firstName; |
153
|
|
|
|
154
|
1 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
1 |
|
public function getLastName(): string |
161
|
|
|
{ |
162
|
1 |
|
return $this->lastName ?? ''; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $lastName |
167
|
|
|
* |
168
|
|
|
* @return FullNameEmbeddableInterface |
169
|
|
|
*/ |
170
|
1 |
|
public function setLastName(string $lastName): FullNameEmbeddableInterface |
171
|
|
|
{ |
172
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
173
|
1 |
|
'lastName', |
174
|
1 |
|
$this->lastName, |
175
|
1 |
|
$lastName |
176
|
|
|
); |
177
|
1 |
|
$this->lastName = $lastName; |
178
|
|
|
|
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
1 |
|
public function getSuffix(): string |
186
|
|
|
{ |
187
|
1 |
|
return $this->suffix ?? ''; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $suffix |
192
|
|
|
* |
193
|
|
|
* @return FullNameEmbeddableInterface |
194
|
|
|
*/ |
195
|
1 |
|
public function setSuffix(string $suffix): FullNameEmbeddableInterface |
196
|
|
|
{ |
197
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
198
|
1 |
|
'suffix', |
199
|
1 |
|
$this->suffix, |
200
|
1 |
|
$suffix |
201
|
|
|
); |
202
|
1 |
|
$this->suffix = $suffix; |
203
|
|
|
|
204
|
1 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function __toString(): string |
208
|
|
|
{ |
209
|
|
|
return (string)print_r( |
210
|
|
|
[ |
211
|
|
|
'fullNameEmbeddabled' => [ |
212
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_TITLE => $this->getTitle(), |
213
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_FIRSTNAME => $this->getFirstName(), |
214
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_MIDDLENAMES => $this->getMiddleNames(), |
215
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_LASTNAME => $this->getLastName(), |
216
|
|
|
FullNameEmbeddableInterface::EMBEDDED_PROP_SUFFIX => $this->getSuffix(), |
217
|
|
|
], |
218
|
|
|
], |
219
|
|
|
true |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
1 |
|
public function getMiddleNames(): array |
227
|
|
|
{ |
228
|
1 |
|
return $this->middleNames ?? []; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param array $middleNames |
233
|
|
|
* |
234
|
|
|
* @return FullNameEmbeddableInterface |
235
|
|
|
*/ |
236
|
1 |
|
public function setMiddleNames(array $middleNames): FullNameEmbeddableInterface |
237
|
|
|
{ |
238
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
239
|
1 |
|
'middleNames', |
240
|
1 |
|
$this->middleNames, |
241
|
1 |
|
$middleNames |
242
|
|
|
); |
243
|
1 |
|
$this->middleNames = $middleNames; |
244
|
|
|
|
245
|
1 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
protected function getPrefix(): string |
249
|
|
|
{ |
250
|
|
|
return HasFullNameEmbeddableInterface::PROP_FULL_NAME_EMBEDDABLE; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|