@@ -11,34 +11,34 @@ |
||
| 11 | 11 | |
| 12 | 12 | abstract class BaseImplementation implements OutputInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var integer |
|
| 16 | - */ |
|
| 17 | - protected $maxValue = 0; |
|
| 14 | + /** |
|
| 15 | + * @var integer |
|
| 16 | + */ |
|
| 17 | + protected $maxValue = 0; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var integer |
|
| 21 | - */ |
|
| 22 | - protected $minValue = 0; |
|
| 19 | + /** |
|
| 20 | + * @var integer |
|
| 21 | + */ |
|
| 22 | + protected $minValue = 0; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function output($value) |
|
| 28 | - { |
|
| 29 | - if ($value < $this->minValue || $value > $this->maxValue) |
|
| 30 | - { |
|
| 31 | - throw new InvalidArgumentException('Value ' . $value . ' is out of bounds (' . $this->minValue . '..' . $this->maxValue . ')'); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function output($value) |
|
| 28 | + { |
|
| 29 | + if ($value < $this->minValue || $value > $this->maxValue) |
|
| 30 | + { |
|
| 31 | + throw new InvalidArgumentException('Value ' . $value . ' is out of bounds (' . $this->minValue . '..' . $this->maxValue . ')'); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return $this->outputValidValue($value); |
|
| 35 | - } |
|
| 34 | + return $this->outputValidValue($value); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Serialize a valid value into a character |
|
| 39 | - * |
|
| 40 | - * @param integer $value |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - abstract protected function outputValidValue($value); |
|
| 37 | + /** |
|
| 38 | + * Serialize a valid value into a character |
|
| 39 | + * |
|
| 40 | + * @param integer $value |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + abstract protected function outputValidValue($value); |
|
| 44 | 44 | } |
| 45 | 45 | \ No newline at end of file |
@@ -9,93 +9,93 @@ |
||
| 9 | 9 | |
| 10 | 10 | class MergePrefix extends AbstractPass |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * {@inheritdoc} |
|
| 14 | - */ |
|
| 15 | - protected function processStrings(array $strings) |
|
| 16 | - { |
|
| 17 | - $newStrings = []; |
|
| 18 | - foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
|
| 19 | - { |
|
| 20 | - $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
|
| 21 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritdoc} |
|
| 14 | + */ |
|
| 15 | + protected function processStrings(array $strings) |
|
| 16 | + { |
|
| 17 | + $newStrings = []; |
|
| 18 | + foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
|
| 19 | + { |
|
| 20 | + $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - return $newStrings; |
|
| 24 | - } |
|
| 23 | + return $newStrings; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the number of leading elements common to all given strings |
|
| 28 | - * |
|
| 29 | - * @param array[] $strings |
|
| 30 | - * @return integer |
|
| 31 | - */ |
|
| 32 | - protected function getPrefixLength(array $strings) |
|
| 33 | - { |
|
| 34 | - $len = 1; |
|
| 35 | - $cnt = count($strings[0]); |
|
| 36 | - while ($len < $cnt && $this->stringsMatch($strings, $len)) |
|
| 37 | - { |
|
| 38 | - ++$len; |
|
| 39 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the number of leading elements common to all given strings |
|
| 28 | + * |
|
| 29 | + * @param array[] $strings |
|
| 30 | + * @return integer |
|
| 31 | + */ |
|
| 32 | + protected function getPrefixLength(array $strings) |
|
| 33 | + { |
|
| 34 | + $len = 1; |
|
| 35 | + $cnt = count($strings[0]); |
|
| 36 | + while ($len < $cnt && $this->stringsMatch($strings, $len)) |
|
| 37 | + { |
|
| 38 | + ++$len; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - return $len; |
|
| 42 | - } |
|
| 41 | + return $len; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Return given strings grouped by their first element |
|
| 46 | - * |
|
| 47 | - * NOTE: assumes that this pass is run before the first element of any string could be replaced |
|
| 48 | - * |
|
| 49 | - * @param array[] $strings |
|
| 50 | - * @return array[] |
|
| 51 | - */ |
|
| 52 | - protected function getStringsByPrefix(array $strings) |
|
| 53 | - { |
|
| 54 | - $byPrefix = []; |
|
| 55 | - foreach ($strings as $string) |
|
| 56 | - { |
|
| 57 | - $byPrefix[$string[0]][] = $string; |
|
| 58 | - } |
|
| 44 | + /** |
|
| 45 | + * Return given strings grouped by their first element |
|
| 46 | + * |
|
| 47 | + * NOTE: assumes that this pass is run before the first element of any string could be replaced |
|
| 48 | + * |
|
| 49 | + * @param array[] $strings |
|
| 50 | + * @return array[] |
|
| 51 | + */ |
|
| 52 | + protected function getStringsByPrefix(array $strings) |
|
| 53 | + { |
|
| 54 | + $byPrefix = []; |
|
| 55 | + foreach ($strings as $string) |
|
| 56 | + { |
|
| 57 | + $byPrefix[$string[0]][] = $string; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return $byPrefix; |
|
| 61 | - } |
|
| 60 | + return $byPrefix; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Merge given strings into a new single string |
|
| 65 | - * |
|
| 66 | - * @param array[] $strings |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - protected function mergeStrings(array $strings) |
|
| 70 | - { |
|
| 71 | - $len = $this->getPrefixLength($strings); |
|
| 72 | - $newString = array_slice($strings[0], 0, $len); |
|
| 73 | - foreach ($strings as $string) |
|
| 74 | - { |
|
| 75 | - $newString[$len][] = array_slice($string, $len); |
|
| 76 | - } |
|
| 63 | + /** |
|
| 64 | + * Merge given strings into a new single string |
|
| 65 | + * |
|
| 66 | + * @param array[] $strings |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + protected function mergeStrings(array $strings) |
|
| 70 | + { |
|
| 71 | + $len = $this->getPrefixLength($strings); |
|
| 72 | + $newString = array_slice($strings[0], 0, $len); |
|
| 73 | + foreach ($strings as $string) |
|
| 74 | + { |
|
| 75 | + $newString[$len][] = array_slice($string, $len); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - return $newString; |
|
| 79 | - } |
|
| 78 | + return $newString; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Test whether all given strings' elements match at given position |
|
| 83 | - * |
|
| 84 | - * @param array[] $strings |
|
| 85 | - * @param integer $pos |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - protected function stringsMatch(array $strings, $pos) |
|
| 89 | - { |
|
| 90 | - $value = $strings[0][$pos]; |
|
| 91 | - foreach ($strings as $string) |
|
| 92 | - { |
|
| 93 | - if (!isset($string[$pos]) || $string[$pos] !== $value) |
|
| 94 | - { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - } |
|
| 81 | + /** |
|
| 82 | + * Test whether all given strings' elements match at given position |
|
| 83 | + * |
|
| 84 | + * @param array[] $strings |
|
| 85 | + * @param integer $pos |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + protected function stringsMatch(array $strings, $pos) |
|
| 89 | + { |
|
| 90 | + $value = $strings[0][$pos]; |
|
| 91 | + foreach ($strings as $string) |
|
| 92 | + { |
|
| 93 | + if (!isset($string[$pos]) || $string[$pos] !== $value) |
|
| 94 | + { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - return true; |
|
| 100 | - } |
|
| 99 | + return true; |
|
| 100 | + } |
|
| 101 | 101 | } |
| 102 | 102 | \ No newline at end of file |
@@ -9,59 +9,59 @@ |
||
| 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 | - $i = count($strings); |
|
| 27 | - while (--$i >= 0) |
|
| 28 | - { |
|
| 29 | - array_pop($strings[$i]); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - array_unshift($newString, $strings); |
|
| 22 | + $newString = []; |
|
| 23 | + while ($this->hasMatchingSuffix($strings)) |
|
| 24 | + { |
|
| 25 | + array_unshift($newString, end($strings[0])); |
|
| 26 | + $i = count($strings); |
|
| 27 | + while (--$i >= 0) |
|
| 28 | + { |
|
| 29 | + array_pop($strings[$i]); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + array_unshift($newString, $strings); |
|
| 33 | 33 | |
| 34 | - return [$newString]; |
|
| 35 | - } |
|
| 34 | + return [$newString]; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Test whether all given strings have the same last element |
|
| 39 | - * |
|
| 40 | - * @param array[] $strings |
|
| 41 | - * @return bool |
|
| 42 | - */ |
|
| 43 | - protected function hasMatchingSuffix(array $strings) |
|
| 44 | - { |
|
| 45 | - $suffix = end($strings[1]); |
|
| 46 | - foreach ($strings as $string) |
|
| 47 | - { |
|
| 48 | - if (end($string) !== $suffix) |
|
| 49 | - { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - } |
|
| 37 | + /** |
|
| 38 | + * Test whether all given strings have the same last element |
|
| 39 | + * |
|
| 40 | + * @param array[] $strings |
|
| 41 | + * @return bool |
|
| 42 | + */ |
|
| 43 | + protected function hasMatchingSuffix(array $strings) |
|
| 44 | + { |
|
| 45 | + $suffix = end($strings[1]); |
|
| 46 | + foreach ($strings as $string) |
|
| 47 | + { |
|
| 48 | + if (end($string) !== $suffix) |
|
| 49 | + { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - return ($suffix !== false); |
|
| 55 | - } |
|
| 54 | + return ($suffix !== false); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Test whether this pass can be run on given list of strings |
|
| 59 | - * |
|
| 60 | - * @param array[] $strings |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - protected function isEligible(array $strings) |
|
| 64 | - { |
|
| 65 | - return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
| 66 | - } |
|
| 57 | + /** |
|
| 58 | + * Test whether this pass can be run on given list of strings |
|
| 59 | + * |
|
| 60 | + * @param array[] $strings |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + protected function isEligible(array $strings) |
|
| 64 | + { |
|
| 65 | + return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); |
|
| 66 | + } |
|
| 67 | 67 | } |
| 68 | 68 | \ No newline at end of file |