1 | <?php |
||
19 | class SemanticVersion |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * RegEx pattern that matches the different version components. |
||
24 | * |
||
25 | * @since 0.1.0 |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | const VERSION_PATTERN = '/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z-]*))?(?:\+([0-9A-Za-z-]*))?$/'; |
||
30 | |||
31 | /** |
||
32 | * Version that is used. |
||
33 | * |
||
34 | * @since 0.1.0 |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $version; |
||
39 | |||
40 | /** |
||
41 | * Different components of the version that is used. |
||
42 | * |
||
43 | * @since 0.1.0 |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $components; |
||
48 | |||
49 | /** |
||
50 | * Instantiate a Version object. |
||
51 | * |
||
52 | * @since 0.1.0 |
||
53 | * |
||
54 | * @param string|null $version Optional. The version to use. Defaults to |
||
55 | * the current PHP interpreter's version. |
||
56 | * @param bool $partial Optional. Whether to accept a partial |
||
57 | * version number. If true, the missing |
||
58 | * components will default to `0` instead of |
||
59 | * throwing an exception. |
||
60 | * @throws RuntimeException When the version fails to validate. |
||
61 | */ |
||
62 | public function __construct($version = null, $partial = false) |
||
73 | |||
74 | /** |
||
75 | * Validate the version and assert it is in SemVer format. |
||
76 | * |
||
77 | * @since 0.1.0 |
||
78 | * |
||
79 | * @param string $version The version to validate. |
||
80 | * @param bool $partial Optional. Whether to accept a partial version |
||
81 | * number. If true, the missing components will |
||
82 | * default to `0` instead of throwing an exception. |
||
83 | * @return string |
||
84 | * @throws RuntimeException When the version fails to validate. |
||
85 | */ |
||
86 | protected function validate($version, $partial = false) |
||
119 | |||
120 | /** |
||
121 | * Get the version that is used. |
||
122 | * |
||
123 | * @since 0.1.0 |
||
124 | * |
||
125 | * @return string The version that is used. '0.0.0' if not defined. |
||
126 | */ |
||
127 | public function getVersion() |
||
131 | |||
132 | /** |
||
133 | * Build and return a versin from the separated components. |
||
134 | * |
||
135 | * @since 0.1.0 |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | protected function getVersionFromComponents() |
||
156 | |||
157 | /** |
||
158 | * Get the major version number. |
||
159 | * |
||
160 | * @since 0.1.0 |
||
161 | * |
||
162 | * @return int The major version that is used. 0 if not defined. |
||
163 | */ |
||
164 | public function getMajor() |
||
168 | |||
169 | /** |
||
170 | * Get the minor version number. |
||
171 | * |
||
172 | * @since 0.1.0 |
||
173 | * |
||
174 | * @return int The minor version that is used. 0 if not defined. |
||
175 | */ |
||
176 | public function getMinor() |
||
180 | |||
181 | /** |
||
182 | * Get the patch version number. |
||
183 | * |
||
184 | * @since 0.1.0 |
||
185 | * |
||
186 | * @return int The patch version that is used. 0 if not defined. |
||
187 | */ |
||
188 | public function getPatch() |
||
192 | |||
193 | /** |
||
194 | * Get the pre-release label. |
||
195 | * |
||
196 | * @since 0.1.0 |
||
197 | * |
||
198 | * @return int The patch version that is used. '' if not defined. |
||
199 | */ |
||
200 | public function getPreRelease() |
||
204 | |||
205 | /** |
||
206 | * Get the build metadata. |
||
207 | * |
||
208 | * @since 0.1.0 |
||
209 | * |
||
210 | * @return int The build metadata for the version that is used. '' if not |
||
211 | * defined. |
||
212 | */ |
||
213 | public function getBuild() |
||
217 | |||
218 | /** |
||
219 | * Get a component of the version. |
||
220 | * |
||
221 | * @since 0.1.0 |
||
222 | * |
||
223 | * @param string $level What level of component to get. Possible values: |
||
224 | * 'major', 'minor', 'patch' |
||
225 | * @return int The requested version component. null if not defined. |
||
226 | */ |
||
227 | protected function getComponent($level) |
||
233 | |||
234 | /** |
||
235 | * Set a component of the version. |
||
236 | * |
||
237 | * @since 0.1.0 |
||
238 | * |
||
239 | * @param string $level What level of component to set. Possible values: |
||
240 | * 'major', 'minor', 'patch' |
||
241 | * @param int $version What version to set that component to. |
||
242 | */ |
||
243 | protected function setComponent($level, $version) |
||
247 | |||
248 | /** |
||
249 | * Get a string representation of the object. |
||
250 | * |
||
251 | * @since 0.2.0 |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function __toString() |
||
259 | } |
||
260 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: