1 | <?php |
||
13 | class LastnameMapper extends AbstractMapper |
||
14 | { |
||
15 | protected $prefixes = []; |
||
16 | |||
17 | protected $matchSinglePart = false; |
||
18 | |||
19 | public function __construct(array $prefixes, bool $matchSinglePart = false) |
||
24 | |||
25 | /** |
||
26 | * map lastnames in the parts array |
||
27 | * |
||
28 | * @param array $parts the name parts |
||
29 | * @return array the mapped parts |
||
30 | */ |
||
31 | public function map(array $parts): array |
||
39 | |||
40 | /** |
||
41 | * we map the parts in reverse order because it makes more |
||
42 | * sense to parse for the lastname starting from the end |
||
43 | * |
||
44 | * @param array $parts |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function mapParts(array $parts): array |
||
80 | |||
81 | /** |
||
82 | * try to map this part as a lastname prefix or as a combined |
||
83 | * lastname part containing a prefix |
||
84 | * |
||
85 | * @param array $parts |
||
86 | * @param int $k |
||
87 | * @return Lastname|null |
||
88 | */ |
||
89 | private function mapAsPrefixIfPossible(array $parts, int $k): ?Lastname |
||
101 | |||
102 | /** |
||
103 | * check if the given part is a combined lastname part |
||
104 | * that ends in a lastname prefix |
||
105 | * |
||
106 | * @param string $part |
||
107 | * @return bool |
||
108 | */ |
||
109 | private function isCombinedWithPrefix(string $part): bool |
||
119 | |||
120 | /** |
||
121 | * skip through the parts we want to ignore and return the start index |
||
122 | * |
||
123 | * @param array $parts |
||
124 | * @return int |
||
125 | */ |
||
126 | protected function skipIgnoredParts(array $parts): int |
||
138 | |||
139 | /** |
||
140 | * indicates if we should stop mapping at the given index $k |
||
141 | * |
||
142 | * the assumption is that lastname parts have already been found |
||
143 | * but we want to see if we should add more parts |
||
144 | * |
||
145 | * @param array $parts |
||
146 | * @param int $k |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function shouldStopMapping(array $parts, int $k): bool |
||
165 | |||
166 | /** |
||
167 | * indicates if the given part should be ignored (skipped) during mapping |
||
168 | * |
||
169 | * @param $part |
||
170 | * @return bool |
||
171 | */ |
||
172 | protected function isIgnoredPart($part) { |
||
175 | |||
176 | /** |
||
177 | * remap ignored parts as lastname |
||
178 | * |
||
179 | * if the mapping did not derive any lastname this is called to transform |
||
180 | * any previously ignored parts into lastname parts |
||
181 | * |
||
182 | * @param array $parts |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function remapIgnored(array $parts): array |
||
201 | |||
202 | /** |
||
203 | * @param array $parts |
||
204 | * @param int $index |
||
205 | * @return bool |
||
206 | */ |
||
207 | protected function isFollowedByLastnamePart(array $parts, int $index): bool |
||
213 | |||
214 | /** |
||
215 | * Assuming that the part at the given index is matched as a prefix, |
||
216 | * determines if the prefix should be applied to the lastname. |
||
217 | * |
||
218 | * We only apply it to the lastname if we already have at least one |
||
219 | * lastname part and there are other parts left in |
||
220 | * the name (this effectively prioritises firstname over prefix matching). |
||
221 | * |
||
222 | * This expects the parts array and index to be in the original order. |
||
223 | * |
||
224 | * @param array $parts |
||
225 | * @param int $index |
||
226 | * @return bool |
||
227 | */ |
||
228 | protected function isApplicablePrefix(array $parts, int $index): bool |
||
236 | |||
237 | /** |
||
238 | * check if the given word is a lastname prefix |
||
239 | * |
||
240 | * @param string $word the word to check |
||
241 | * @return bool |
||
242 | */ |
||
243 | protected function isPrefix($word): bool |
||
247 | |||
248 | /** |
||
249 | * find the next non-nickname index in parts |
||
250 | * |
||
251 | * @param $parts |
||
252 | * @param $startIndex |
||
253 | * @return int|void |
||
254 | */ |
||
255 | protected function skipNicknameParts($parts, $startIndex) |
||
267 | } |
||
268 |