1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\NameParser; |
4
|
|
|
|
5
|
|
|
use TheIconic\NameParser\Mapper\NicknameMapper; |
6
|
|
|
use TheIconic\NameParser\Mapper\SalutationMapper; |
7
|
|
|
use TheIconic\NameParser\Mapper\SuffixMapper; |
8
|
|
|
use TheIconic\NameParser\Mapper\InitialMapper; |
9
|
|
|
use TheIconic\NameParser\Mapper\LastnameMapper; |
10
|
|
|
use TheIconic\NameParser\Mapper\FirstnameMapper; |
11
|
|
|
use TheIconic\NameParser\Mapper\MiddlenameMapper; |
12
|
|
|
|
13
|
|
|
class Parser |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $whitespace = " \r\n\t"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $mappers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* split full names into the following parts: |
27
|
|
|
* - prefix / salutation (Mr., Mrs., etc) |
28
|
|
|
* - given name / first name |
29
|
|
|
* - middle initials |
30
|
|
|
* - surname / last name |
31
|
|
|
* - suffix (II, Phd, Jr, etc) |
32
|
|
|
* |
33
|
|
|
* @param string $name |
34
|
|
|
* @return Name |
35
|
|
|
*/ |
36
|
|
|
public function parse($name): Name |
37
|
|
|
{ |
38
|
|
|
$name = $this->normalize($name); |
39
|
|
|
|
40
|
|
|
if (false !== $pos = strpos($name, ',')) { |
41
|
|
|
return $this->parseSplitName(substr($name, 0, $pos), substr($name, $pos + 1)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$parts = explode(' ', $name); |
45
|
|
|
|
46
|
|
|
foreach ($this->getMappers() as $mapper) { |
47
|
|
|
$parts = $mapper->map($parts); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return new Name($parts); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* handles split-parsing of comma-separated name parts |
55
|
|
|
* |
56
|
|
|
* @param $left - the name part left of the comma |
57
|
|
|
* @param $right - the name part right of the comma |
58
|
|
|
* |
59
|
|
|
* @return Name |
60
|
|
|
*/ |
61
|
|
|
protected function parseSplitName($left, $right) |
62
|
|
|
{ |
63
|
|
|
$parts = array_merge( |
64
|
|
|
$this->getLeftSplitNameParser()->parse($left)->getParts(), |
65
|
|
|
$this->getRightSplitNameParser()->parse($right)->getParts() |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return new Name($parts); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Parser |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
protected function getLeftSplitNameParser() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$parser = new Parser(); |
77
|
|
|
$parser->setMappers([ |
78
|
|
|
new SalutationMapper(), |
79
|
|
|
new SuffixMapper(), |
80
|
|
|
new LastnameMapper(['match_single' => true]), |
81
|
|
|
new FirstnameMapper(), |
82
|
|
|
new MiddlenameMapper(), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
return $parser; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Parser |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
protected function getRightSplitNameParser() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$parser = new Parser(); |
94
|
|
|
$parser->setMappers([ |
95
|
|
|
new SalutationMapper(), |
96
|
|
|
new SuffixMapper(['match_single' => true]), |
97
|
|
|
new NicknameMapper(), |
98
|
|
|
new InitialMapper(), |
99
|
|
|
new FirstnameMapper(), |
100
|
|
|
new MiddlenameMapper(), |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
return $parser; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* get the mappers for this parser |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getMappers() |
112
|
|
|
{ |
113
|
|
|
if (empty($this->mappers)) { |
114
|
|
|
$this->setMappers([ |
115
|
|
|
new NicknameMapper(), |
116
|
|
|
new SalutationMapper(), |
117
|
|
|
new SuffixMapper(), |
118
|
|
|
new InitialMapper(), |
119
|
|
|
new LastnameMapper(), |
120
|
|
|
new FirstnameMapper(), |
121
|
|
|
new MiddlenameMapper(), |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->mappers; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* set the mappers for this parser |
130
|
|
|
* |
131
|
|
|
* @param array $mappers |
132
|
|
|
*/ |
133
|
|
|
public function setMappers(array $mappers) |
134
|
|
|
{ |
135
|
|
|
$this->mappers = $mappers; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* normalize the name |
140
|
|
|
* |
141
|
|
|
* @param $name |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
protected function normalize($name) |
145
|
|
|
{ |
146
|
|
|
$whitespace = $this->getWhitespace(); |
147
|
|
|
|
148
|
|
|
$name = trim($name); |
149
|
|
|
|
150
|
|
|
return preg_replace('/[' . preg_quote($whitespace) . ']+/', ' ', $name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* get a string of characters that are supposed to be treated as whitespace |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getWhitespace() |
159
|
|
|
{ |
160
|
|
|
return $this->whitespace; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* set the string of characters that are supposed to be treated as whitespace |
165
|
|
|
* |
166
|
|
|
* @param $whitespace |
167
|
|
|
* @return Parser |
168
|
|
|
*/ |
169
|
|
|
public function setWhitespace($whitespace): Parser |
170
|
|
|
{ |
171
|
|
|
$this->whitespace = $whitespace; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.