1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enzyme\Name; |
4
|
|
|
|
5
|
|
|
use Stringy\Stringy as S; |
6
|
|
|
|
7
|
|
|
class Simple |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name prefix. |
11
|
|
|
* |
12
|
|
|
* @var Part |
13
|
|
|
*/ |
14
|
|
|
protected $prefix; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The first name. |
18
|
|
|
* |
19
|
|
|
* @var Part |
20
|
|
|
*/ |
21
|
|
|
protected $first; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The middle name. |
25
|
|
|
* |
26
|
|
|
* @var Part |
27
|
|
|
*/ |
28
|
|
|
protected $middle; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The last name. |
32
|
|
|
* |
33
|
|
|
* @var Part |
34
|
|
|
*/ |
35
|
|
|
protected $last; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new name given the specified parts. |
39
|
|
|
* |
40
|
|
|
* @param Part|null $prefix The prefix |
41
|
|
|
* @param Part|null $first The first name. |
42
|
|
|
* @param Part|null $middle The middle name. |
43
|
|
|
* @param Part|null $last The last name. |
44
|
|
|
*/ |
45
|
|
|
protected function __construct( |
46
|
|
|
Part $prefix = null, |
47
|
|
|
Part $first = null, |
48
|
|
|
Part $middle = null, |
49
|
|
|
Part $last = null |
50
|
|
|
) { |
51
|
|
|
$this->prefix = $prefix; |
52
|
|
|
$this->first = $first; |
53
|
|
|
$this->middle = $middle; |
54
|
|
|
$this->last = $last; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Try to intelligently create a name from the given string. |
59
|
|
|
* |
60
|
|
|
* @param string $name The name. |
61
|
|
|
* |
62
|
|
|
* @return Simple |
63
|
|
|
*/ |
64
|
|
|
public static function fromString($name) |
65
|
|
|
{ |
66
|
|
|
$name = S::create($name)->collapseWhitespace(); |
67
|
|
|
|
68
|
|
|
$parts = $name->split(' '); |
69
|
|
|
$segments = array_filter($parts, function($part) { |
70
|
|
|
return S::create($part)->trim()->length() > 0; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$count = count($segments); |
74
|
|
|
|
75
|
|
|
if ($count < 1) { |
76
|
|
|
throw new NameException('The string provided could not be processed.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return static::processName($segments); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Build a name from the given function arguments. |
84
|
|
|
* |
85
|
|
|
* @return Simple |
86
|
|
|
*/ |
87
|
|
|
public static function fromArgs() |
88
|
|
|
{ |
89
|
|
|
$num_args = func_num_args(); |
90
|
|
|
$args = func_get_args(); |
91
|
|
|
|
92
|
|
|
switch ($num_args) { |
93
|
|
|
case 0: |
94
|
|
|
throw new NameException('At least one argument should be passed.'); |
95
|
|
|
|
96
|
|
|
case 1: |
97
|
|
|
return new static( |
98
|
|
|
null, |
99
|
|
|
new Part($args[0]) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
case 2: |
103
|
|
|
return new static( |
104
|
|
|
null, |
105
|
|
|
new Part($args[0]), |
106
|
|
|
null, |
107
|
|
|
new Part($args[1]) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
View Code Duplication |
case 3: |
|
|
|
|
111
|
|
|
return new static( |
112
|
|
|
null, |
113
|
|
|
new Part($args[0]), |
114
|
|
|
new Part($args[1]), |
115
|
|
|
new Part($args[2]) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
View Code Duplication |
case 4: |
|
|
|
|
119
|
|
|
return new static( |
120
|
|
|
new Part($args[0]), |
121
|
|
|
new Part($args[1]), |
122
|
|
|
new Part($args[2]), |
123
|
|
|
new Part($args[3]) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
default: |
127
|
|
|
throw new NameException('Too many arguments provided.'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return a fresh instance of Simple. |
133
|
|
|
* |
134
|
|
|
* @return Simple |
135
|
|
|
*/ |
136
|
|
|
public static function strict() |
137
|
|
|
{ |
138
|
|
|
return new static(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the prefix for this name |
143
|
|
|
* |
144
|
|
|
* @param Part $prefix The prefix. |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function prefix(Part $prefix) |
149
|
|
|
{ |
150
|
|
|
$this->prefix = $prefix; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the first part of this name |
157
|
|
|
* |
158
|
|
|
* @param Part $first The first part. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function first(Part $first) |
163
|
|
|
{ |
164
|
|
|
$this->first = $first; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set the middle part of this name |
171
|
|
|
* |
172
|
|
|
* @param Part $middle The middle part. |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function middle(Part $middle) |
177
|
|
|
{ |
178
|
|
|
$this->middle = $middle; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the last part of this name |
185
|
|
|
* |
186
|
|
|
* @param Part $last The last part. |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function last(Part $last) |
191
|
|
|
{ |
192
|
|
|
$this->last = $last; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the prefix for this name. |
199
|
|
|
* |
200
|
|
|
* @return Part |
201
|
|
|
*/ |
202
|
|
|
public function getPrefix() |
203
|
|
|
{ |
204
|
|
|
return $this->prefix; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the first part of this name. |
209
|
|
|
* |
210
|
|
|
* @return Part |
211
|
|
|
*/ |
212
|
|
|
public function getFirst() |
213
|
|
|
{ |
214
|
|
|
return $this->first; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the middle part of this name. |
219
|
|
|
* |
220
|
|
|
* @return Part |
221
|
|
|
*/ |
222
|
|
|
public function getMiddle() |
223
|
|
|
{ |
224
|
|
|
return $this->middle; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the last part of this name. |
229
|
|
|
* |
230
|
|
|
* @return Part |
231
|
|
|
*/ |
232
|
|
|
public function getLast() |
233
|
|
|
{ |
234
|
|
|
return $this->last; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Process this name given the segments. |
239
|
|
|
* |
240
|
|
|
* @param array $segments The name segments. |
241
|
|
|
* |
242
|
|
|
* @return Simple |
243
|
|
|
*/ |
244
|
|
|
protected static function processName($segments) |
245
|
|
|
{ |
246
|
|
|
$name = new static(); |
247
|
|
|
|
248
|
|
|
static::tryExtractPrefix($name, $segments); |
249
|
|
|
static::tryExtractExtended($name, $segments); |
250
|
|
|
static::tryExtractStandard($name, $segments); |
251
|
|
|
static::tryExtractSimple($name, $segments); |
252
|
|
|
|
253
|
|
|
return $name; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Try and extract simply a first name. |
258
|
|
|
* |
259
|
|
|
* @param Simple $name The name to process. |
260
|
|
|
* @param array &$segments The given segments. |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
protected static function tryExtractSimple($name, &$segments) |
265
|
|
|
{ |
266
|
|
|
if (count($segments) > 0 && count($segments) < 2) { |
267
|
|
|
// First name. |
268
|
|
|
$name->first(new Part($segments[0])); |
269
|
|
|
array_shift($segments); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Try and extract simply a first and last name. |
275
|
|
|
* |
276
|
|
|
* @param Simple $name The name to process. |
277
|
|
|
* @param array &$segments The given segments. |
278
|
|
|
* |
279
|
|
|
* @return void |
280
|
|
|
*/ |
281
|
|
|
protected static function tryExtractStandard($name, &$segments) |
282
|
|
|
{ |
283
|
|
|
if (count($segments) > 0 && count($segments) === 2) { |
284
|
|
|
// First name. |
285
|
|
|
$name->first(new Part($segments[0])); |
286
|
|
|
array_shift($segments); |
287
|
|
|
|
288
|
|
|
// Last name. |
289
|
|
|
$name->last(new Part(array_pop($segments))); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Try and extract a first, middle and last name. |
295
|
|
|
* |
296
|
|
|
* @param Simple $name The name to process. |
297
|
|
|
* @param array &$segments The given segments. |
298
|
|
|
* |
299
|
|
|
* @return void |
300
|
|
|
*/ |
301
|
|
|
protected static function tryExtractExtended($name, &$segments) |
302
|
|
|
{ |
303
|
|
|
if (count($segments) > 2) { |
304
|
|
|
// First name. |
305
|
|
|
$name->first(new Part($segments[0])); |
306
|
|
|
array_shift($segments); |
307
|
|
|
|
308
|
|
|
// Last name. |
309
|
|
|
$name->last(new Part(array_pop($segments))); |
310
|
|
|
|
311
|
|
|
// Middle name. |
312
|
|
|
$name->middle(new Part(implode(' ', $segments))); |
313
|
|
|
$segments = []; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Try and extract the prefix for this name. |
319
|
|
|
* |
320
|
|
|
* @param Simple $name The name to process. |
321
|
|
|
* @param array &$segments The given segments. |
322
|
|
|
* |
323
|
|
|
* @return void |
324
|
|
|
*/ |
325
|
|
|
protected static function tryExtractPrefix($name, &$segments) |
326
|
|
|
{ |
327
|
|
|
if (stripos($segments[0], '.') !== false) { |
328
|
|
|
// Prefix. |
329
|
|
|
$name->prefix(new Part($segments[0])); |
330
|
|
|
array_shift($segments); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.