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