@@ -9,16 +9,16 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Bytes implements InputInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - public function split($string) |
|
| 16 | - { |
|
| 17 | - if ($string === '') |
|
| 18 | - { |
|
| 19 | - return []; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + public function split($string) |
|
| 16 | + { |
|
| 17 | + if ($string === '') |
|
| 18 | + { |
|
| 19 | + return []; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - return array_map('ord', str_split($string, 1)); |
|
| 23 | - } |
|
| 22 | + return array_map('ord', str_split($string, 1)); |
|
| 23 | + } |
|
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | |
| 10 | 10 | interface InputInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Split given string into a list of values |
|
| 14 | - * |
|
| 15 | - * @param string $string |
|
| 16 | - * @return integer[] |
|
| 17 | - */ |
|
| 18 | - public function split($string); |
|
| 12 | + /** |
|
| 13 | + * Split given string into a list of values |
|
| 14 | + * |
|
| 15 | + * @param string $string |
|
| 16 | + * @return integer[] |
|
| 17 | + */ |
|
| 18 | + public function split($string); |
|
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file |
@@ -11,62 +11,62 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Utf8 implements InputInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * {@inheritdoc} |
|
| 16 | - */ |
|
| 17 | - public function split($string) |
|
| 18 | - { |
|
| 19 | - if (preg_match_all('(.)us', $string, $matches) === false) |
|
| 20 | - { |
|
| 21 | - throw new InvalidArgumentException('Invalid UTF-8 string'); |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * {@inheritdoc} |
|
| 16 | + */ |
|
| 17 | + public function split($string) |
|
| 18 | + { |
|
| 19 | + if (preg_match_all('(.)us', $string, $matches) === false) |
|
| 20 | + { |
|
| 21 | + throw new InvalidArgumentException('Invalid UTF-8 string'); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - return $this->charsToCodepoints($matches[0]); |
|
| 25 | - } |
|
| 24 | + return $this->charsToCodepoints($matches[0]); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Convert a list of UTF-8 characters to a list of Unicode codepoint |
|
| 29 | - * |
|
| 30 | - * @param string[] $chars |
|
| 31 | - * @return integer[] |
|
| 32 | - */ |
|
| 33 | - protected function charsToCodepoints(array $chars) |
|
| 34 | - { |
|
| 35 | - return array_map([$this, 'cp'], $chars); |
|
| 36 | - } |
|
| 27 | + /** |
|
| 28 | + * Convert a list of UTF-8 characters to a list of Unicode codepoint |
|
| 29 | + * |
|
| 30 | + * @param string[] $chars |
|
| 31 | + * @return integer[] |
|
| 32 | + */ |
|
| 33 | + protected function charsToCodepoints(array $chars) |
|
| 34 | + { |
|
| 35 | + return array_map([$this, 'cp'], $chars); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Compute and return the Unicode codepoint for given UTF-8 char |
|
| 40 | - * |
|
| 41 | - * @param string $char UTF-8 char |
|
| 42 | - * @return integer |
|
| 43 | - */ |
|
| 44 | - protected function cp($char) |
|
| 45 | - { |
|
| 46 | - $size = strlen($char); |
|
| 47 | - if ($size === 1) |
|
| 48 | - { |
|
| 49 | - $cp = ord($char); |
|
| 50 | - } |
|
| 51 | - elseif ($size === 2) |
|
| 52 | - { |
|
| 53 | - $cp = ((ord($char[0]) & 0b00011111) << 6) |
|
| 54 | - | (ord($char[1]) & 0b00111111); |
|
| 55 | - } |
|
| 56 | - elseif ($size === 3) |
|
| 57 | - { |
|
| 58 | - $cp = ((ord($char[0]) & 0b00001111) << 12) |
|
| 59 | - | ((ord($char[1]) & 0b00111111) << 6) |
|
| 60 | - | (ord($char[2]) & 0b00111111); |
|
| 61 | - } |
|
| 62 | - else |
|
| 63 | - { |
|
| 64 | - $cp = ((ord($char[0]) & 0b00000111) << 18) |
|
| 65 | - | ((ord($char[1]) & 0b00111111) << 12) |
|
| 66 | - | ((ord($char[2]) & 0b00111111) << 6) |
|
| 67 | - | (ord($char[3]) & 0b00111111); |
|
| 68 | - } |
|
| 38 | + /** |
|
| 39 | + * Compute and return the Unicode codepoint for given UTF-8 char |
|
| 40 | + * |
|
| 41 | + * @param string $char UTF-8 char |
|
| 42 | + * @return integer |
|
| 43 | + */ |
|
| 44 | + protected function cp($char) |
|
| 45 | + { |
|
| 46 | + $size = strlen($char); |
|
| 47 | + if ($size === 1) |
|
| 48 | + { |
|
| 49 | + $cp = ord($char); |
|
| 50 | + } |
|
| 51 | + elseif ($size === 2) |
|
| 52 | + { |
|
| 53 | + $cp = ((ord($char[0]) & 0b00011111) << 6) |
|
| 54 | + | (ord($char[1]) & 0b00111111); |
|
| 55 | + } |
|
| 56 | + elseif ($size === 3) |
|
| 57 | + { |
|
| 58 | + $cp = ((ord($char[0]) & 0b00001111) << 12) |
|
| 59 | + | ((ord($char[1]) & 0b00111111) << 6) |
|
| 60 | + | (ord($char[2]) & 0b00111111); |
|
| 61 | + } |
|
| 62 | + else |
|
| 63 | + { |
|
| 64 | + $cp = ((ord($char[0]) & 0b00000111) << 18) |
|
| 65 | + | ((ord($char[1]) & 0b00111111) << 12) |
|
| 66 | + | ((ord($char[2]) & 0b00111111) << 6) |
|
| 67 | + | (ord($char[3]) & 0b00111111); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return $cp; |
|
| 71 | - } |
|
| 70 | + return $cp; |
|
| 71 | + } |
|
| 72 | 72 | } |
| 73 | 73 | \ No newline at end of file |
@@ -18,94 +18,94 @@ |
||
| 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 | - sort($strings); |
|
| 62 | - |
|
| 63 | - $strings = $this->splitStrings($strings); |
|
| 64 | - $strings = $this->runner->run($strings); |
|
| 65 | - |
|
| 66 | - return $this->serializer->serializeStrings($strings); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Build the full config array based on given input |
|
| 71 | - * |
|
| 72 | - * @param array $config Sparse config |
|
| 73 | - * @return array Full config |
|
| 74 | - */ |
|
| 75 | - protected function getConfig(array $config) |
|
| 76 | - { |
|
| 77 | - $config += [ |
|
| 78 | - 'delimiter' => '/', |
|
| 79 | - 'input' => 'Bytes', |
|
| 80 | - 'output' => 'Bytes' |
|
| 81 | - ]; |
|
| 82 | - $config['escaper'] = new Escaper($config['delimiter']); |
|
| 83 | - |
|
| 84 | - $className = __NAMESPACE__ . '\\Input\\' . $config['input']; |
|
| 85 | - $config['input'] = new $className; |
|
| 86 | - |
|
| 87 | - $className = __NAMESPACE__ . '\\Output\\' . $config['output']; |
|
| 88 | - $config['output'] = new $className; |
|
| 89 | - |
|
| 90 | - $config['runner'] = new Runner; |
|
| 91 | - $config['runner']->addPass(new MergePrefix); |
|
| 92 | - $config['runner']->addPass(new GroupSingleCharacters); |
|
| 93 | - $config['runner']->addPass(new Recurse($config['runner'])); |
|
| 94 | - $config['runner']->addPass(new PromoteSingleStrings); |
|
| 95 | - $config['runner']->addPass(new MergeSuffix); |
|
| 96 | - $config['runner']->addPass(new CoalesceSingleCharacterPrefix); |
|
| 97 | - |
|
| 98 | - return $config; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Split all given strings by character |
|
| 103 | - * |
|
| 104 | - * @param string[] $strings List of strings |
|
| 105 | - * @return array[] List of arrays |
|
| 106 | - */ |
|
| 107 | - protected function splitStrings(array $strings) |
|
| 108 | - { |
|
| 109 | - return array_map([$this->input, 'split'], $strings); |
|
| 110 | - } |
|
| 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 | + sort($strings); |
|
| 62 | + |
|
| 63 | + $strings = $this->splitStrings($strings); |
|
| 64 | + $strings = $this->runner->run($strings); |
|
| 65 | + |
|
| 66 | + return $this->serializer->serializeStrings($strings); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Build the full config array based on given input |
|
| 71 | + * |
|
| 72 | + * @param array $config Sparse config |
|
| 73 | + * @return array Full config |
|
| 74 | + */ |
|
| 75 | + protected function getConfig(array $config) |
|
| 76 | + { |
|
| 77 | + $config += [ |
|
| 78 | + 'delimiter' => '/', |
|
| 79 | + 'input' => 'Bytes', |
|
| 80 | + 'output' => 'Bytes' |
|
| 81 | + ]; |
|
| 82 | + $config['escaper'] = new Escaper($config['delimiter']); |
|
| 83 | + |
|
| 84 | + $className = __NAMESPACE__ . '\\Input\\' . $config['input']; |
|
| 85 | + $config['input'] = new $className; |
|
| 86 | + |
|
| 87 | + $className = __NAMESPACE__ . '\\Output\\' . $config['output']; |
|
| 88 | + $config['output'] = new $className; |
|
| 89 | + |
|
| 90 | + $config['runner'] = new Runner; |
|
| 91 | + $config['runner']->addPass(new MergePrefix); |
|
| 92 | + $config['runner']->addPass(new GroupSingleCharacters); |
|
| 93 | + $config['runner']->addPass(new Recurse($config['runner'])); |
|
| 94 | + $config['runner']->addPass(new PromoteSingleStrings); |
|
| 95 | + $config['runner']->addPass(new MergeSuffix); |
|
| 96 | + $config['runner']->addPass(new CoalesceSingleCharacterPrefix); |
|
| 97 | + |
|
| 98 | + return $config; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Split all given strings by character |
|
| 103 | + * |
|
| 104 | + * @param string[] $strings List of strings |
|
| 105 | + * @return array[] List of arrays |
|
| 106 | + */ |
|
| 107 | + protected function splitStrings(array $strings) |
|
| 108 | + { |
|
| 109 | + return array_map([$this->input, 'split'], $strings); |
|
| 110 | + } |
|
| 111 | 111 | } |
| 112 | 112 | \ No newline at end of file |
@@ -11,35 +11,35 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Runner |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var PassInterface[] |
|
| 16 | - */ |
|
| 17 | - protected $passes = []; |
|
| 14 | + /** |
|
| 15 | + * @var PassInterface[] |
|
| 16 | + */ |
|
| 17 | + protected $passes = []; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Add a pass to the list |
|
| 21 | - * |
|
| 22 | - * @param PassInterface $pass |
|
| 23 | - * @return void |
|
| 24 | - */ |
|
| 25 | - public function addPass(PassInterface $pass) |
|
| 26 | - { |
|
| 27 | - $this->passes[] = $pass; |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Add a pass to the list |
|
| 21 | + * |
|
| 22 | + * @param PassInterface $pass |
|
| 23 | + * @return void |
|
| 24 | + */ |
|
| 25 | + public function addPass(PassInterface $pass) |
|
| 26 | + { |
|
| 27 | + $this->passes[] = $pass; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Run all passes on the list of strings |
|
| 32 | - * |
|
| 33 | - * @param array[] $strings |
|
| 34 | - * @return array[] |
|
| 35 | - */ |
|
| 36 | - public function run(array $strings) |
|
| 37 | - { |
|
| 38 | - foreach ($this->passes as $pass) |
|
| 39 | - { |
|
| 40 | - $strings = $pass->run($strings); |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Run all passes on the list of strings |
|
| 32 | + * |
|
| 33 | + * @param array[] $strings |
|
| 34 | + * @return array[] |
|
| 35 | + */ |
|
| 36 | + public function run(array $strings) |
|
| 37 | + { |
|
| 38 | + foreach ($this->passes as $pass) |
|
| 39 | + { |
|
| 40 | + $strings = $pass->run($strings); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return $strings; |
|
| 44 | - } |
|
| 43 | + return $strings; |
|
| 44 | + } |
|
| 45 | 45 | } |
| 46 | 46 | \ No newline at end of file |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | |
| 10 | 10 | interface OutputInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Serialize a value into a character |
|
| 14 | - * |
|
| 15 | - * @param integer $value |
|
| 16 | - * @return string |
|
| 17 | - */ |
|
| 18 | - public function output($value); |
|
| 12 | + /** |
|
| 13 | + * Serialize a value into a character |
|
| 14 | + * |
|
| 15 | + * @param integer $value |
|
| 16 | + * @return string |
|
| 17 | + */ |
|
| 18 | + public function output($value); |
|
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file |
@@ -11,16 +11,16 @@ |
||
| 11 | 11 | |
| 12 | 12 | class JavaScript extends PrintableAscii |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * {@inheritdoc} |
|
| 16 | - */ |
|
| 17 | - public function escapeUnicode($cp) |
|
| 18 | - { |
|
| 19 | - if ($cp > 0xFFFF) |
|
| 20 | - { |
|
| 21 | - throw new InvalidArgumentException('Invalid JavaScript codepoint 0x' . dechex($cp)); |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * {@inheritdoc} |
|
| 16 | + */ |
|
| 17 | + public function escapeUnicode($cp) |
|
| 18 | + { |
|
| 19 | + if ($cp > 0xFFFF) |
|
| 20 | + { |
|
| 21 | + throw new InvalidArgumentException('Invalid JavaScript codepoint 0x' . dechex($cp)); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - return sprintf('\\u%04X', $cp); |
|
| 25 | - } |
|
| 24 | + return sprintf('\\u%04X', $cp); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -11,16 +11,16 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Bytes implements OutputInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * {@inheritdoc} |
|
| 16 | - */ |
|
| 17 | - public function output($value) |
|
| 18 | - { |
|
| 19 | - if ($value > 255) |
|
| 20 | - { |
|
| 21 | - throw new InvalidArgumentException('Invalid byte value ' . $value); |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * {@inheritdoc} |
|
| 16 | + */ |
|
| 17 | + public function output($value) |
|
| 18 | + { |
|
| 19 | + if ($value > 255) |
|
| 20 | + { |
|
| 21 | + throw new InvalidArgumentException('Invalid byte value ' . $value); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - return chr($value); |
|
| 25 | - } |
|
| 24 | + return chr($value); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | |
| 10 | 10 | class PHP extends PrintableAscii |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - public function escapeUnicode($cp) |
|
| 16 | - { |
|
| 17 | - return sprintf('\\x{%04X}', $cp); |
|
| 18 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + public function escapeUnicode($cp) |
|
| 16 | + { |
|
| 17 | + return sprintf('\\x{%04X}', $cp); |
|
| 18 | + } |
|
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file |