1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\NameParser; |
4
|
|
|
|
5
|
|
|
use TheIconic\NameParser\Part\AbstractPart; |
6
|
|
|
|
7
|
|
|
class Name |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array the parts that make up this name |
11
|
|
|
*/ |
12
|
|
|
protected $parts = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* constructor takes the array of parts this name consists of |
16
|
|
|
* |
17
|
|
|
* @param array|null $parts |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $parts = null) |
20
|
|
|
{ |
21
|
|
|
if (null !== $parts) { |
22
|
|
|
$this->setParts($parts); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function __toString(): string |
30
|
|
|
{ |
31
|
|
|
return implode(' ', $this->getAll()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* set the parts this name consists of |
36
|
|
|
* |
37
|
|
|
* @param array $parts |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function setParts(array $parts): Name |
41
|
|
|
{ |
42
|
|
|
$this->parts = $parts; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* get the parts this name consists of |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getParts(): array |
53
|
|
|
{ |
54
|
|
|
return $this->parts; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getAll(): array |
61
|
|
|
{ |
62
|
|
|
$results = []; |
63
|
|
|
$keys = [ |
64
|
|
|
'salutation', |
65
|
|
|
'firstname', |
66
|
|
|
'middlename', |
67
|
|
|
'lastname', |
68
|
|
|
'nickname', |
69
|
|
|
'initials', |
70
|
|
|
'suffix' |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
foreach ($keys as $key) { |
74
|
|
|
$method = sprintf('get%s', ucfirst($key)); |
75
|
|
|
if ($value = call_user_func(array($this, $method))) { |
76
|
|
|
$results[$key] = $value; |
77
|
|
|
}; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $results; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* get the first name |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getFirstname(): string |
89
|
|
|
{ |
90
|
|
|
return $this->export('Firstname'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* get the last name |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getLastname(): string |
99
|
|
|
{ |
100
|
|
|
return $this->export('Lastname'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* get the initials |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getInitials(): string |
109
|
|
|
{ |
110
|
|
|
return $this->export('Initial'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* get the suffix(es) |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getSuffix(): string |
119
|
|
|
{ |
120
|
|
|
return $this->export('Suffix'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* get the salutation(s) |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getSalutation(): string |
129
|
|
|
{ |
130
|
|
|
return $this->export('Salutation'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* get the nick name(s) |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getNickname(): string |
139
|
|
|
{ |
140
|
|
|
return $this->export('Nickname'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* get the middle name(s) |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getMiddlename(): string |
149
|
|
|
{ |
150
|
|
|
return $this->export('Middlename'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* helper method used by getters to extract and format relevant name parts |
155
|
|
|
* |
156
|
|
|
* @param string $type the part type to export |
157
|
|
|
* @return string the exported parts |
158
|
|
|
*/ |
159
|
|
|
protected function export($type): string |
160
|
|
|
{ |
161
|
|
|
$matched = []; |
162
|
|
|
|
163
|
|
|
foreach ($this->parts as $part) { |
164
|
|
|
if ($part instanceof AbstractPart && is_a($part, 'TheIconic\\NameParser\\Part\\' . $type)) { |
165
|
|
|
$matched[] = $part->normalize(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return implode(' ', $matched); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|