1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Selami\Stdlib; |
6
|
|
|
|
7
|
|
|
use Selami\Stdlib\Exception\InvalidSemverPatternException; |
8
|
|
|
|
9
|
|
|
use function preg_match; |
10
|
|
|
use function sprintf; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Semantic Versioning 2.0.0 compatible version handling |
14
|
|
|
*/ |
15
|
|
|
final class Semver |
16
|
|
|
{ |
17
|
|
|
private static $semverPattern = '/(\d+).(\d+).(\d+)(|[-.+](?:dev|alpha|beta|rc|stable)(.*?))$/i'; |
18
|
|
|
private $major; |
19
|
|
|
private $minor; |
20
|
|
|
private $patch; |
21
|
|
|
private $preRelease; |
22
|
|
|
|
23
|
|
|
private function __construct(int $major, int $minor, int $patch, ?string $preRelase = null) |
24
|
|
|
{ |
25
|
|
|
$this->major = $major; |
26
|
|
|
$this->minor = $minor; |
27
|
|
|
$this->patch = $patch; |
28
|
|
|
$this->preRelease = $preRelase; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function createFromString(string $version): self |
32
|
|
|
{ |
33
|
|
|
$preRelease = null; |
34
|
|
|
if (! preg_match(self::$semverPattern, $version, $match)) { |
35
|
|
|
throw new InvalidSemverPatternException( |
36
|
|
|
sprintf('%s is not valid semver compatible version name', $version) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
[, $major, $minor, $patch, $preRelease] = $match; |
|
|
|
|
41
|
|
|
if ($preRelease === '') { |
42
|
|
|
$preRelease = null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return new self((int) $major, (int) $minor, (int) $patch, $preRelease); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getCurrent(): string |
49
|
|
|
{ |
50
|
|
|
return $this->major . '.' |
51
|
|
|
. $this->minor . '.' |
52
|
|
|
. $this->patch |
53
|
|
|
. $this->preRelease; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getNextPatchRelease(): string |
57
|
|
|
{ |
58
|
|
|
$patch = $this->patch; |
59
|
|
|
if ($this->preRelease === null) { |
60
|
|
|
$patch++; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->major . '.' . $this->minor . '.' . $patch; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getNextMinorRelease(): string |
67
|
|
|
{ |
68
|
|
|
$minor = $this->minor; |
69
|
|
|
$patch = $this->patch; |
70
|
|
|
if ($this->preRelease === null) { |
71
|
|
|
$minor++; |
72
|
|
|
|
73
|
|
|
$patch = 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->major . '.' . $minor . '.' . $patch; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getNextMajorRelease(): string |
80
|
|
|
{ |
81
|
|
|
$major = $this->major; |
82
|
|
|
$minor = $this->minor; |
83
|
|
|
$patch = $this->patch; |
84
|
|
|
if ($this->preRelease === null) { |
85
|
|
|
$major++; |
86
|
|
|
$minor = 0; |
87
|
|
|
$patch = 0; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $major . '.' . $minor . '.' . $patch; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.