1 | <?php |
||
13 | class MergePrefix extends AbstractPass |
||
14 | { |
||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | 5 | protected function runPass(array $strings) |
|
19 | { |
||
20 | 5 | $newStrings = []; |
|
21 | 5 | foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
|
22 | { |
||
23 | 4 | $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
|
24 | } |
||
25 | |||
26 | 5 | return $newStrings; |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * Get the number of leading elements common to all given strings |
||
31 | * |
||
32 | * @param array[] $strings |
||
33 | * @return integer |
||
34 | */ |
||
35 | 4 | protected function getPrefixLength(array $strings) |
|
46 | |||
47 | /** |
||
48 | * Return given strings grouped by their first element |
||
49 | * |
||
50 | * NOTE: assumes that this pass is run before the first element of any string could be replaced |
||
51 | * |
||
52 | * @param array[] $strings |
||
53 | * @return array[] |
||
54 | */ |
||
55 | 5 | protected function getStringsByPrefix(array $strings) |
|
65 | |||
66 | /** |
||
67 | * Merge given strings into a new single string |
||
68 | * |
||
69 | * @param array[] $strings |
||
70 | * @return array |
||
71 | */ |
||
72 | 4 | protected function mergeStrings(array $strings) |
|
83 | |||
84 | /** |
||
85 | * Test whether all given strings' elements match at given position |
||
86 | * |
||
87 | * @param array[] $strings |
||
88 | * @param integer $pos |
||
89 | * @return bool |
||
90 | */ |
||
91 | 3 | protected function stringsMatch(array $strings, $pos) |
|
104 | } |