1 | <?php |
||
8 | class SalutationMapper extends AbstractMapper |
||
9 | { |
||
10 | protected $salutations = []; |
||
11 | |||
12 | protected $maxIndex = 0; |
||
13 | |||
14 | public function __construct(array $salutations, $maxIndex = 0) |
||
19 | |||
20 | /** |
||
21 | * map salutations in the parts array |
||
22 | * |
||
23 | * @param array $parts the name parts |
||
24 | * @return array the mapped parts |
||
25 | */ |
||
26 | public function map(array $parts): array |
||
40 | |||
41 | /** |
||
42 | * We pass the full parts array and the current position to allow |
||
43 | * not only single-word matches but also combined matches with |
||
44 | * subsequent words (parts). |
||
45 | * |
||
46 | * @param array $parts |
||
47 | * @param int $start |
||
48 | * @return array |
||
49 | */ |
||
50 | protected function substituteWithSalutation(array $parts, int $start): array |
||
71 | |||
72 | /** |
||
73 | * check if the given subset matches the given keys entry by entry, |
||
74 | * which means word by word, except that we first need to key-ify |
||
75 | * the subset words |
||
76 | * |
||
77 | * @param array $keys |
||
78 | * @param array $subset |
||
79 | * @return bool |
||
80 | */ |
||
81 | private function isMatchingSubset(array $keys, array $subset): bool |
||
91 | |||
92 | /** |
||
93 | * check if the given word is a viable salutation |
||
94 | * |
||
95 | * @param string $word the word to check |
||
96 | * @return bool |
||
97 | */ |
||
98 | protected function isSalutation($word): bool |
||
102 | } |
||
103 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: