1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami\Stdlib; |
5
|
|
|
|
6
|
|
|
use Selami\Stdlib\Exception\InvalidSemverPatternException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Semver |
10
|
|
|
* Semantic Versioning 2.0.0 compatible version handling |
11
|
|
|
* @package Selami\Stdlib |
12
|
|
|
*/ |
13
|
|
|
final class Semver |
14
|
|
|
{ |
15
|
|
|
private static $semverPattern = '/(\d+).(\d+).(\d+)(|[-.+](?:dev|alpha|beta|rc|stable)(.*?))$/i'; |
16
|
|
|
private $major; |
17
|
|
|
private $minor; |
18
|
|
|
private $patch; |
19
|
|
|
private $preRelease; |
20
|
|
|
|
21
|
|
|
private function __construct(int $major, int $minor, int $patch, ?string $preRelase = null) |
22
|
|
|
{ |
23
|
|
|
$this->major = $major; |
24
|
|
|
$this->minor = $minor; |
25
|
|
|
$this->patch = $patch; |
26
|
|
|
$this->preRelease = $preRelase; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function createFromString(string $version) : self |
30
|
|
|
{ |
31
|
|
|
$preRelease = null; |
32
|
|
|
if (!preg_match(self::$semverPattern, $version, $match)) { |
33
|
|
|
throw new InvalidSemverPatternException( |
34
|
|
|
sprintf('%s is not valid semver compatible version name', $version) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
[,$major, $minor, $patch, $preRelease] = $match; |
|
|
|
|
38
|
|
|
if ($preRelease === '') { |
39
|
|
|
$preRelease = null; |
40
|
|
|
} |
41
|
|
|
return new self((int) $major, (int) $minor, (int) $patch, $preRelease); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCurrent() : string |
45
|
|
|
{ |
46
|
|
|
return $this->major . '.' |
47
|
|
|
. $this->minor . '.' |
48
|
|
|
. $this->patch |
49
|
|
|
. $this->preRelease; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getNextPatchRelease() : string |
53
|
|
|
{ |
54
|
|
|
return $this->major . '.' . $this->minor . '.' . (++$this->patch); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getNextMinorRelease() : string |
58
|
|
|
{ |
59
|
|
|
return $this->major . '.' . (++$this->minor) . '.' . $this->patch; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getNextMajorRelease() : string |
63
|
|
|
{ |
64
|
|
|
if ($this->preRelease === null) { |
65
|
|
|
$this->major++; |
66
|
|
|
} |
67
|
|
|
return $this->major . '.' |
68
|
|
|
. $this->minor . '.' |
69
|
|
|
. $this->patch; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.