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) |
|
63 | { |
||
64 | |||
65 | 66 | if (null === $version) { |
|
66 | $version = '0.0.0'; |
||
67 | } |
||
68 | |||
69 | 66 | $version = $this->validate($version, $partial); |
|
70 | |||
71 | 66 | $this->version = $version; |
|
72 | 66 | } |
|
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) |
|
171 | { |
||
172 | |||
173 | 66 | $version = trim($version); |
|
174 | 66 | $pattern = self::VERSION_PATTERN; |
|
175 | |||
176 | 66 | $components = array(); |
|
177 | 66 | $result = preg_match($pattern, $version, $components); |
|
178 | |||
179 | 66 | if ( ! $result) { |
|
180 | throw new RuntimeException(sprintf( |
||
181 | 'Failed to validate version "%1$s".', |
||
182 | (string)$version |
||
183 | )); |
||
184 | } |
||
185 | |||
186 | 66 | if ( ! $partial && ( ! isset($components[2]) || ! isset($components[3]))) { |
|
187 | throw new RuntimeException(sprintf( |
||
188 | 'Could not accept partial version "%1$s", requested full versions only.', |
||
189 | (string)$version |
||
190 | )); |
||
191 | } |
||
192 | |||
193 | 66 | $this->setComponent('major', isset($components[1]) ? (int)$components[1] : 0); |
|
194 | 66 | $this->setComponent('minor', isset($components[2]) ? (int)$components[2] : 0); |
|
195 | 66 | $this->setComponent('patch', isset($components[3]) ? (int)$components[3] : 0); |
|
196 | 66 | $this->setComponent('pre-release', isset($components[4]) ? (string)$components[4] : ''); |
|
197 | 66 | $this->setComponent('build', isset($components[5]) ? (string)$components[5] : ''); |
|
198 | |||
199 | 66 | $version = $this->getVersionFromComponents(); |
|
200 | |||
201 | 66 | 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 | 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 |