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\RangeParser; |
13
|
|
|
|
14
|
|
|
use ptlis\SemanticVersion\Comparator\ComparatorInterface; |
15
|
|
|
use ptlis\SemanticVersion\Parse\Token; |
16
|
|
|
use ptlis\SemanticVersion\Parse\VersionParser; |
17
|
|
|
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface; |
18
|
|
|
use ptlis\SemanticVersion\VersionRange\LogicalAnd; |
19
|
|
|
use ptlis\SemanticVersion\VersionRange\ComparatorVersion; |
20
|
|
|
use ptlis\SemanticVersion\Version\Version; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Parser for caret ranges. |
24
|
|
|
* |
25
|
|
|
* Behaviour of caret ranges is described @ https://getcomposer.org/doc/articles/versions.md#caret |
26
|
|
|
*/ |
27
|
|
|
final class CaretRangeParser implements RangeParserInterface |
28
|
|
|
{ |
29
|
|
|
/** @var VersionParser */ |
30
|
|
|
private $versionParser; |
31
|
|
|
|
32
|
|
|
/** @var ComparatorInterface */ |
33
|
|
|
private $greaterOrEqualTo; |
34
|
|
|
|
35
|
|
|
/** @var ComparatorInterface */ |
36
|
|
|
private $lessThan; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param VersionParser $versionParser |
43
|
|
|
* @param ComparatorInterface $greaterOrEqualTo |
44
|
|
|
* @param ComparatorInterface $lessThan |
45
|
|
|
*/ |
46
|
4 |
|
public function __construct( |
47
|
|
|
VersionParser $versionParser, |
48
|
|
|
ComparatorInterface $greaterOrEqualTo, |
49
|
|
|
ComparatorInterface $lessThan |
50
|
|
|
) { |
51
|
4 |
|
$this->versionParser = $versionParser; |
52
|
4 |
|
$this->greaterOrEqualTo = $greaterOrEqualTo; |
53
|
4 |
|
$this->lessThan = $lessThan; |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns true if the provided tokens represent a caret range. |
58
|
|
|
* |
59
|
|
|
* @param Token[] $tokenList |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
4 |
|
public function canParse(array $tokenList) |
64
|
|
|
{ |
65
|
|
|
return ( |
66
|
4 |
|
count($tokenList) > 0 |
67
|
4 |
|
&& Token::CARET_RANGE === $tokenList[0]->getType() |
68
|
4 |
|
&& $this->versionParser->canParse(array_slice($tokenList, 1)) |
69
|
4 |
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build a ComparatorVersion representing the caret range. |
74
|
|
|
* |
75
|
|
|
* @param Token[] $tokenList |
76
|
|
|
* |
77
|
|
|
* @return VersionRangeInterface |
78
|
|
|
*/ |
79
|
4 |
|
public function parse(array $tokenList) |
80
|
|
|
{ |
81
|
4 |
|
if (!$this->canParse($tokenList)) { |
82
|
1 |
|
throw new \RuntimeException('Invalid caret range (^) version range'); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
$lowerVersion = $this->versionParser->parse(array_slice($tokenList, 1)); |
86
|
|
|
|
87
|
3 |
|
return new LogicalAnd( |
88
|
3 |
|
new ComparatorVersion( |
89
|
3 |
|
$this->greaterOrEqualTo, |
90
|
|
|
$lowerVersion |
91
|
3 |
|
), |
92
|
3 |
|
new ComparatorVersion( |
93
|
3 |
|
$this->lessThan, |
94
|
3 |
|
new Version($lowerVersion->getMajor() + 1) |
95
|
3 |
|
) |
96
|
3 |
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|