1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\NameParser; |
4
|
|
|
|
5
|
|
|
use TheIconic\NameParser\Part\AbstractPart; |
6
|
|
|
use TheIconic\NameParser\Part\GivenNamePart; |
7
|
|
|
|
8
|
|
|
class Name |
9
|
|
|
{ |
10
|
|
|
private const PARTS_NAMESPACE = 'TheIconic\NameParser\Part'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array the parts that make up this name |
14
|
|
|
*/ |
15
|
|
|
protected $parts = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* constructor takes the array of parts this name consists of |
19
|
|
|
* |
20
|
|
|
* @param array|null $parts |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $parts = null) |
23
|
|
|
{ |
24
|
|
|
if (null !== $parts) { |
25
|
|
|
$this->setParts($parts); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function __toString(): string |
33
|
|
|
{ |
34
|
|
|
return implode(' ', $this->getAll(true)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* set the parts this name consists of |
39
|
|
|
* |
40
|
|
|
* @param array $parts |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setParts(array $parts): Name |
44
|
|
|
{ |
45
|
|
|
$this->parts = $parts; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* get the parts this name consists of |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getParts(): array |
56
|
|
|
{ |
57
|
|
|
return $this->parts; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param bool $format |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getAll(bool $format = false): array |
65
|
|
|
{ |
66
|
|
|
$results = []; |
67
|
|
|
$keys = [ |
68
|
|
|
'salutation' => [], |
69
|
|
|
'firstname' => [], |
70
|
|
|
'nickname' => [$format], |
71
|
|
|
'middlename' => [], |
72
|
|
|
'initials' => [], |
73
|
|
|
'lastname' => [], |
74
|
|
|
'suffix' => [], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
foreach ($keys as $key => $args) { |
78
|
|
|
$method = sprintf('get%s', ucfirst($key)); |
79
|
|
|
if ($value = call_user_func_array(array($this, $method), $args)) { |
80
|
|
|
$results[$key] = $value; |
81
|
|
|
}; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $results; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* get the given name (first name, middle names and initials) |
89
|
|
|
* in the order they were entered while still applying normalisation |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getGivenName(): string |
94
|
|
|
{ |
95
|
|
|
return $this->export('GivenNamePart'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get the given name followed by the last name (including any prefixes) |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getFullName(): string |
104
|
|
|
{ |
105
|
|
|
return sprintf('%s %s', $this->getGivenName(), $this->getLastname()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* get the first name |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getFirstname(): string |
114
|
|
|
{ |
115
|
|
|
return $this->export('Firstname'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* get the last name |
120
|
|
|
* |
121
|
|
|
* @param bool $pure |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getLastname(bool $pure = false): string |
125
|
|
|
{ |
126
|
|
|
return $this->export('Lastname', $pure); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* get the last name prefix |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getLastnamePrefix(): string |
135
|
|
|
{ |
136
|
|
|
return $this->export('LastnamePrefix'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get the initials |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getInitials(): string |
145
|
|
|
{ |
146
|
|
|
return $this->export('Initial'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* get the suffix(es) |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getSuffix(): string |
155
|
|
|
{ |
156
|
|
|
return $this->export('Suffix'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* get the salutation(s) |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getSalutation(): string |
165
|
|
|
{ |
166
|
|
|
return $this->export('Salutation'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* get the nick name(s) |
171
|
|
|
* |
172
|
|
|
* @param bool $wrap |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getNickname(bool $wrap = false): string |
176
|
|
|
{ |
177
|
|
|
if ($wrap) { |
178
|
|
|
return sprintf('(%s)', $this->export('Nickname')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->export('Nickname'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* get the middle name(s) |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getMiddlename(): string |
190
|
|
|
{ |
191
|
|
|
return $this->export('Middlename'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* helper method used by getters to extract and format relevant name parts |
196
|
|
|
* |
197
|
|
|
* @param string $type |
198
|
|
|
* @param bool $strict |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected function export(string $type, bool $strict = false): string |
202
|
|
|
{ |
203
|
|
|
$matched = []; |
204
|
|
|
|
205
|
|
|
foreach ($this->parts as $part) { |
206
|
|
|
if ($part instanceof AbstractPart && $this->isType($part, $type, $strict)) { |
207
|
|
|
$matched[] = $part->normalize(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return implode(' ', $matched); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* helper method to check if a part is of the given type |
216
|
|
|
* |
217
|
|
|
* @param AbstractPart $part |
218
|
|
|
* @param string $type |
219
|
|
|
* @param bool $strict |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
protected function isType(AbstractPart $part, string $type, bool $strict = false): bool |
223
|
|
|
{ |
224
|
|
|
$className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type); |
225
|
|
|
|
226
|
|
|
if ($strict) { |
227
|
|
|
return get_class($part) === $className; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return is_a($part, $className); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|