1 | <?php |
||
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 | 66 | public function __construct($version = null, $partial = false) |
|
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 | 64 | public function getVersion() |
|
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 | 66 | public function getMajor() |
|
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 | 66 | public function getMinor() |
|
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 | 66 | public function getPatch() |
|
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 | 66 | public function getPreRelease() |
|
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 | 66 | public function getBuild() |
|
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() |
||
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 | 66 | protected function validate($version, $partial = false) |
|
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 | 66 | protected function getVersionFromComponents() |
|
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 | 66 | protected function getComponent($level) |
|
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 | 66 | protected function setComponent($level, $version) |
|
259 | } |
||
260 |