1 | <?php |
||
28 | final class HyphenatedRangeParser implements RangeParserInterface |
||
29 | { |
||
30 | use ChunkBySeparator; |
||
31 | |||
32 | /** @var VersionParser */ |
||
33 | private $versionParser; |
||
34 | |||
35 | /** @var ComparatorInterface */ |
||
36 | private $greaterOrEqualTo; |
||
37 | |||
38 | /** @var ComparatorInterface */ |
||
39 | private $lessThan; |
||
40 | |||
41 | /** @var ComparatorInterface */ |
||
42 | private $lessOrEqualTo; |
||
43 | |||
44 | /** @var string[][] */ |
||
45 | private $validConfigurations = [ |
||
46 | [Token::DIGITS, Token::DIGITS], |
||
47 | [Token::DIGITS, Token::LABEL_STRING, Token::DIGITS], |
||
48 | [Token::DIGITS, Token::DIGITS, Token::LABEL_STRING], |
||
49 | [Token::DIGITS, Token::LABEL_STRING, Token::DIGITS, Token::LABEL_STRING] |
||
50 | ]; |
||
51 | |||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param VersionParser $versionParser |
||
57 | * @param ComparatorInterface $greaterOrEqualTo |
||
58 | * @param ComparatorInterface $lessThan |
||
59 | * @param ComparatorInterface $lessOrEqualTo |
||
60 | */ |
||
61 | 11 | public function __construct( |
|
72 | |||
73 | /** |
||
74 | * Returns true if the token list can be parsed as a hyphenated range. |
||
75 | * |
||
76 | * @param Token[] $tokenList |
||
77 | * |
||
78 | * @return boolean |
||
79 | */ |
||
80 | 11 | public function canParse(array $tokenList) |
|
81 | { |
||
82 | 11 | $isRange = false; |
|
83 | 11 | $chunkedList = $this->chunkByDash($tokenList); |
|
84 | 11 | foreach ($this->validConfigurations as $configuration) { |
|
85 | 11 | list($lowerVersionTokenList, $upperVersionTokenList) = $this->getSingleVersionTokens($chunkedList); |
|
86 | 11 | $isRange = $isRange || ( |
|
87 | 11 | $this->chunksMatchConfiguration($chunkedList, $configuration) |
|
88 | 11 | && $this->versionParser->parse($lowerVersionTokenList) |
|
89 | 11 | && $this->versionParser->parse($upperVersionTokenList) |
|
90 | 11 | ); |
|
91 | 11 | } |
|
92 | |||
93 | 11 | return $isRange; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Build a ComparatorVersion representing the hyphenated range. |
||
98 | * |
||
99 | * @param Token[] $tokenList |
||
100 | * |
||
101 | * @return VersionRangeInterface |
||
102 | */ |
||
103 | 10 | public function parse(array $tokenList) |
|
104 | { |
||
105 | 10 | if (!$this->canParse($tokenList)) { |
|
106 | 4 | throw new \RuntimeException('Invalid version'); |
|
107 | } |
||
108 | |||
109 | 6 | $chunkedList = $this->chunkByDash($tokenList); |
|
110 | |||
111 | 6 | list($lowerVersionTokenList, $upperVersionTokenList) = $this->getSingleVersionTokens($chunkedList); |
|
112 | |||
113 | 6 | return new LogicalAnd( |
|
114 | 6 | new ComparatorVersion( |
|
115 | 6 | $this->greaterOrEqualTo, |
|
116 | 6 | $this->versionParser->parse($lowerVersionTokenList) |
|
117 | 6 | ), |
|
118 | 6 | $this->getUpperConstraint($upperVersionTokenList) |
|
119 | 6 | ); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Chunk a token list by the dash seperator, returning array of token lists omitting the seperator tokens. |
||
124 | * |
||
125 | * @param Token[] $tokenList |
||
126 | * @return Token[][] |
||
127 | */ |
||
128 | 11 | private function chunkByDash(array $tokenList) |
|
137 | |||
138 | /** |
||
139 | * Returns an array of token arrays, the first is tokens for the lower bound and the second is the upper bound. |
||
140 | * |
||
141 | * @param Token[][] $chunkedList |
||
142 | * |
||
143 | * @return Token[][] |
||
144 | */ |
||
145 | 11 | private function getSingleVersionTokens( |
|
180 | |||
181 | /** |
||
182 | * Returns true if the chunks match the configuration. |
||
183 | * |
||
184 | * @param Token[][] $chunkedList |
||
185 | * @param string[] $configuration |
||
186 | * |
||
187 | * @return boolean |
||
188 | */ |
||
189 | 11 | private function chunksMatchConfiguration( |
|
203 | |||
204 | /** |
||
205 | * Get the upper version constraint from a token list. |
||
206 | * |
||
207 | * @param Token[] $tokenList |
||
208 | * |
||
209 | * @return ComparatorVersion |
||
210 | */ |
||
211 | 6 | private function getUpperConstraint(array $tokenList) |
|
232 | } |
||
233 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.