1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sugarcrm\UpgradeSpec\Version; |
4
|
|
|
|
5
|
|
|
final class Version implements \Serializable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $version; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Version constructor. |
14
|
|
|
* |
15
|
|
|
* @param $version |
16
|
|
|
*/ |
17
|
|
|
public function __construct($version) |
18
|
|
|
{ |
19
|
|
|
if (!is_string($version) || !preg_match('/\d+(\.\d+){1,3}/', $version)) { |
20
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid SugarCRM version', $version)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->version = $version; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isMajor() |
30
|
|
|
{ |
31
|
|
|
return count(explode('.', $this->version)) == 2; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function isFull() |
38
|
|
|
{ |
39
|
|
|
return count(explode('.', $this->version)) == 4; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isMinor() |
46
|
|
|
{ |
47
|
|
|
return !$this->isMajor(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Version $version |
52
|
|
|
* |
53
|
|
|
* @param bool $strict |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function isEqualTo(Version $version, $strict = false) |
58
|
|
|
{ |
59
|
|
|
if ($strict) { |
60
|
|
|
return $this->version == (string) $version; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return (string) $this->getFull() == (string) $version->getFull(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks if the current version is a subversion of given parent |
68
|
|
|
* |
69
|
|
|
* @param Version $parent |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isChildOf(Version $parent) |
74
|
|
|
{ |
75
|
|
|
$parent = (string) $parent; |
|
|
|
|
76
|
|
|
|
77
|
|
|
// exactly the same version is not a child |
78
|
|
|
if ((string) $this === $parent) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return implode('.', array_slice(explode('.', $this->version), 0, count(explode('.', $parent)))) === $parent; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Version |
87
|
|
|
*/ |
88
|
|
|
public function getMajor() |
89
|
|
|
{ |
90
|
|
|
if ($this->isMajor()) { |
91
|
|
|
return clone $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return new self(implode('.', array_slice(explode('.', $this->version), 0, 2))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Version |
99
|
|
|
*/ |
100
|
|
|
public function getFull() |
101
|
|
|
{ |
102
|
|
|
$parts = explode('.', $this->version); |
103
|
|
|
if (count($parts) == 4) { |
104
|
|
|
return $this->version; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$parts = array_merge($parts, array_fill(0, 4 - count($parts), '0')); |
108
|
|
|
|
109
|
|
|
return new self(implode('.', $parts)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Gets all version aliases |
114
|
|
|
* 7.6.0 -> [7.6, 7.6.0, 7.6.0.0], 7.6.1 -> [7.6.1, 7.6.1.0] |
115
|
|
|
* |
116
|
|
|
* @return OrderedList |
117
|
|
|
*/ |
118
|
|
|
public function getAliases() |
119
|
|
|
{ |
120
|
|
|
if ($this->isMajor()) { |
121
|
|
|
return new OrderedList([$this, $this->version . '.0', $this->version . '.0.0']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$parts = explode('.', $this->version); |
125
|
|
|
if ($parts[count($parts) - 1] === '0') { |
126
|
|
|
return $this->getParent()->getAliases(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$aliases = [$this]; |
130
|
|
|
while (count($parts) < 4) { |
131
|
|
|
$parts[] = '0'; |
132
|
|
|
$aliases[] = implode('.', $parts); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new OrderedList($aliases); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* |
141
|
|
|
* @return Version |
142
|
|
|
*/ |
143
|
|
|
public function getParent() |
144
|
|
|
{ |
145
|
|
|
if ($this->isMajor()) { |
146
|
|
|
throw new \LogicException('Major version doesn\'t have parent'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$parts = explode('.', $this->version); |
150
|
|
|
array_pop($parts); |
151
|
|
|
|
152
|
|
|
return new self(implode('.', $parts)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* String representation of object. |
157
|
|
|
* |
158
|
|
|
* @return string the string representation of the object or null |
159
|
|
|
*/ |
160
|
|
|
public function serialize() |
161
|
|
|
{ |
162
|
|
|
return serialize($this->version); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Constructs the object. |
167
|
|
|
* |
168
|
|
|
* @param string $serialized |
169
|
|
|
*/ |
170
|
|
|
public function unserialize($serialized) |
171
|
|
|
{ |
172
|
|
|
$this->version = unserialize($serialized); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function __toString() |
179
|
|
|
{ |
180
|
|
|
return $this->version; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|