|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage\Migration; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Storage\Exception\VersionException; |
|
6
|
|
|
|
|
7
|
|
|
final class Version |
|
8
|
|
|
{ |
|
9
|
|
|
private $major; |
|
10
|
|
|
private $minor; |
|
11
|
|
|
private $patch; |
|
12
|
|
|
|
|
13
|
15 |
|
public function __construct(int $major, int $minor, int $patch) |
|
14
|
|
|
{ |
|
15
|
15 |
|
$this->major = $major; |
|
16
|
15 |
|
$this->minor = $minor; |
|
17
|
15 |
|
$this->patch = $patch; |
|
18
|
15 |
|
} |
|
19
|
|
|
|
|
20
|
2 |
|
public function getMajor(): int |
|
21
|
|
|
{ |
|
22
|
2 |
|
return $this->major; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public function getMinor(): int |
|
26
|
|
|
{ |
|
27
|
2 |
|
return $this->minor; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
public function getPatch(): int |
|
31
|
|
|
{ |
|
32
|
2 |
|
return $this->patch; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
7 |
|
public function compare(Version $version): int |
|
36
|
|
|
{ |
|
37
|
7 |
|
if ($version->major > $this->major) { |
|
38
|
2 |
|
return 1; |
|
39
|
7 |
|
} else if ($this->major > $version->major) { |
|
40
|
2 |
|
return -1; |
|
41
|
7 |
|
} else if ($version->minor > $this->minor) { |
|
42
|
5 |
|
return 1; |
|
43
|
7 |
|
} else if ($this->minor > $version->minor) { |
|
44
|
5 |
|
return -1; |
|
45
|
6 |
|
} else if ($version->patch > $this->patch) { |
|
46
|
|
|
return 1; |
|
47
|
6 |
|
} else if ($this->patch > $version->patch) { |
|
48
|
|
|
return -1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
return 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function lowerThan(Version $version): bool |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->compare($version) === 1; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function lowerOrEquals(Version $version): bool |
|
60
|
|
|
{ |
|
61
|
2 |
|
return $this->compare($version) >= 0; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
public function greaterThan(Version $version): bool |
|
65
|
|
|
{ |
|
66
|
3 |
|
return $this->compare($version) === -1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function greaterOrEquals(Version $version): bool |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->compare($version) <= 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function equals(Version $version): bool |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->compare($version) === 0; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function equalsLiteral(string $version): bool |
|
80
|
|
|
{ |
|
81
|
2 |
|
return (string) $this === $version; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function getNextPatch(): self |
|
85
|
|
|
{ |
|
86
|
1 |
|
$instance = clone $this; |
|
87
|
1 |
|
$instance->patch++; |
|
88
|
|
|
|
|
89
|
1 |
|
return $instance; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function getNextMinor(): self |
|
93
|
|
|
{ |
|
94
|
1 |
|
$instance = clone $this; |
|
95
|
1 |
|
$instance->minor++; |
|
96
|
|
|
|
|
97
|
1 |
|
return $instance; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function getNextMajor(): self |
|
101
|
|
|
{ |
|
102
|
1 |
|
$instance = clone $this; |
|
103
|
1 |
|
$instance->major++; |
|
104
|
|
|
|
|
105
|
1 |
|
return $instance; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
8 |
|
public static function fromString(string $version): self |
|
109
|
|
|
{ |
|
110
|
8 |
|
$version = explode('.', $version); |
|
111
|
8 |
|
if (count($version) !== 3) { |
|
112
|
|
|
throw VersionException::forParseError(implode('.', $version)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
8 |
|
return new self((int) $version[0], (int) $version[1], (int) $version[2]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
public function __toString(): string |
|
119
|
|
|
{ |
|
120
|
5 |
|
return "{$this->major}.{$this->minor}.{$this->patch}"; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|