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