@@ -9,80 +9,80 @@ |
||
| 9 | 9 | |
| 10 | 10 | class MergeSuffix extends AbstractPass |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - protected function processStrings(array $strings) |
|
| 16 | - { |
|
| 17 | - if (!$this->isEligible($strings)) |
|
| 18 | - { |
|
| 19 | - return $strings; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + protected function processStrings(array $strings) |
|
| 16 | + { |
|
| 17 | + if (!$this->isEligible($strings)) |
|
| 18 | + { |
|
| 19 | + return $strings; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - $newString = []; |
|
| 23 | - while ($this->hasMatchingSuffix($strings)) |
|
| 24 | - { |
|
| 25 | - array_unshift($newString, end($strings[0])); |
|
| 26 | - $strings = $this->pop($strings); |
|
| 27 | - } |
|
| 28 | - array_unshift($newString, $strings); |
|
| 22 | + $newString = []; |
|
| 23 | + while ($this->hasMatchingSuffix($strings)) |
|
| 24 | + { |
|
| 25 | + array_unshift($newString, end($strings[0])); |
|
| 26 | + $strings = $this->pop($strings); |
|
| 27 | + } |
|
| 28 | + array_unshift($newString, $strings); |
|
| 29 | 29 | |
| 30 | - return [$newString]; |
|
| 31 | - } |
|
| 30 | + return [$newString]; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Test whether all given strings have the same last element |
|
| 35 | - * |
|
| 36 | - * @param array[] $strings |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - protected function hasMatchingSuffix(array $strings) |
|
| 40 | - { |
|
| 41 | - $suffix = end($strings[1]); |
|
| 42 | - foreach ($strings as $string) |
|
| 43 | - { |
|
| 44 | - if (end($string) !== $suffix) |
|
| 45 | - { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - } |
|
| 33 | + /** |
|
| 34 | + * Test whether all given strings have the same last element |
|
| 35 | + * |
|
| 36 | + * @param array[] $strings |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + protected function hasMatchingSuffix(array $strings) |
|
| 40 | + { |
|
| 41 | + $suffix = end($strings[1]); |
|
| 42 | + foreach ($strings as $string) |
|
| 43 | + { |
|
| 44 | + if (end($string) !== $suffix) |
|
| 45 | + { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return ($suffix !== false); |
|
| 51 | - } |
|
| 50 | + return ($suffix !== false); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Test whether this pass can be run on given list of strings |
|
| 55 | - * |
|
| 56 | - * @param array[] $strings |
|
| 57 | - * @return bool |
|
| 58 | - */ |
|
| 59 | - protected function isEligible(array $strings) |
|
| 60 | - { |
|
| 61 | - return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
| 62 | - } |
|
| 53 | + /** |
|
| 54 | + * Test whether this pass can be run on given list of strings |
|
| 55 | + * |
|
| 56 | + * @param array[] $strings |
|
| 57 | + * @return bool |
|
| 58 | + */ |
|
| 59 | + protected function isEligible(array $strings) |
|
| 60 | + { |
|
| 61 | + return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Remove the last element of every string |
|
| 66 | - * |
|
| 67 | - * @param array[] $strings Original strings |
|
| 68 | - * @return array[] Processed strings |
|
| 69 | - */ |
|
| 70 | - protected function pop(array $strings) |
|
| 71 | - { |
|
| 72 | - $cnt = count($strings); |
|
| 73 | - $i = $cnt; |
|
| 74 | - while (--$i >= 0) |
|
| 75 | - { |
|
| 76 | - array_pop($strings[$i]); |
|
| 77 | - } |
|
| 64 | + /** |
|
| 65 | + * Remove the last element of every string |
|
| 66 | + * |
|
| 67 | + * @param array[] $strings Original strings |
|
| 68 | + * @return array[] Processed strings |
|
| 69 | + */ |
|
| 70 | + protected function pop(array $strings) |
|
| 71 | + { |
|
| 72 | + $cnt = count($strings); |
|
| 73 | + $i = $cnt; |
|
| 74 | + while (--$i >= 0) |
|
| 75 | + { |
|
| 76 | + array_pop($strings[$i]); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - // Remove empty elements then prepend one back at the start of the array if applicable |
|
| 80 | - $strings = array_filter($strings); |
|
| 81 | - if (count($strings) < $cnt) |
|
| 82 | - { |
|
| 83 | - array_unshift($strings, []); |
|
| 84 | - } |
|
| 79 | + // Remove empty elements then prepend one back at the start of the array if applicable |
|
| 80 | + $strings = array_filter($strings); |
|
| 81 | + if (count($strings) < $cnt) |
|
| 82 | + { |
|
| 83 | + array_unshift($strings, []); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - return $strings; |
|
| 87 | - } |
|
| 86 | + return $strings; |
|
| 87 | + } |
|
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -9,67 +9,67 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CoalesceSingleCharacterPrefix extends AbstractPass |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - protected function processStrings(array $strings) |
|
| 16 | - { |
|
| 17 | - $newStrings = []; |
|
| 18 | - foreach ($this->getEligibleKeys($strings) as $keys) |
|
| 19 | - { |
|
| 20 | - // Create a new string to hold the merged strings and replace the first element with |
|
| 21 | - // an empty character class |
|
| 22 | - $newString = $strings[$keys[0]]; |
|
| 23 | - $newString[0] = []; |
|
| 24 | - foreach ($keys as $key) |
|
| 25 | - { |
|
| 26 | - $newString[0][] = [$strings[$key][0]]; |
|
| 27 | - unset($strings[$key]); |
|
| 28 | - } |
|
| 29 | - $newStrings[] = $newString; |
|
| 30 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + protected function processStrings(array $strings) |
|
| 16 | + { |
|
| 17 | + $newStrings = []; |
|
| 18 | + foreach ($this->getEligibleKeys($strings) as $keys) |
|
| 19 | + { |
|
| 20 | + // Create a new string to hold the merged strings and replace the first element with |
|
| 21 | + // an empty character class |
|
| 22 | + $newString = $strings[$keys[0]]; |
|
| 23 | + $newString[0] = []; |
|
| 24 | + foreach ($keys as $key) |
|
| 25 | + { |
|
| 26 | + $newString[0][] = [$strings[$key][0]]; |
|
| 27 | + unset($strings[$key]); |
|
| 28 | + } |
|
| 29 | + $newStrings[] = $newString; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - return array_merge($newStrings, $strings); |
|
| 33 | - } |
|
| 32 | + return array_merge($newStrings, $strings); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Filter the list of eligible keys and keep those that have at least two matches |
|
| 37 | - * |
|
| 38 | - * @param array[] $eligibleKeys List of lists of keys |
|
| 39 | - * @return array[] |
|
| 40 | - */ |
|
| 41 | - protected function filterEligibleKeys(array $eligibleKeys) |
|
| 42 | - { |
|
| 43 | - $filteredKeys = []; |
|
| 44 | - foreach ($eligibleKeys as $k => $keys) |
|
| 45 | - { |
|
| 46 | - if (count($keys) > 1) |
|
| 47 | - { |
|
| 48 | - $filteredKeys[] = $keys; |
|
| 49 | - } |
|
| 50 | - } |
|
| 35 | + /** |
|
| 36 | + * Filter the list of eligible keys and keep those that have at least two matches |
|
| 37 | + * |
|
| 38 | + * @param array[] $eligibleKeys List of lists of keys |
|
| 39 | + * @return array[] |
|
| 40 | + */ |
|
| 41 | + protected function filterEligibleKeys(array $eligibleKeys) |
|
| 42 | + { |
|
| 43 | + $filteredKeys = []; |
|
| 44 | + foreach ($eligibleKeys as $k => $keys) |
|
| 45 | + { |
|
| 46 | + if (count($keys) > 1) |
|
| 47 | + { |
|
| 48 | + $filteredKeys[] = $keys; |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - return $filteredKeys; |
|
| 53 | - } |
|
| 52 | + return $filteredKeys; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get a list of keys of strings eligible to be merged together, grouped by suffix |
|
| 57 | - * |
|
| 58 | - * @param array[] $strings |
|
| 59 | - * @return array[] |
|
| 60 | - */ |
|
| 61 | - protected function getEligibleKeys(array $strings) |
|
| 62 | - { |
|
| 63 | - $eligibleKeys = []; |
|
| 64 | - foreach ($strings as $k => $string) |
|
| 65 | - { |
|
| 66 | - if (!is_array($string[0]) && isset($string[1])) |
|
| 67 | - { |
|
| 68 | - $suffix = serialize(array_slice($string, 1)); |
|
| 69 | - $eligibleKeys[$suffix][] = $k; |
|
| 70 | - } |
|
| 71 | - } |
|
| 55 | + /** |
|
| 56 | + * Get a list of keys of strings eligible to be merged together, grouped by suffix |
|
| 57 | + * |
|
| 58 | + * @param array[] $strings |
|
| 59 | + * @return array[] |
|
| 60 | + */ |
|
| 61 | + protected function getEligibleKeys(array $strings) |
|
| 62 | + { |
|
| 63 | + $eligibleKeys = []; |
|
| 64 | + foreach ($strings as $k => $string) |
|
| 65 | + { |
|
| 66 | + if (!is_array($string[0]) && isset($string[1])) |
|
| 67 | + { |
|
| 68 | + $suffix = serialize(array_slice($string, 1)); |
|
| 69 | + $eligibleKeys[$suffix][] = $k; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return $this->filterEligibleKeys($eligibleKeys); |
|
| 74 | - } |
|
| 73 | + return $this->filterEligibleKeys($eligibleKeys); |
|
| 74 | + } |
|
| 75 | 75 | } |
| 76 | 76 | \ No newline at end of file |
@@ -18,123 +18,123 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Builder |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var InputInterface |
|
| 23 | - */ |
|
| 24 | - protected $input; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var Runner |
|
| 28 | - */ |
|
| 29 | - protected $runner; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Serializer |
|
| 33 | - */ |
|
| 34 | - protected $serializer; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param array $config |
|
| 38 | - */ |
|
| 39 | - public function __construct(array $config = []) |
|
| 40 | - { |
|
| 41 | - $config = $this->getConfig($config); |
|
| 42 | - |
|
| 43 | - $this->input = $config['input']; |
|
| 44 | - $this->runner = $config['runner']; |
|
| 45 | - $this->serializer = new Serializer($config['output'], $config['escaper']); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Build and return a regular expression that matches all of the given strings |
|
| 50 | - * |
|
| 51 | - * @param string[] $strings Literal strings to be matched |
|
| 52 | - * @return string Regular expression (without delimiters) |
|
| 53 | - */ |
|
| 54 | - public function build(array $strings) |
|
| 55 | - { |
|
| 56 | - $strings = array_unique($strings); |
|
| 57 | - if ($strings === ['']) |
|
| 58 | - { |
|
| 59 | - return ''; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $strings = $this->splitStrings($strings); |
|
| 63 | - usort($strings, __CLASS__ . '::compareStrings'); |
|
| 64 | - $strings = $this->runner->run($strings); |
|
| 65 | - |
|
| 66 | - return $this->serializer->serializeStrings($strings); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Compare two split strings |
|
| 71 | - * |
|
| 72 | - * Will sort strings in ascending order |
|
| 73 | - * |
|
| 74 | - * @param integer[] $a |
|
| 75 | - * @param integer[] $b |
|
| 76 | - * @return integer |
|
| 77 | - */ |
|
| 78 | - protected function compareStrings(array $a, array $b) |
|
| 79 | - { |
|
| 80 | - foreach ($b as $k => $v) |
|
| 81 | - { |
|
| 82 | - if (!isset($a[$k]) || $a[$k] < $b[$k]) |
|
| 83 | - { |
|
| 84 | - // A is shorter or A[k] is less than B[k]. Sort A before B |
|
| 85 | - return -1; |
|
| 86 | - } |
|
| 87 | - if ($a[$k] > $b[$k]) |
|
| 88 | - { |
|
| 89 | - // A[k] comes after B[k]. Sort A after B |
|
| 90 | - return 1; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // A is longer than B. Sort A after B |
|
| 95 | - return 1; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Build the full config array based on given input |
|
| 100 | - * |
|
| 101 | - * @param array $config Sparse config |
|
| 102 | - * @return array Full config |
|
| 103 | - */ |
|
| 104 | - protected function getConfig(array $config) |
|
| 105 | - { |
|
| 106 | - $config += [ |
|
| 107 | - 'delimiter' => '/', |
|
| 108 | - 'input' => 'Bytes', |
|
| 109 | - 'output' => 'Bytes' |
|
| 110 | - ]; |
|
| 111 | - $config['escaper'] = new Escaper($config['delimiter']); |
|
| 112 | - |
|
| 113 | - $className = __NAMESPACE__ . '\\Input\\' . $config['input']; |
|
| 114 | - $config['input'] = new $className; |
|
| 115 | - |
|
| 116 | - $className = __NAMESPACE__ . '\\Output\\' . $config['output']; |
|
| 117 | - $config['output'] = new $className; |
|
| 118 | - |
|
| 119 | - $config['runner'] = new Runner; |
|
| 120 | - $config['runner']->addPass(new MergePrefix); |
|
| 121 | - $config['runner']->addPass(new GroupSingleCharacters); |
|
| 122 | - $config['runner']->addPass(new Recurse($config['runner'])); |
|
| 123 | - $config['runner']->addPass(new PromoteSingleStrings); |
|
| 124 | - $config['runner']->addPass(new MergeSuffix); |
|
| 125 | - $config['runner']->addPass(new CoalesceSingleCharacterPrefix); |
|
| 126 | - |
|
| 127 | - return $config; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Split all given strings by character |
|
| 132 | - * |
|
| 133 | - * @param string[] $strings List of strings |
|
| 134 | - * @return array[] List of arrays |
|
| 135 | - */ |
|
| 136 | - protected function splitStrings(array $strings) |
|
| 137 | - { |
|
| 138 | - return array_map([$this->input, 'split'], $strings); |
|
| 139 | - } |
|
| 21 | + /** |
|
| 22 | + * @var InputInterface |
|
| 23 | + */ |
|
| 24 | + protected $input; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var Runner |
|
| 28 | + */ |
|
| 29 | + protected $runner; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Serializer |
|
| 33 | + */ |
|
| 34 | + protected $serializer; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param array $config |
|
| 38 | + */ |
|
| 39 | + public function __construct(array $config = []) |
|
| 40 | + { |
|
| 41 | + $config = $this->getConfig($config); |
|
| 42 | + |
|
| 43 | + $this->input = $config['input']; |
|
| 44 | + $this->runner = $config['runner']; |
|
| 45 | + $this->serializer = new Serializer($config['output'], $config['escaper']); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Build and return a regular expression that matches all of the given strings |
|
| 50 | + * |
|
| 51 | + * @param string[] $strings Literal strings to be matched |
|
| 52 | + * @return string Regular expression (without delimiters) |
|
| 53 | + */ |
|
| 54 | + public function build(array $strings) |
|
| 55 | + { |
|
| 56 | + $strings = array_unique($strings); |
|
| 57 | + if ($strings === ['']) |
|
| 58 | + { |
|
| 59 | + return ''; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $strings = $this->splitStrings($strings); |
|
| 63 | + usort($strings, __CLASS__ . '::compareStrings'); |
|
| 64 | + $strings = $this->runner->run($strings); |
|
| 65 | + |
|
| 66 | + return $this->serializer->serializeStrings($strings); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Compare two split strings |
|
| 71 | + * |
|
| 72 | + * Will sort strings in ascending order |
|
| 73 | + * |
|
| 74 | + * @param integer[] $a |
|
| 75 | + * @param integer[] $b |
|
| 76 | + * @return integer |
|
| 77 | + */ |
|
| 78 | + protected function compareStrings(array $a, array $b) |
|
| 79 | + { |
|
| 80 | + foreach ($b as $k => $v) |
|
| 81 | + { |
|
| 82 | + if (!isset($a[$k]) || $a[$k] < $b[$k]) |
|
| 83 | + { |
|
| 84 | + // A is shorter or A[k] is less than B[k]. Sort A before B |
|
| 85 | + return -1; |
|
| 86 | + } |
|
| 87 | + if ($a[$k] > $b[$k]) |
|
| 88 | + { |
|
| 89 | + // A[k] comes after B[k]. Sort A after B |
|
| 90 | + return 1; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // A is longer than B. Sort A after B |
|
| 95 | + return 1; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Build the full config array based on given input |
|
| 100 | + * |
|
| 101 | + * @param array $config Sparse config |
|
| 102 | + * @return array Full config |
|
| 103 | + */ |
|
| 104 | + protected function getConfig(array $config) |
|
| 105 | + { |
|
| 106 | + $config += [ |
|
| 107 | + 'delimiter' => '/', |
|
| 108 | + 'input' => 'Bytes', |
|
| 109 | + 'output' => 'Bytes' |
|
| 110 | + ]; |
|
| 111 | + $config['escaper'] = new Escaper($config['delimiter']); |
|
| 112 | + |
|
| 113 | + $className = __NAMESPACE__ . '\\Input\\' . $config['input']; |
|
| 114 | + $config['input'] = new $className; |
|
| 115 | + |
|
| 116 | + $className = __NAMESPACE__ . '\\Output\\' . $config['output']; |
|
| 117 | + $config['output'] = new $className; |
|
| 118 | + |
|
| 119 | + $config['runner'] = new Runner; |
|
| 120 | + $config['runner']->addPass(new MergePrefix); |
|
| 121 | + $config['runner']->addPass(new GroupSingleCharacters); |
|
| 122 | + $config['runner']->addPass(new Recurse($config['runner'])); |
|
| 123 | + $config['runner']->addPass(new PromoteSingleStrings); |
|
| 124 | + $config['runner']->addPass(new MergeSuffix); |
|
| 125 | + $config['runner']->addPass(new CoalesceSingleCharacterPrefix); |
|
| 126 | + |
|
| 127 | + return $config; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Split all given strings by character |
|
| 132 | + * |
|
| 133 | + * @param string[] $strings List of strings |
|
| 134 | + * @return array[] List of arrays |
|
| 135 | + */ |
|
| 136 | + protected function splitStrings(array $strings) |
|
| 137 | + { |
|
| 138 | + return array_map([$this->input, 'split'], $strings); |
|
| 139 | + } |
|
| 140 | 140 | } |
| 141 | 141 | \ No newline at end of file |