@@ -18,134 +18,134 @@ |
||
| 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 += [ |
|
| 42 | - 'delimiter' => '/', |
|
| 43 | - 'input' => 'Bytes', |
|
| 44 | - 'output' => 'Bytes' |
|
| 45 | - ]; |
|
| 46 | - |
|
| 47 | - $this->setInput($config['input']); |
|
| 48 | - $this->setSerializer($config['output'], $config['delimiter']); |
|
| 49 | - $this->setRunner(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Build and return a regular expression that matches all of the given strings |
|
| 54 | - * |
|
| 55 | - * @param string[] $strings Literal strings to be matched |
|
| 56 | - * @return string Regular expression (without delimiters) |
|
| 57 | - */ |
|
| 58 | - public function build(array $strings) |
|
| 59 | - { |
|
| 60 | - $strings = array_unique($strings); |
|
| 61 | - if ($strings === ['']) |
|
| 62 | - { |
|
| 63 | - return ''; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $strings = $this->splitStrings($strings); |
|
| 67 | - usort($strings, __CLASS__ . '::compareStrings'); |
|
| 68 | - $strings = $this->runner->run($strings); |
|
| 69 | - |
|
| 70 | - return $this->serializer->serializeStrings($strings); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Compare two split strings |
|
| 75 | - * |
|
| 76 | - * Will sort strings in ascending order |
|
| 77 | - * |
|
| 78 | - * @param integer[] $a |
|
| 79 | - * @param integer[] $b |
|
| 80 | - * @return integer |
|
| 81 | - */ |
|
| 82 | - protected function compareStrings(array $a, array $b) |
|
| 83 | - { |
|
| 84 | - $i = -1; |
|
| 85 | - $cnt = min(count($a), count($b)); |
|
| 86 | - while (++$i < $cnt) |
|
| 87 | - { |
|
| 88 | - if ($a[$i] !== $b[$i]) |
|
| 89 | - { |
|
| 90 | - return $a[$i] - $b[$i]; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return count($a) - count($b); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Set the InputInterface instance in $this->input |
|
| 99 | - * |
|
| 100 | - * @param string $inputType |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - protected function setInput($inputType) |
|
| 104 | - { |
|
| 105 | - $className = __NAMESPACE__ . '\\Input\\' . $inputType; |
|
| 106 | - $this->input = new $className; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Set the Runner instance $in this->runner |
|
| 111 | - * |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - protected function setRunner() |
|
| 115 | - { |
|
| 116 | - $this->runner = new Runner; |
|
| 117 | - $this->runner->addPass(new MergePrefix); |
|
| 118 | - $this->runner->addPass(new GroupSingleCharacters); |
|
| 119 | - $this->runner->addPass(new Recurse($this->runner)); |
|
| 120 | - $this->runner->addPass(new PromoteSingleStrings); |
|
| 121 | - $this->runner->addPass(new MergeSuffix); |
|
| 122 | - $this->runner->addPass(new CoalesceSingleCharacterPrefix); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Set the Serializer instance in $this->serializer |
|
| 127 | - * |
|
| 128 | - * @param string $outputType |
|
| 129 | - * @param string $delimiter |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - protected function setSerializer($outputType, $delimiter) |
|
| 133 | - { |
|
| 134 | - $className = __NAMESPACE__ . '\\Output\\' . $outputType; |
|
| 135 | - $output = new $className; |
|
| 136 | - $escaper = new Escaper($delimiter); |
|
| 137 | - |
|
| 138 | - $this->serializer = new Serializer($output, $escaper); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Split all given strings by character |
|
| 143 | - * |
|
| 144 | - * @param string[] $strings List of strings |
|
| 145 | - * @return array[] List of arrays |
|
| 146 | - */ |
|
| 147 | - protected function splitStrings(array $strings) |
|
| 148 | - { |
|
| 149 | - return array_map([$this->input, 'split'], $strings); |
|
| 150 | - } |
|
| 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 += [ |
|
| 42 | + 'delimiter' => '/', |
|
| 43 | + 'input' => 'Bytes', |
|
| 44 | + 'output' => 'Bytes' |
|
| 45 | + ]; |
|
| 46 | + |
|
| 47 | + $this->setInput($config['input']); |
|
| 48 | + $this->setSerializer($config['output'], $config['delimiter']); |
|
| 49 | + $this->setRunner(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Build and return a regular expression that matches all of the given strings |
|
| 54 | + * |
|
| 55 | + * @param string[] $strings Literal strings to be matched |
|
| 56 | + * @return string Regular expression (without delimiters) |
|
| 57 | + */ |
|
| 58 | + public function build(array $strings) |
|
| 59 | + { |
|
| 60 | + $strings = array_unique($strings); |
|
| 61 | + if ($strings === ['']) |
|
| 62 | + { |
|
| 63 | + return ''; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $strings = $this->splitStrings($strings); |
|
| 67 | + usort($strings, __CLASS__ . '::compareStrings'); |
|
| 68 | + $strings = $this->runner->run($strings); |
|
| 69 | + |
|
| 70 | + return $this->serializer->serializeStrings($strings); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Compare two split strings |
|
| 75 | + * |
|
| 76 | + * Will sort strings in ascending order |
|
| 77 | + * |
|
| 78 | + * @param integer[] $a |
|
| 79 | + * @param integer[] $b |
|
| 80 | + * @return integer |
|
| 81 | + */ |
|
| 82 | + protected function compareStrings(array $a, array $b) |
|
| 83 | + { |
|
| 84 | + $i = -1; |
|
| 85 | + $cnt = min(count($a), count($b)); |
|
| 86 | + while (++$i < $cnt) |
|
| 87 | + { |
|
| 88 | + if ($a[$i] !== $b[$i]) |
|
| 89 | + { |
|
| 90 | + return $a[$i] - $b[$i]; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return count($a) - count($b); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Set the InputInterface instance in $this->input |
|
| 99 | + * |
|
| 100 | + * @param string $inputType |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + protected function setInput($inputType) |
|
| 104 | + { |
|
| 105 | + $className = __NAMESPACE__ . '\\Input\\' . $inputType; |
|
| 106 | + $this->input = new $className; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Set the Runner instance $in this->runner |
|
| 111 | + * |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + protected function setRunner() |
|
| 115 | + { |
|
| 116 | + $this->runner = new Runner; |
|
| 117 | + $this->runner->addPass(new MergePrefix); |
|
| 118 | + $this->runner->addPass(new GroupSingleCharacters); |
|
| 119 | + $this->runner->addPass(new Recurse($this->runner)); |
|
| 120 | + $this->runner->addPass(new PromoteSingleStrings); |
|
| 121 | + $this->runner->addPass(new MergeSuffix); |
|
| 122 | + $this->runner->addPass(new CoalesceSingleCharacterPrefix); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Set the Serializer instance in $this->serializer |
|
| 127 | + * |
|
| 128 | + * @param string $outputType |
|
| 129 | + * @param string $delimiter |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + protected function setSerializer($outputType, $delimiter) |
|
| 133 | + { |
|
| 134 | + $className = __NAMESPACE__ . '\\Output\\' . $outputType; |
|
| 135 | + $output = new $className; |
|
| 136 | + $escaper = new Escaper($delimiter); |
|
| 137 | + |
|
| 138 | + $this->serializer = new Serializer($output, $escaper); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Split all given strings by character |
|
| 143 | + * |
|
| 144 | + * @param string[] $strings List of strings |
|
| 145 | + * @return array[] List of arrays |
|
| 146 | + */ |
|
| 147 | + protected function splitStrings(array $strings) |
|
| 148 | + { |
|
| 149 | + return array_map([$this->input, 'split'], $strings); |
|
| 150 | + } |
|
| 151 | 151 | } |
| 152 | 152 | \ No newline at end of file |