@@ -13,35 +13,35 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class PromoteSingleStrings extends AbstractPass |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - protected function runPass(array $strings) |
|
| 20 | - { |
|
| 21 | - return array_map([$this, 'promoteSingleStrings'], $strings); |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + protected function runPass(array $strings) |
|
| 20 | + { |
|
| 21 | + return array_map([$this, 'promoteSingleStrings'], $strings); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Promote single strings found inside given string |
|
| 26 | - * |
|
| 27 | - * @param array $string Original string |
|
| 28 | - * @return array Modified string |
|
| 29 | - */ |
|
| 30 | - protected function promoteSingleStrings(array $string) |
|
| 31 | - { |
|
| 32 | - $newString = []; |
|
| 33 | - foreach ($string as $element) |
|
| 34 | - { |
|
| 35 | - if (is_array($element) && count($element) === 1) |
|
| 36 | - { |
|
| 37 | - $newString = array_merge($newString, $element[0]); |
|
| 38 | - } |
|
| 39 | - else |
|
| 40 | - { |
|
| 41 | - $newString[] = $element; |
|
| 42 | - } |
|
| 43 | - } |
|
| 24 | + /** |
|
| 25 | + * Promote single strings found inside given string |
|
| 26 | + * |
|
| 27 | + * @param array $string Original string |
|
| 28 | + * @return array Modified string |
|
| 29 | + */ |
|
| 30 | + protected function promoteSingleStrings(array $string) |
|
| 31 | + { |
|
| 32 | + $newString = []; |
|
| 33 | + foreach ($string as $element) |
|
| 34 | + { |
|
| 35 | + if (is_array($element) && count($element) === 1) |
|
| 36 | + { |
|
| 37 | + $newString = array_merge($newString, $element[0]); |
|
| 38 | + } |
|
| 39 | + else |
|
| 40 | + { |
|
| 41 | + $newString[] = $element; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - return $newString; |
|
| 46 | - } |
|
| 45 | + return $newString; |
|
| 46 | + } |
|
| 47 | 47 | } |
| 48 | 48 | \ No newline at end of file |
@@ -9,75 +9,75 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class AbstractPass implements PassInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var bool Whether the current set of strings is optional |
|
| 14 | - */ |
|
| 15 | - protected $isOptional; |
|
| 12 | + /** |
|
| 13 | + * @var bool Whether the current set of strings is optional |
|
| 14 | + */ |
|
| 15 | + protected $isOptional; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function run(array $strings) |
|
| 21 | - { |
|
| 22 | - $strings = $this->beforeRun($strings); |
|
| 23 | - if ($this->canRun($strings)) |
|
| 24 | - { |
|
| 25 | - $strings = $this->runPass($strings); |
|
| 26 | - } |
|
| 27 | - $strings = $this->afterRun($strings); |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function run(array $strings) |
|
| 21 | + { |
|
| 22 | + $strings = $this->beforeRun($strings); |
|
| 23 | + if ($this->canRun($strings)) |
|
| 24 | + { |
|
| 25 | + $strings = $this->runPass($strings); |
|
| 26 | + } |
|
| 27 | + $strings = $this->afterRun($strings); |
|
| 28 | 28 | |
| 29 | - return $strings; |
|
| 30 | - } |
|
| 29 | + return $strings; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Process the list of strings after the pass is run |
|
| 34 | - * |
|
| 35 | - * @param array[] $strings |
|
| 36 | - * @return array[] |
|
| 37 | - */ |
|
| 38 | - protected function afterRun(array $strings) |
|
| 39 | - { |
|
| 40 | - if ($this->isOptional && $strings[0] !== []) |
|
| 41 | - { |
|
| 42 | - array_unshift($strings, []); |
|
| 43 | - } |
|
| 32 | + /** |
|
| 33 | + * Process the list of strings after the pass is run |
|
| 34 | + * |
|
| 35 | + * @param array[] $strings |
|
| 36 | + * @return array[] |
|
| 37 | + */ |
|
| 38 | + protected function afterRun(array $strings) |
|
| 39 | + { |
|
| 40 | + if ($this->isOptional && $strings[0] !== []) |
|
| 41 | + { |
|
| 42 | + array_unshift($strings, []); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - return $strings; |
|
| 46 | - } |
|
| 45 | + return $strings; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Prepare the list of strings before the pass is run |
|
| 50 | - * |
|
| 51 | - * @param array[] $strings |
|
| 52 | - * @return array[] |
|
| 53 | - */ |
|
| 54 | - protected function beforeRun(array $strings) |
|
| 55 | - { |
|
| 56 | - $this->isOptional = (isset($strings[0]) && $strings[0] === []); |
|
| 57 | - if ($this->isOptional) |
|
| 58 | - { |
|
| 59 | - array_shift($strings); |
|
| 60 | - } |
|
| 48 | + /** |
|
| 49 | + * Prepare the list of strings before the pass is run |
|
| 50 | + * |
|
| 51 | + * @param array[] $strings |
|
| 52 | + * @return array[] |
|
| 53 | + */ |
|
| 54 | + protected function beforeRun(array $strings) |
|
| 55 | + { |
|
| 56 | + $this->isOptional = (isset($strings[0]) && $strings[0] === []); |
|
| 57 | + if ($this->isOptional) |
|
| 58 | + { |
|
| 59 | + array_shift($strings); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $strings; |
|
| 63 | - } |
|
| 62 | + return $strings; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Test whether this pass can be run on a given list of strings |
|
| 67 | - * |
|
| 68 | - * @param array[] $strings |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 71 | - protected function canRun(array $strings) |
|
| 72 | - { |
|
| 73 | - return true; |
|
| 74 | - } |
|
| 65 | + /** |
|
| 66 | + * Test whether this pass can be run on a given list of strings |
|
| 67 | + * |
|
| 68 | + * @param array[] $strings |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | + protected function canRun(array $strings) |
|
| 72 | + { |
|
| 73 | + return true; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Run this pass on a list of strings |
|
| 78 | - * |
|
| 79 | - * @param array[] $strings |
|
| 80 | - * @return array[] |
|
| 81 | - */ |
|
| 82 | - abstract protected function runPass(array $strings); |
|
| 76 | + /** |
|
| 77 | + * Run this pass on a list of strings |
|
| 78 | + * |
|
| 79 | + * @param array[] $strings |
|
| 80 | + * @return array[] |
|
| 81 | + */ |
|
| 82 | + abstract protected function runPass(array $strings); |
|
| 83 | 83 | } |
| 84 | 84 | \ No newline at end of file |
@@ -12,67 +12,67 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class CoalesceSingleCharacterPrefix extends AbstractPass |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected function runPass(array $strings) |
|
| 19 | - { |
|
| 20 | - $newStrings = []; |
|
| 21 | - foreach ($this->getEligibleKeys($strings) as $keys) |
|
| 22 | - { |
|
| 23 | - // Create a new string to hold the merged strings and replace the first element with |
|
| 24 | - // an empty character class |
|
| 25 | - $newString = $strings[$keys[0]]; |
|
| 26 | - $newString[0] = []; |
|
| 27 | - foreach ($keys as $key) |
|
| 28 | - { |
|
| 29 | - $newString[0][] = [$strings[$key][0]]; |
|
| 30 | - unset($strings[$key]); |
|
| 31 | - } |
|
| 32 | - $newStrings[] = $newString; |
|
| 33 | - } |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected function runPass(array $strings) |
|
| 19 | + { |
|
| 20 | + $newStrings = []; |
|
| 21 | + foreach ($this->getEligibleKeys($strings) as $keys) |
|
| 22 | + { |
|
| 23 | + // Create a new string to hold the merged strings and replace the first element with |
|
| 24 | + // an empty character class |
|
| 25 | + $newString = $strings[$keys[0]]; |
|
| 26 | + $newString[0] = []; |
|
| 27 | + foreach ($keys as $key) |
|
| 28 | + { |
|
| 29 | + $newString[0][] = [$strings[$key][0]]; |
|
| 30 | + unset($strings[$key]); |
|
| 31 | + } |
|
| 32 | + $newStrings[] = $newString; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - return array_merge($newStrings, $strings); |
|
| 36 | - } |
|
| 35 | + return array_merge($newStrings, $strings); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Filter the list of eligible keys and keep those that have at least two matches |
|
| 40 | - * |
|
| 41 | - * @param array[] $eligibleKeys List of lists of keys |
|
| 42 | - * @return array[] |
|
| 43 | - */ |
|
| 44 | - protected function filterEligibleKeys(array $eligibleKeys) |
|
| 45 | - { |
|
| 46 | - $filteredKeys = []; |
|
| 47 | - foreach ($eligibleKeys as $k => $keys) |
|
| 48 | - { |
|
| 49 | - if (count($keys) > 1) |
|
| 50 | - { |
|
| 51 | - $filteredKeys[] = $keys; |
|
| 52 | - } |
|
| 53 | - } |
|
| 38 | + /** |
|
| 39 | + * Filter the list of eligible keys and keep those that have at least two matches |
|
| 40 | + * |
|
| 41 | + * @param array[] $eligibleKeys List of lists of keys |
|
| 42 | + * @return array[] |
|
| 43 | + */ |
|
| 44 | + protected function filterEligibleKeys(array $eligibleKeys) |
|
| 45 | + { |
|
| 46 | + $filteredKeys = []; |
|
| 47 | + foreach ($eligibleKeys as $k => $keys) |
|
| 48 | + { |
|
| 49 | + if (count($keys) > 1) |
|
| 50 | + { |
|
| 51 | + $filteredKeys[] = $keys; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - return $filteredKeys; |
|
| 56 | - } |
|
| 55 | + return $filteredKeys; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get a list of keys of strings eligible to be merged together, grouped by suffix |
|
| 60 | - * |
|
| 61 | - * @param array[] $strings |
|
| 62 | - * @return array[] |
|
| 63 | - */ |
|
| 64 | - protected function getEligibleKeys(array $strings) |
|
| 65 | - { |
|
| 66 | - $eligibleKeys = []; |
|
| 67 | - foreach ($strings as $k => $string) |
|
| 68 | - { |
|
| 69 | - if (!is_array($string[0]) && isset($string[1])) |
|
| 70 | - { |
|
| 71 | - $suffix = serialize(array_slice($string, 1)); |
|
| 72 | - $eligibleKeys[$suffix][] = $k; |
|
| 73 | - } |
|
| 74 | - } |
|
| 58 | + /** |
|
| 59 | + * Get a list of keys of strings eligible to be merged together, grouped by suffix |
|
| 60 | + * |
|
| 61 | + * @param array[] $strings |
|
| 62 | + * @return array[] |
|
| 63 | + */ |
|
| 64 | + protected function getEligibleKeys(array $strings) |
|
| 65 | + { |
|
| 66 | + $eligibleKeys = []; |
|
| 67 | + foreach ($strings as $k => $string) |
|
| 68 | + { |
|
| 69 | + if (!is_array($string[0]) && isset($string[1])) |
|
| 70 | + { |
|
| 71 | + $suffix = serialize(array_slice($string, 1)); |
|
| 72 | + $eligibleKeys[$suffix][] = $k; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return $this->filterEligibleKeys($eligibleKeys); |
|
| 77 | - } |
|
| 76 | + return $this->filterEligibleKeys($eligibleKeys); |
|
| 77 | + } |
|
| 78 | 78 | } |
| 79 | 79 | \ No newline at end of file |
@@ -14,45 +14,45 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Recurse extends AbstractPass |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * @var Runner |
|
| 19 | - */ |
|
| 20 | - protected $runner; |
|
| 17 | + /** |
|
| 18 | + * @var Runner |
|
| 19 | + */ |
|
| 20 | + protected $runner; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param Runner $runner |
|
| 24 | - */ |
|
| 25 | - public function __construct(Runner $runner) |
|
| 26 | - { |
|
| 27 | - $this->runner = $runner; |
|
| 28 | - } |
|
| 22 | + /** |
|
| 23 | + * @param Runner $runner |
|
| 24 | + */ |
|
| 25 | + public function __construct(Runner $runner) |
|
| 26 | + { |
|
| 27 | + $this->runner = $runner; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - protected function runPass(array $strings) |
|
| 34 | - { |
|
| 35 | - return array_map([$this, 'recurseString'], $strings); |
|
| 36 | - } |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + protected function runPass(array $strings) |
|
| 34 | + { |
|
| 35 | + return array_map([$this, 'recurseString'], $strings); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Recurse into given string and run all passes on each element |
|
| 40 | - * |
|
| 41 | - * @param array $string |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - protected function recurseString(array $string) |
|
| 45 | - { |
|
| 46 | - $isOptional = $this->isOptional; |
|
| 47 | - foreach ($string as $k => $element) |
|
| 48 | - { |
|
| 49 | - if (is_array($element)) |
|
| 50 | - { |
|
| 51 | - $string[$k] = $this->runner->run($element); |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - $this->isOptional = $isOptional; |
|
| 38 | + /** |
|
| 39 | + * Recurse into given string and run all passes on each element |
|
| 40 | + * |
|
| 41 | + * @param array $string |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + protected function recurseString(array $string) |
|
| 45 | + { |
|
| 46 | + $isOptional = $this->isOptional; |
|
| 47 | + foreach ($string as $k => $element) |
|
| 48 | + { |
|
| 49 | + if (is_array($element)) |
|
| 50 | + { |
|
| 51 | + $string[$k] = $this->runner->run($element); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + $this->isOptional = $isOptional; |
|
| 55 | 55 | |
| 56 | - return $string; |
|
| 57 | - } |
|
| 56 | + return $string; |
|
| 57 | + } |
|
| 58 | 58 | } |
| 59 | 59 | \ No newline at end of file |
@@ -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 |