@@ -12,72 +12,72 @@ |
||
12 | 12 | */ |
13 | 13 | class MergeSuffix extends AbstractPass |
14 | 14 | { |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected function canRun(array $strings) |
|
19 | - { |
|
20 | - return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
21 | - } |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected function canRun(array $strings) |
|
19 | + { |
|
20 | + return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * {@inheritdoc} |
|
25 | - */ |
|
26 | - protected function runPass(array $strings) |
|
27 | - { |
|
28 | - $newString = []; |
|
29 | - while ($this->hasMatchingSuffix($strings)) |
|
30 | - { |
|
31 | - array_unshift($newString, end($strings[0])); |
|
32 | - $strings = $this->pop($strings); |
|
33 | - } |
|
34 | - array_unshift($newString, $strings); |
|
23 | + /** |
|
24 | + * {@inheritdoc} |
|
25 | + */ |
|
26 | + protected function runPass(array $strings) |
|
27 | + { |
|
28 | + $newString = []; |
|
29 | + while ($this->hasMatchingSuffix($strings)) |
|
30 | + { |
|
31 | + array_unshift($newString, end($strings[0])); |
|
32 | + $strings = $this->pop($strings); |
|
33 | + } |
|
34 | + array_unshift($newString, $strings); |
|
35 | 35 | |
36 | - return [$newString]; |
|
37 | - } |
|
36 | + return [$newString]; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Test whether all given strings have the same last element |
|
41 | - * |
|
42 | - * @param array[] $strings |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - protected function hasMatchingSuffix(array $strings) |
|
46 | - { |
|
47 | - $suffix = end($strings[1]); |
|
48 | - foreach ($strings as $string) |
|
49 | - { |
|
50 | - if (end($string) !== $suffix) |
|
51 | - { |
|
52 | - return false; |
|
53 | - } |
|
54 | - } |
|
39 | + /** |
|
40 | + * Test whether all given strings have the same last element |
|
41 | + * |
|
42 | + * @param array[] $strings |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + protected function hasMatchingSuffix(array $strings) |
|
46 | + { |
|
47 | + $suffix = end($strings[1]); |
|
48 | + foreach ($strings as $string) |
|
49 | + { |
|
50 | + if (end($string) !== $suffix) |
|
51 | + { |
|
52 | + return false; |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - return ($suffix !== false); |
|
57 | - } |
|
56 | + return ($suffix !== false); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Remove the last element of every string |
|
61 | - * |
|
62 | - * @param array[] $strings Original strings |
|
63 | - * @return array[] Processed strings |
|
64 | - */ |
|
65 | - protected function pop(array $strings) |
|
66 | - { |
|
67 | - $cnt = count($strings); |
|
68 | - $i = $cnt; |
|
69 | - while (--$i >= 0) |
|
70 | - { |
|
71 | - array_pop($strings[$i]); |
|
72 | - } |
|
59 | + /** |
|
60 | + * Remove the last element of every string |
|
61 | + * |
|
62 | + * @param array[] $strings Original strings |
|
63 | + * @return array[] Processed strings |
|
64 | + */ |
|
65 | + protected function pop(array $strings) |
|
66 | + { |
|
67 | + $cnt = count($strings); |
|
68 | + $i = $cnt; |
|
69 | + while (--$i >= 0) |
|
70 | + { |
|
71 | + array_pop($strings[$i]); |
|
72 | + } |
|
73 | 73 | |
74 | - // Remove empty elements then prepend one back at the start of the array if applicable |
|
75 | - $strings = array_filter($strings); |
|
76 | - if (count($strings) < $cnt) |
|
77 | - { |
|
78 | - array_unshift($strings, []); |
|
79 | - } |
|
74 | + // Remove empty elements then prepend one back at the start of the array if applicable |
|
75 | + $strings = array_filter($strings); |
|
76 | + if (count($strings) < $cnt) |
|
77 | + { |
|
78 | + array_unshift($strings, []); |
|
79 | + } |
|
80 | 80 | |
81 | - return $strings; |
|
82 | - } |
|
81 | + return $strings; |
|
82 | + } |
|
83 | 83 | } |
84 | 84 | \ No newline at end of file |
@@ -12,40 +12,40 @@ |
||
12 | 12 | */ |
13 | 13 | class GroupSingleCharacters extends AbstractPass |
14 | 14 | { |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected function runPass(array $strings) |
|
19 | - { |
|
20 | - $singles = $this->getSingleCharStrings($strings); |
|
21 | - $cnt = count($singles); |
|
22 | - if ($cnt > 1 && $cnt < count($strings)) |
|
23 | - { |
|
24 | - // Remove the singles from the input, then prepend them as a new string |
|
25 | - $strings = array_diff_key($strings, $singles); |
|
26 | - array_unshift($strings, [array_values($singles)]); |
|
27 | - } |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected function runPass(array $strings) |
|
19 | + { |
|
20 | + $singles = $this->getSingleCharStrings($strings); |
|
21 | + $cnt = count($singles); |
|
22 | + if ($cnt > 1 && $cnt < count($strings)) |
|
23 | + { |
|
24 | + // Remove the singles from the input, then prepend them as a new string |
|
25 | + $strings = array_diff_key($strings, $singles); |
|
26 | + array_unshift($strings, [array_values($singles)]); |
|
27 | + } |
|
28 | 28 | |
29 | - return $strings; |
|
30 | - } |
|
29 | + return $strings; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Return an array of every single-char string in given list of strings |
|
34 | - * |
|
35 | - * @param array[] $strings |
|
36 | - * @return array[] |
|
37 | - */ |
|
38 | - protected function getSingleCharStrings(array $strings) |
|
39 | - { |
|
40 | - $singles = []; |
|
41 | - foreach ($strings as $k => $string) |
|
42 | - { |
|
43 | - if (count($string) === 1 && !is_array($string[0])) |
|
44 | - { |
|
45 | - $singles[$k] = $string; |
|
46 | - } |
|
47 | - } |
|
32 | + /** |
|
33 | + * Return an array of every single-char string in given list of strings |
|
34 | + * |
|
35 | + * @param array[] $strings |
|
36 | + * @return array[] |
|
37 | + */ |
|
38 | + protected function getSingleCharStrings(array $strings) |
|
39 | + { |
|
40 | + $singles = []; |
|
41 | + foreach ($strings as $k => $string) |
|
42 | + { |
|
43 | + if (count($string) === 1 && !is_array($string[0])) |
|
44 | + { |
|
45 | + $singles[$k] = $string; |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - return $singles; |
|
50 | - } |
|
49 | + return $singles; |
|
50 | + } |
|
51 | 51 | } |
52 | 52 | \ No newline at end of file |
@@ -12,93 +12,93 @@ |
||
12 | 12 | */ |
13 | 13 | class MergePrefix extends AbstractPass |
14 | 14 | { |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected function runPass(array $strings) |
|
19 | - { |
|
20 | - $newStrings = []; |
|
21 | - foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
|
22 | - { |
|
23 | - $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
|
24 | - } |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected function runPass(array $strings) |
|
19 | + { |
|
20 | + $newStrings = []; |
|
21 | + foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
|
22 | + { |
|
23 | + $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
|
24 | + } |
|
25 | 25 | |
26 | - return $newStrings; |
|
27 | - } |
|
26 | + return $newStrings; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Get the number of leading elements common to all given strings |
|
31 | - * |
|
32 | - * @param array[] $strings |
|
33 | - * @return integer |
|
34 | - */ |
|
35 | - protected function getPrefixLength(array $strings) |
|
36 | - { |
|
37 | - $len = 1; |
|
38 | - $cnt = count($strings[0]); |
|
39 | - while ($len < $cnt && $this->stringsMatch($strings, $len)) |
|
40 | - { |
|
41 | - ++$len; |
|
42 | - } |
|
29 | + /** |
|
30 | + * Get the number of leading elements common to all given strings |
|
31 | + * |
|
32 | + * @param array[] $strings |
|
33 | + * @return integer |
|
34 | + */ |
|
35 | + protected function getPrefixLength(array $strings) |
|
36 | + { |
|
37 | + $len = 1; |
|
38 | + $cnt = count($strings[0]); |
|
39 | + while ($len < $cnt && $this->stringsMatch($strings, $len)) |
|
40 | + { |
|
41 | + ++$len; |
|
42 | + } |
|
43 | 43 | |
44 | - return $len; |
|
45 | - } |
|
44 | + return $len; |
|
45 | + } |
|
46 | 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 | - protected function getStringsByPrefix(array $strings) |
|
56 | - { |
|
57 | - $byPrefix = []; |
|
58 | - foreach ($strings as $string) |
|
59 | - { |
|
60 | - $byPrefix[$string[0]][] = $string; |
|
61 | - } |
|
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 | + protected function getStringsByPrefix(array $strings) |
|
56 | + { |
|
57 | + $byPrefix = []; |
|
58 | + foreach ($strings as $string) |
|
59 | + { |
|
60 | + $byPrefix[$string[0]][] = $string; |
|
61 | + } |
|
62 | 62 | |
63 | - return $byPrefix; |
|
64 | - } |
|
63 | + return $byPrefix; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Merge given strings into a new single string |
|
68 | - * |
|
69 | - * @param array[] $strings |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - protected function mergeStrings(array $strings) |
|
73 | - { |
|
74 | - $len = $this->getPrefixLength($strings); |
|
75 | - $newString = array_slice($strings[0], 0, $len); |
|
76 | - foreach ($strings as $string) |
|
77 | - { |
|
78 | - $newString[$len][] = array_slice($string, $len); |
|
79 | - } |
|
66 | + /** |
|
67 | + * Merge given strings into a new single string |
|
68 | + * |
|
69 | + * @param array[] $strings |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + protected function mergeStrings(array $strings) |
|
73 | + { |
|
74 | + $len = $this->getPrefixLength($strings); |
|
75 | + $newString = array_slice($strings[0], 0, $len); |
|
76 | + foreach ($strings as $string) |
|
77 | + { |
|
78 | + $newString[$len][] = array_slice($string, $len); |
|
79 | + } |
|
80 | 80 | |
81 | - return $newString; |
|
82 | - } |
|
81 | + return $newString; |
|
82 | + } |
|
83 | 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 | - protected function stringsMatch(array $strings, $pos) |
|
92 | - { |
|
93 | - $value = $strings[0][$pos]; |
|
94 | - foreach ($strings as $string) |
|
95 | - { |
|
96 | - if (!isset($string[$pos]) || $string[$pos] !== $value) |
|
97 | - { |
|
98 | - return false; |
|
99 | - } |
|
100 | - } |
|
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 | + protected function stringsMatch(array $strings, $pos) |
|
92 | + { |
|
93 | + $value = $strings[0][$pos]; |
|
94 | + foreach ($strings as $string) |
|
95 | + { |
|
96 | + if (!isset($string[$pos]) || $string[$pos] !== $value) |
|
97 | + { |
|
98 | + return false; |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - return true; |
|
103 | - } |
|
102 | + return true; |
|
103 | + } |
|
104 | 104 | } |
105 | 105 | \ No newline at end of file |