|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPFeature_SemanticVersion Class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package brightnucleus/phpfeature |
|
6
|
|
|
* @author Alain Schlesser <[email protected]> |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @link http://www.brightnucleus.com/ |
|
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PHPFeature_SemanticVersion. |
|
14
|
|
|
* |
|
15
|
|
|
* This class implements the semantic versioning logic and enables comparison between version strings. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 0.1.0 |
|
18
|
|
|
* |
|
19
|
|
|
* @author Alain Schlesser <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class PHPFeature_SemanticVersion |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* RegEx pattern that matches the different version components. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 0.1.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const VERSION_PATTERN = '/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z-]*))?(?:\+([0-9A-Za-z-]*))?$/'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Version that is used. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 0.1.0 |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $version; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Different components of the version that is used. |
|
44
|
|
|
* |
|
45
|
|
|
* @since 0.1.0 |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $components; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Instantiate a PHPFeature_SemanticVersion object. |
|
53
|
|
|
* |
|
54
|
|
|
* @since 0.1.0 |
|
55
|
|
|
* |
|
56
|
|
|
* @param string|null $version Optional. The version to use. Defaults to the current PHP interpreter's version. |
|
57
|
|
|
* @param bool $partial Optional. Whether to accept a partial version number. If true, the missing |
|
58
|
|
|
* components will default to `0` instead of throwing an exception. |
|
59
|
|
|
* |
|
60
|
|
|
* @throws RuntimeException When the version fails to validate. |
|
61
|
|
|
*/ |
|
62
|
89 |
|
public function __construct($version = null, $partial = false) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
89 |
|
if (null === $version) { |
|
66
|
2 |
|
$version = '0.0.0'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
89 |
|
$version = $this->validate($version, $partial); |
|
70
|
|
|
|
|
71
|
76 |
|
$this->version = $version; |
|
72
|
76 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the version that is used. |
|
76
|
|
|
* |
|
77
|
|
|
* @since 0.1.0 |
|
78
|
|
|
* |
|
79
|
|
|
* @return string The version that is used. '0.0.0' if not defined. |
|
80
|
|
|
*/ |
|
81
|
44 |
|
public function getVersion() |
|
82
|
|
|
{ |
|
83
|
44 |
|
return (string)isset($this->version) ? $this->version : '0.0.0'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the major version number. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 0.1.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @return int The major version that is used. 0 if not defined. |
|
92
|
|
|
*/ |
|
93
|
76 |
|
public function getMajor() |
|
94
|
|
|
{ |
|
95
|
76 |
|
return (int)$this->getComponent('major') ?: 0; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the minor version number. |
|
100
|
|
|
* |
|
101
|
|
|
* @since 0.1.0 |
|
102
|
|
|
* |
|
103
|
|
|
* @return int The minor version that is used. 0 if not defined. |
|
104
|
|
|
*/ |
|
105
|
76 |
|
public function getMinor() |
|
106
|
|
|
{ |
|
107
|
76 |
|
return (int)$this->getComponent('minor') ?: 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the patch version number. |
|
112
|
|
|
* |
|
113
|
|
|
* @since 0.1.0 |
|
114
|
|
|
* |
|
115
|
|
|
* @return int The patch version that is used. 0 if not defined. |
|
116
|
|
|
*/ |
|
117
|
76 |
|
public function getPatch() |
|
118
|
|
|
{ |
|
119
|
76 |
|
return (int)$this->getComponent('patch') ?: 0; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the pre-release label. |
|
124
|
|
|
* |
|
125
|
|
|
* @since 0.1.0 |
|
126
|
|
|
* |
|
127
|
|
|
* @return string The patch version that is used. Empty string if not defined. |
|
128
|
|
|
*/ |
|
129
|
76 |
|
public function getPreRelease() |
|
130
|
|
|
{ |
|
131
|
76 |
|
return (string)$this->getComponent('pre-release') ?: ''; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the build metadata. |
|
136
|
|
|
* |
|
137
|
|
|
* @since 0.1.0 |
|
138
|
|
|
* |
|
139
|
|
|
* @return string The build metadata for the version that is used. Empty string if not defined. |
|
140
|
|
|
*/ |
|
141
|
76 |
|
public function getBuild() |
|
142
|
|
|
{ |
|
143
|
76 |
|
return (string)$this->getComponent('build') ?: ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get a string representation of the object. |
|
148
|
|
|
* |
|
149
|
|
|
* @since 0.2.0 |
|
150
|
|
|
* |
|
151
|
|
|
* @return string String representation of the version. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function __toString() |
|
154
|
|
|
{ |
|
155
|
|
|
return (string)$this->getVersion(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Validate the version and assert it is in SemVer format. |
|
160
|
|
|
* |
|
161
|
|
|
* @since 0.1.0 |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $version The version to validate. |
|
164
|
|
|
* @param bool $partial Optional. Whether to accept a partial version number. If true, the missing components |
|
165
|
|
|
* will default to `0` instead of throwing an exception. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string Validated version string. |
|
168
|
|
|
* @throws RuntimeException When the version fails to validate. |
|
169
|
|
|
*/ |
|
170
|
89 |
|
protected function validate($version, $partial = false) |
|
171
|
|
|
{ |
|
172
|
|
|
|
|
173
|
89 |
|
$version = trim($version); |
|
174
|
89 |
|
$pattern = self::VERSION_PATTERN; |
|
175
|
|
|
|
|
176
|
89 |
|
$components = array(); |
|
177
|
89 |
|
$result = preg_match($pattern, $version, $components); |
|
178
|
|
|
|
|
179
|
89 |
|
if ( ! $result) { |
|
180
|
10 |
|
throw new RuntimeException(sprintf( |
|
181
|
10 |
|
'Failed to validate version "%1$s".', |
|
182
|
10 |
|
(string)$version |
|
183
|
|
|
)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
79 |
|
if ( ! $partial && ( ! isset($components[2]) || ! isset($components[3]))) { |
|
187
|
3 |
|
throw new RuntimeException(sprintf( |
|
188
|
3 |
|
'Could not accept partial version "%1$s", requested full versions only.', |
|
189
|
3 |
|
(string)$version |
|
190
|
|
|
)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
76 |
|
$this->setComponent('major', isset($components[1]) ? (int)$components[1] : 0); |
|
194
|
76 |
|
$this->setComponent('minor', isset($components[2]) ? (int)$components[2] : 0); |
|
195
|
76 |
|
$this->setComponent('patch', isset($components[3]) ? (int)$components[3] : 0); |
|
196
|
76 |
|
$this->setComponent('pre-release', isset($components[4]) ? (string)$components[4] : ''); |
|
197
|
76 |
|
$this->setComponent('build', isset($components[5]) ? (string)$components[5] : ''); |
|
198
|
|
|
|
|
199
|
76 |
|
$version = $this->getVersionFromComponents(); |
|
200
|
|
|
|
|
201
|
76 |
|
return $version; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Build and return a version from the separated components. |
|
206
|
|
|
* |
|
207
|
|
|
* @since 0.1.0 |
|
208
|
|
|
* |
|
209
|
|
|
* @return string String representation of the component's version. |
|
210
|
|
|
*/ |
|
211
|
76 |
|
protected function getVersionFromComponents() |
|
212
|
|
|
{ |
|
213
|
|
|
|
|
214
|
76 |
|
$pre_release = $this->getPreRelease() ? '-' . $this->getPreRelease() : ''; |
|
215
|
76 |
|
$build = $this->getBuild() ? '+' . $this->getBuild() : ''; |
|
216
|
|
|
|
|
217
|
76 |
|
$version = sprintf( |
|
218
|
76 |
|
'%1$s.%2$s.%3$s%4$s%5$s', |
|
219
|
76 |
|
$this->getMajor(), |
|
220
|
76 |
|
$this->getMinor(), |
|
221
|
76 |
|
$this->getPatch(), |
|
222
|
|
|
$pre_release, |
|
223
|
|
|
$build |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
76 |
|
return $version; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get a component of the version. |
|
231
|
|
|
* |
|
232
|
|
|
* @since 0.1.0 |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $level What level of component to get. |
|
235
|
|
|
* Possible values: 'major', 'minor', 'patch' |
|
236
|
|
|
* |
|
237
|
|
|
* @return int The requested version component. null if not defined. |
|
238
|
|
|
*/ |
|
239
|
76 |
|
protected function getComponent($level) |
|
240
|
|
|
{ |
|
241
|
76 |
|
return array_key_exists($level, $this->components) |
|
242
|
76 |
|
? $this->components[$level] |
|
243
|
76 |
|
: null; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Set a component of the version. |
|
248
|
|
|
* |
|
249
|
|
|
* @since 0.1.0 |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $level What level of component to set. |
|
252
|
|
|
* Possible values: 'major', 'minor', 'patch' |
|
253
|
|
|
* @param int $version What version to set that component to. |
|
254
|
|
|
*/ |
|
255
|
76 |
|
protected function setComponent($level, $version) |
|
256
|
|
|
{ |
|
257
|
76 |
|
$this->components[$level] = $version; |
|
258
|
76 |
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|