-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersion.php
188 lines (147 loc) · 4.13 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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Version;
use SonsOfPHP\Component\Version\Exception\VersionException;
use Stringable;
/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
final class Version implements VersionInterface, Stringable
{
private const REGEX = '/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';
private int $major;
private int $minor;
private int $patch;
private string $preRelease = '';
private string $build = '';
public function __construct(string $version)
{
if (0 === preg_match(self::REGEX, $version, $matches)) {
throw new VersionException('Invalid Version');
}
if (!isset($matches['major']) || !isset($matches['minor']) || !isset($matches['patch'])) {
throw new VersionException('Invalid Version');
}
$this->major = (int) $matches['major'];
$this->minor = (int) $matches['minor'];
$this->patch = (int) $matches['patch'];
if (isset($matches['prerelease'])) {
$this->preRelease = $matches['prerelease'];
}
if (isset($matches['buildmetadata'])) {
$this->build = $matches['buildmetadata'];
}
}
/**
* @see self::toString()
*/
public function __toString(): string
{
return $this->toString();
}
public static function from(string $version): VersionInterface
{
return new self($version);
}
public function toString(): string
{
$version = sprintf('%d.%d.%d', $this->getMajor(), $this->getMinor(), $this->getPatch());
if ('' !== $this->getPreRelease()) {
$version = $version . '-' . $this->getPreRelease();
}
if ('' !== $this->getBuild()) {
return $version . '+' . $this->getBuild();
}
return $version;
}
public function getMajor(): int
{
return $this->major;
}
public function getMinor(): int
{
return $this->minor;
}
public function getPatch(): int
{
return $this->patch;
}
public function getPreRelease(): ?string
{
return $this->preRelease;
}
public function getBuild(): ?string
{
return $this->build;
}
public function compare(VersionInterface $version): int
{
if ($this->getMajor() > $version->getMajor()) {
return 1;
}
if ($this->getMajor() < $version->getMajor()) {
return -1;
}
if ($this->getMinor() > $version->getMinor()) {
return 1;
}
if ($this->getMinor() < $version->getMinor()) {
return -1;
}
if ($this->getPatch() > $version->getPatch()) {
return 1;
}
if ($this->getPatch() < $version->getPatch()) {
return -1;
}
return 0;
}
public function isGreaterThan(VersionInterface $version): bool
{
return 1 === $this->compare($version);
}
public function isLessThan(VersionInterface $version): bool
{
return -1 === $this->compare($version);
}
public function isEqualTo(VersionInterface $version): bool
{
return 0 === $this->compare($version);
}
/**
* Bumps the patch version by one.
*
* @return static
*/
public function nextPatch(): VersionInterface
{
$ver = clone $this;
++$ver->patch;
return $ver;
}
/**
* Bumps the minor version by one.
*
* @return static
*/
public function nextMinor(): VersionInterface
{
$ver = clone $this;
$ver->patch = 0;
++$ver->minor;
return $ver;
}
/**
* Bumps the major version by one.
*
* @return static
*/
public function nextMajor(): VersionInterface
{
$ver = clone $this;
$ver->patch = 0;
$ver->minor = 0;
++$ver->major;
return $ver;
}
}