| 1 | <?php |
||
| 17 | final class VersionTokenizer |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Accepts a version string & returns an array of tokenized values. |
||
| 21 | * |
||
| 22 | * @todo Handle build metadata ('+' at end of version followed by [0-9a-z-]+ |
||
| 23 | * |
||
| 24 | * @param string $versionString |
||
| 25 | * |
||
| 26 | * @return Token[] |
||
| 27 | */ |
||
| 28 | 31 | public function tokenize($versionString) |
|
| 29 | { |
||
| 30 | /** @var Token[] $tokenList */ |
||
| 31 | 31 | $tokenList = []; |
|
| 32 | 31 | $digitAccumulator = ''; |
|
| 33 | 31 | $stringAccumulator = ''; |
|
| 34 | |||
| 35 | // Iterate through string, character-by-character |
||
| 36 | 31 | for ($i = 0; $i < strlen($versionString); $i++) { |
|
| 37 | 31 | $chr = $this->getCharacter($i, $versionString); |
|
| 38 | |||
| 39 | 31 | switch (true) { |
|
|
1 ignored issue
–
show
|
|||
| 40 | |||
| 41 | // Simple token types - separators and range indicators |
||
| 42 | 31 | case !is_null($this->getSimpleToken($chr)): |
|
| 43 | |||
| 44 | // Handle preceding digits or labels |
||
| 45 | 29 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
| 46 | 29 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
| 47 | |||
| 48 | 29 | $tokenList[] = $this->getSimpleToken($chr); |
|
| 49 | 29 | break; |
|
| 50 | |||
| 51 | // Comparator token types |
||
| 52 | 31 | case !is_null($token = $this->getComparatorToken($i, $versionString)): |
|
| 53 | |||
| 54 | // Handle preceding digits or labels |
||
| 55 | 11 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
| 56 | 11 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
| 57 | |||
| 58 | 11 | $this->addImplicitAnd($tokenList); |
|
| 59 | |||
| 60 | 11 | $tokenList[] = $token; |
|
| 61 | 11 | break; |
|
| 62 | |||
| 63 | // Skip the 'v' character if immediately preceding a digit |
||
| 64 | 31 | case $this->isPrefixedV($i, $versionString): |
|
| 65 | // Do nothing |
||
| 66 | // TODO: Should we store this for correct reverse transformation? |
||
| 67 | 1 | break; |
|
| 68 | |||
| 69 | // Spaces, pipes, ampersands and commas may contextually be logical AND/OR |
||
| 70 | 31 | case $this->isPossibleLogicalOperator($chr): |
|
| 71 | |||
| 72 | // Handle preceding digits or labels |
||
| 73 | 6 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
| 74 | 6 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
| 75 | |||
| 76 | $operatorTokenList = [ |
||
| 77 | 6 | Token::GREATER_THAN, |
|
| 78 | 6 | Token::GREATER_THAN_EQUAL, |
|
| 79 | 6 | Token::LESS_THAN, |
|
| 80 | 6 | Token::LESS_THAN_EQUAL, |
|
| 81 | Token::EQUAL_TO |
||
| 82 | 6 | ]; |
|
| 83 | |||
| 84 | // No previous tokens, or previous token was not a comparator |
||
| 85 | if ( |
||
| 86 | 6 | !count($tokenList) |
|
| 87 | 6 | || !in_array($tokenList[count($tokenList)-1]->getType(), $operatorTokenList) |
|
| 88 | 6 | ) { |
|
| 89 | 5 | $possibleOperator = trim($chr); |
|
| 90 | |||
| 91 | 5 | for ($j = $i + 1; $j < strlen($versionString); $j++) { |
|
| 92 | 5 | $operatorChr = $this->getCharacter($j, $versionString); |
|
| 93 | |||
| 94 | 5 | if ($this->isPossibleLogicalOperator($operatorChr)) { |
|
| 95 | 3 | $possibleOperator .= trim($operatorChr); |
|
| 96 | 3 | } else { |
|
| 97 | 5 | if (!strlen($possibleOperator) || ',' === $possibleOperator) { |
|
| 98 | 4 | $tokenList[] = new Token(Token::LOGICAL_AND, $possibleOperator); |
|
| 99 | 4 | } else { |
|
| 100 | 2 | $tokenList[] = new Token(Token::LOGICAL_OR, $possibleOperator); |
|
| 101 | } |
||
| 102 | 5 | $i = $j - 1; |
|
| 103 | 5 | break; |
|
| 104 | } |
||
| 105 | 3 | } |
|
| 106 | 5 | } |
|
| 107 | |||
| 108 | 6 | break; |
|
| 109 | |||
| 110 | // Start accumulating on the first non-digit & continue until we reach a separator |
||
| 111 | 31 | case !is_numeric($chr) || strlen($stringAccumulator): |
|
| 112 | 5 | $stringAccumulator .= $chr; |
|
| 113 | 5 | break; |
|
| 114 | |||
| 115 | // Simple digits |
||
| 116 | 31 | default: |
|
| 117 | 31 | $digitAccumulator .= $chr; |
|
| 118 | 31 | break; |
|
| 119 | 31 | } |
|
| 120 | 31 | } |
|
| 121 | |||
| 122 | // Handle any remaining digits or labels |
||
| 123 | 31 | $this->conditionallyAddToken(Token::DIGITS, $digitAccumulator, $tokenList); |
|
| 124 | 31 | $this->conditionallyAddToken(Token::LABEL_STRING, $stringAccumulator, $tokenList); |
|
| 125 | |||
| 126 | 31 | return $tokenList; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add an implicit AND between a version & the comparator of a subsequent version. |
||
| 131 | * |
||
| 132 | * @param Token[] $tokenList |
||
| 133 | */ |
||
| 134 | 11 | private function addImplicitAnd(array &$tokenList) |
|
| 135 | { |
||
| 136 | $digitTokenList = [ |
||
| 137 | 11 | Token::DIGITS, |
|
| 138 | Token::WILDCARD_DIGITS |
||
| 139 | 11 | ]; |
|
| 140 | |||
| 141 | 11 | if (count($tokenList) && in_array($tokenList[count($tokenList) -1]->getType(), $digitTokenList)) { |
|
| 142 | 1 | $tokenList[] = new Token(Token::LOGICAL_AND, ''); |
|
| 143 | 1 | } |
|
| 144 | 11 | } |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Returns true if the character is an AND comparator |
||
| 148 | * |
||
| 149 | * @param string $chr |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 31 | private function isPossibleLogicalOperator($chr) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Tries to find a comparator token beginning at the specified index. |
||
| 160 | * |
||
| 161 | * @param int $index |
||
| 162 | * @param string $versionString |
||
| 163 | * |
||
| 164 | * @return Token|null |
||
| 165 | */ |
||
| 166 | 31 | private function getComparatorToken(&$index, $versionString) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get a token from a single-character. |
||
| 197 | * |
||
| 198 | * @param string $chr |
||
| 199 | * |
||
| 200 | * @return Token|null |
||
| 201 | */ |
||
| 202 | 31 | private function getSimpleToken($chr) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Add a token to the list if the string length is greater than 0, empties string. |
||
| 223 | * |
||
| 224 | * @param string $type One of Token class constants |
||
| 225 | * @param string $value |
||
| 226 | * @param Token[] $tokenList |
||
| 227 | */ |
||
| 228 | 31 | public function conditionallyAddToken($type, &$value, &$tokenList) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Returns true if the character is a 'v' prefix to a version number (e.g. v1.0.7). |
||
| 242 | * |
||
| 243 | * @param int $index |
||
| 244 | * @param string $versionString |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 31 | private function isPrefixedV($index, $versionString) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get the next character after the specified index or an empty string if not present. |
||
| 259 | * |
||
| 260 | * @param int $index |
||
| 261 | * @param string $versionString |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 31 | private function getCharacter($index, $versionString) |
|
| 273 | } |
||
| 274 |