1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of semver/semver. |
5
|
|
|
* |
6
|
|
|
* (c) SemVer <https://github.com/git-pull-request> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace SemVer\SemVer; |
15
|
|
|
|
16
|
|
|
use SemVer\SemVer\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Definition of a Version according to Semantic Versioning 2.0.0. |
20
|
|
|
* |
21
|
|
|
* @link http://semver.org/ |
22
|
|
|
*/ |
23
|
|
|
final class Version |
24
|
|
|
{ |
25
|
|
|
/** @var string */ |
26
|
|
|
private $build; |
27
|
|
|
/** @var int */ |
28
|
|
|
private $major; |
29
|
|
|
/** @var int */ |
30
|
|
|
private $minor; |
31
|
|
|
/** @var int */ |
32
|
|
|
private $patch; |
33
|
|
|
/** @var string */ |
34
|
|
|
private $preRelease; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $major |
38
|
|
|
* @param int $minor |
39
|
|
|
* @param int $patch |
40
|
|
|
* @param string $preRelease |
41
|
|
|
* @param string $build |
42
|
|
|
* |
43
|
|
|
* @throws \SemVer\SemVer\Exception\InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function __construct(int $major, int $minor, int $patch, string $preRelease = '', string $build = '') |
46
|
|
|
{ |
47
|
|
|
$this->major = $major; |
48
|
|
|
$this->minor = $minor; |
49
|
|
|
$this->patch = $patch; |
50
|
|
|
$this->preRelease = $preRelease; |
51
|
|
|
$this->build = $build; |
52
|
|
|
|
53
|
|
|
if ('' !== $this->preRelease && !preg_match('#^([0-9A-Za-z-]+\.)*[0-9A-Za-z-]+$#', $this->preRelease)) { |
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
'The pre-release version is not compatible with rule 9 of the specifications.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ('' !== $this->build && !preg_match('#^([0-9A-Za-z-]+\.)*[0-9A-Za-z-]+$#', $this->build)) { |
60
|
|
|
throw new InvalidArgumentException( |
61
|
|
|
'The build version is not compatible with rule 10 of the specifications.' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @return string */ |
67
|
|
|
public function __toString() : string |
68
|
|
|
{ |
69
|
|
|
$str = sprintf('%d.%d.%d', $this->major, $this->minor, $this->patch); |
70
|
|
|
if ('' !== $this->preRelease) { |
71
|
|
|
$str .= '-'.$this->preRelease; |
72
|
|
|
} |
73
|
|
|
if ('' !== $this->build) { |
74
|
|
|
$str .= '+'.$this->build; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $str; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $version |
82
|
|
|
* |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
* |
85
|
|
|
* @return Version |
86
|
|
|
*/ |
87
|
|
|
public static function fromString(string $version) : Version |
88
|
|
|
{ |
89
|
|
|
$patternMajor = '(?P<major>[0-9]+)'; |
90
|
|
|
$patternMinor = '(?P<minor>[0-9]+)'; |
91
|
|
|
$patternPatch = '(?P<patch>[0-9]+)'; |
92
|
|
|
$patternPreRelease = '(?:-(?P<prerelease>(?:[0-9A-Za-z-]+\.)*(?:[0-9A-Za-z-]+)))?'; |
93
|
|
|
$patternBuild = '(?:\+(?P<build>(?:[0-9A-Za-z-]+\.)*(?:[0-9A-Za-z-]+)))?'; |
94
|
|
|
|
95
|
|
|
$pattern = '#^'.$patternMajor.'\.'.$patternMinor.'\.'.$patternPatch.$patternPreRelease.$patternBuild.'$#'; |
96
|
|
|
if (!preg_match($pattern, $version, $matches)) { |
97
|
|
|
throw new InvalidArgumentException(sprintf('The string "%s" does not look like a version.', $version)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new static( |
101
|
|
|
(int) $matches['major'], |
102
|
|
|
(int) $matches['minor'], |
103
|
|
|
(int) $matches['patch'], |
104
|
|
|
$matches['prerelease'] ?? '', |
105
|
|
|
$matches['build'] ?? '' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $versions |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public static function sort(array $versions) : array |
115
|
|
|
{ |
116
|
|
|
usort( |
117
|
|
|
$versions, |
118
|
|
|
function (Version $a, Version $b) { |
119
|
|
|
return VersionComparator::compare($a, $b); |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $versions; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function getMajor() : int |
130
|
|
|
{ |
131
|
|
|
return $this->major; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
public function getMinor() : int |
138
|
|
|
{ |
139
|
|
|
return $this->minor; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
|
|
public function getPatch() : int |
146
|
|
|
{ |
147
|
|
|
return $this->patch; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getPreRelease() : string |
154
|
|
|
{ |
155
|
|
|
return $this->preRelease; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getBuild() : string |
162
|
|
|
{ |
163
|
|
|
return $this->build; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Version $other |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function equals(Version $other) : bool |
172
|
|
|
{ |
173
|
|
|
return 0 === VersionComparator::compare($this, $other); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Version $other |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function greaterThan(Version $other) : bool |
182
|
|
|
{ |
183
|
|
|
return 1 === VersionComparator::compare($this, $other); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param Version $other |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function greaterThanOrEqual(Version $other) : bool |
192
|
|
|
{ |
193
|
|
|
return 0 <= VersionComparator::compare($this, $other); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param Version $other |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function lessThan(Version $other) : bool |
202
|
|
|
{ |
203
|
|
|
return -1 === VersionComparator::compare($this, $other); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param Version $other |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function lessThanOrEqual(Version $other) : bool |
212
|
|
|
{ |
213
|
|
|
return 0 >= VersionComparator::compare($this, $other); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|