|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright (c) 2014-2017 brian ridley |
|
5
|
|
|
* @author brian ridley <[email protected]> |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ptlis\SemanticVersion\Parse; |
|
13
|
|
|
|
|
14
|
|
|
use ptlis\SemanticVersion\Parse\Matcher\RangeParserInterface; |
|
15
|
|
|
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parser accepting array of tokens and returning an array of comparators & versions. |
|
19
|
|
|
* |
|
20
|
|
|
* @todo Correctly validate versions |
|
21
|
|
|
*/ |
|
22
|
|
|
final class VersionParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var RangeParserInterface[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $rangeParserList; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param RangeParserInterface[] $rangeParserList |
|
34
|
|
|
*/ |
|
35
|
30 |
|
public function __construct(array $rangeParserList) |
|
36
|
|
|
{ |
|
37
|
30 |
|
$this->rangeParserList = $rangeParserList; |
|
38
|
30 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Parse a version range. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Token[] $tokenList |
|
44
|
|
|
* |
|
45
|
|
|
* @return VersionRangeInterface |
|
46
|
|
|
*/ |
|
47
|
30 |
|
public function parseRange(array $tokenList) |
|
48
|
|
|
{ |
|
49
|
30 |
|
$clusteredTokenList = $this->clusterTokens($tokenList); |
|
50
|
|
|
|
|
51
|
|
|
$operatorList = [ |
|
52
|
30 |
|
Token::LOGICAL_AND, |
|
53
|
|
|
Token::LOGICAL_OR |
|
54
|
30 |
|
]; |
|
55
|
|
|
|
|
56
|
30 |
|
$realResultList = []; |
|
57
|
30 |
|
foreach ($clusteredTokenList as $clusteredTokens) { |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
30 |
|
$parsed = null; |
|
60
|
30 |
|
foreach ($this->rangeParserList as $rangeParser) { |
|
61
|
30 |
|
if ($rangeParser->canParse($clusteredTokens)) { |
|
62
|
30 |
|
$parsed = $rangeParser->parse($clusteredTokens); |
|
63
|
30 |
|
break; |
|
64
|
|
|
} |
|
65
|
30 |
|
} |
|
66
|
|
|
|
|
67
|
30 |
|
if (is_null($parsed)) { |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
5 |
|
if (in_array($clusteredTokens[0]->getType(), $operatorList)) { |
|
70
|
5 |
|
$realResultList[] = $clusteredTokens[0]; |
|
71
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
|
73
|
5 |
|
} else { |
|
74
|
|
|
throw new \RuntimeException('Unable to parse version string'); |
|
75
|
|
|
} |
|
76
|
5 |
|
} else { |
|
77
|
30 |
|
$realResultList[] = $parsed; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
|
80
|
30 |
|
} |
|
81
|
|
|
|
|
82
|
30 |
|
$buildRange = new LogicalOperatorProcessor(); |
|
83
|
|
|
|
|
84
|
30 |
|
return $buildRange->run($realResultList); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Clusters the tokens, breaking them up upon finding a logical AND / OR. |
|
89
|
|
|
* |
|
90
|
|
|
* @param Token[] $tokenList |
|
91
|
|
|
* |
|
92
|
|
|
* @return Token[][] $tokenList |
|
93
|
|
|
*/ |
|
94
|
30 |
|
private function clusterTokens(array $tokenList) |
|
95
|
|
|
{ |
|
96
|
|
|
$comparatorTokenList = [ |
|
97
|
30 |
|
Token::LOGICAL_AND, |
|
98
|
|
|
Token::LOGICAL_OR |
|
99
|
30 |
|
]; |
|
100
|
|
|
|
|
101
|
30 |
|
$tokenClusterList = []; |
|
102
|
|
|
|
|
103
|
|
|
// Stores tokens not yet parcelled out |
|
104
|
30 |
|
$tokenAccumulator = []; |
|
105
|
30 |
|
$tokenListCount = count($tokenList); |
|
106
|
30 |
|
for ($i = 0; $i < $tokenListCount; $i++) { |
|
107
|
30 |
|
$currentToken = $tokenList[$i]; |
|
108
|
|
|
|
|
109
|
30 |
|
if (in_array($currentToken->getType(), $comparatorTokenList)) { |
|
110
|
5 |
|
$tokenClusterList[] = $tokenAccumulator; |
|
111
|
5 |
|
$tokenClusterList[] = [$currentToken]; |
|
112
|
5 |
|
$tokenAccumulator = []; |
|
113
|
|
|
|
|
|
|
|
|
|
114
|
5 |
|
} else { |
|
115
|
30 |
|
$tokenAccumulator[] = $currentToken; |
|
116
|
|
|
} |
|
117
|
30 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Add any remaining tokens |
|
120
|
30 |
|
if (count($tokenAccumulator)) { |
|
121
|
30 |
|
$tokenClusterList[] = $tokenAccumulator; |
|
122
|
30 |
|
} |
|
123
|
|
|
|
|
124
|
30 |
|
return $tokenClusterList; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|