@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * PHPFeature Class |
|
4 | - * |
|
5 | - * @package phpfeature |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * PHPFeature Class |
|
4 | + * |
|
5 | + * @package phpfeature |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Class PHPFeature |
@@ -19,247 +19,247 @@ discard block |
||
19 | 19 | class PHPFeature implements FeatureInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * RegEx pattern that matches the comparison string. |
|
24 | - * |
|
25 | - * @since 0.1.0 |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - const COMPARISON_PATTERN = '/^(?:(<=|lt|<|le|>=|gt|>|ge|=|==|eq|!=|<>|ne))([0-9].*)$/'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Reference to the Configuration object. |
|
33 | - * |
|
34 | - * @since 0.1.0 |
|
35 | - * |
|
36 | - * @var ConfigInterface |
|
37 | - */ |
|
38 | - protected $config; |
|
39 | - |
|
40 | - /** |
|
41 | - * Reference to the Version object. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * |
|
45 | - * @var SemanticVersion |
|
46 | - */ |
|
47 | - protected $version; |
|
48 | - |
|
49 | - /** |
|
50 | - * Instantiate a PHPFeature object. |
|
51 | - * |
|
52 | - * @since 0.1.0 |
|
53 | - * |
|
54 | - * @param SemanticVersion|string|int|null $phpVersion Version of PHP to |
|
55 | - * check the features |
|
56 | - * for. |
|
57 | - * @param ConfigInterface|null $config Configuration that |
|
58 | - * contains the known |
|
59 | - * features. |
|
60 | - * @throws RuntimeException If the PHP version could not be validated. |
|
61 | - */ |
|
62 | - public function __construct($phpVersion = null, ConfigInterface $config = null) |
|
63 | - { |
|
64 | - |
|
65 | - // TODO: Better way to bootstrap this while still allowing DI? |
|
66 | - if ( ! $config) { |
|
67 | - $config = new Config(include(__DIR__ . '/../config/known_features.php')); |
|
68 | - } |
|
69 | - |
|
70 | - $this->config = $config; |
|
71 | - |
|
72 | - if (null === $phpVersion) { |
|
73 | - $phpVersion = phpversion(); |
|
74 | - } |
|
75 | - |
|
76 | - if (is_integer($phpVersion)) { |
|
77 | - $phpVersion = (string)$phpVersion; |
|
78 | - } |
|
79 | - |
|
80 | - if (is_string($phpVersion)) { |
|
81 | - $phpVersion = new SemanticVersion($phpVersion, true); |
|
82 | - } |
|
83 | - |
|
84 | - $this->version = $phpVersion; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Check whether a feature or a collection of features is supported. |
|
89 | - * |
|
90 | - * Accepts either a string or an array of strings. Returns true if all the |
|
91 | - * passed-in features are supported, or false if at least one of them is |
|
92 | - * not. |
|
93 | - * |
|
94 | - * @since 0.1.0 |
|
95 | - * |
|
96 | - * @param string|array $features What features to check the support of. |
|
97 | - * @return bool |
|
98 | - * @throws InvalidArgumentException If the wrong type of argument is passed |
|
99 | - * in. |
|
100 | - * @throws RuntimeException If a requirement could not be parsed. |
|
101 | - */ |
|
102 | - public function isSupported($features) |
|
103 | - { |
|
104 | - |
|
105 | - if (is_string($features)) { |
|
106 | - $features = array($features); |
|
107 | - } |
|
108 | - |
|
109 | - if ( ! is_array($features)) { |
|
110 | - throw new InvalidArgumentException(sprintf( |
|
111 | - 'Wrong type of argument passed in to is_supported(): "%1$s".', |
|
112 | - gettype($features) |
|
113 | - )); |
|
114 | - } |
|
115 | - |
|
116 | - $isSupported = true; |
|
117 | - |
|
118 | - while ($isSupported && count($features) > 0) { |
|
119 | - $feature = array_pop($features); |
|
120 | - $isSupported &= (bool)$this->checkSupport($feature); |
|
121 | - } |
|
122 | - |
|
123 | - return (bool)$isSupported; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Get the minimum required version that supports all of the requested |
|
128 | - * features. |
|
129 | - * |
|
130 | - * Accepts either a string or an array of strings. Returns a |
|
131 | - * SemanticVersion object for the version number that is known to support |
|
132 | - * all the passed-in features, or false if at least one of |
|
133 | - * them is not supported by any known version. |
|
134 | - * |
|
135 | - * @since 0.2.0 |
|
136 | - * |
|
137 | - * @param string|array $features What features to check the support of. |
|
138 | - * @return SemanticVersion|false |
|
139 | - * @throws InvalidArgumentException If the wrong type of argument is passed |
|
140 | - * in. |
|
141 | - * @throws RuntimeException If a requirement could not be parsed. |
|
142 | - */ |
|
143 | - public function getMinimumRequired($features) |
|
144 | - { |
|
145 | - |
|
146 | - if (is_string($features)) { |
|
147 | - $features = array($features); |
|
148 | - } |
|
149 | - |
|
150 | - if ( ! is_array($features)) { |
|
151 | - throw new InvalidArgumentException(sprintf( |
|
152 | - 'Wrong type of argument passed in to get_minimum_required(): "%1$s".', |
|
153 | - gettype($features) |
|
154 | - )); |
|
155 | - } |
|
156 | - |
|
157 | - $minimumRequired = '0.0.0'; |
|
158 | - $isSupported = true; |
|
159 | - |
|
160 | - while (count($features) > 0) { |
|
161 | - $feature = array_pop($features); |
|
162 | - $isSupported &= (bool)$this->checkSupport($feature, $minimumRequired); |
|
163 | - } |
|
164 | - |
|
165 | - return $minimumRequired !== '0.0.0' ? new SemanticVersion($minimumRequired, true) : false; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Check whether a single feature is supported. |
|
170 | - * |
|
171 | - * @since 0.1.0 |
|
172 | - * |
|
173 | - * @param string $feature The feature to check. |
|
174 | - * @param string|null $minimumRequired Optional. Minimum required version that |
|
175 | - * supports all features. |
|
176 | - * @return bool |
|
177 | - * @throws RuntimeException If the requirement could not be parsed. |
|
178 | - */ |
|
179 | - protected function checkSupport($feature, &$minimumRequired = null) |
|
180 | - { |
|
181 | - |
|
182 | - if ( ! $this->config->hasKey($feature)) { |
|
183 | - return false; |
|
184 | - } |
|
185 | - |
|
186 | - $requirements = $this->config->getKey($feature); |
|
187 | - |
|
188 | - if ( ! is_array($requirements)) { |
|
189 | - $requirements = array($requirements); |
|
190 | - } |
|
191 | - |
|
192 | - $isSupported = true; |
|
193 | - |
|
194 | - while (($isSupported || null !== $minimumRequired) && count($requirements) > 0) { |
|
195 | - $requirement = array_pop($requirements); |
|
196 | - $isSupported &= (bool)$this->checkRequirement($requirement, $minimumRequired); |
|
197 | - } |
|
198 | - |
|
199 | - return (bool)$isSupported; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Check whether a single requirement is met. |
|
204 | - * |
|
205 | - * @since 0.1.0 |
|
206 | - * |
|
207 | - * @param string $requirement A requirement that is composed of an |
|
208 | - * operator and a version milestone. |
|
209 | - * @param string|null $minimumRequired Optional. Minimum required version that |
|
210 | - * supports all features. |
|
211 | - * @return bool |
|
212 | - * @throws RuntimeException If the requirement could not be parsed. |
|
213 | - */ |
|
214 | - protected function checkRequirement($requirement, &$minimumRequired = null) |
|
215 | - { |
|
216 | - |
|
217 | - $requirement = trim($requirement); |
|
218 | - $pattern = self::COMPARISON_PATTERN; |
|
219 | - |
|
220 | - $arguments = array(); |
|
221 | - $result = preg_match($pattern, $requirement, $arguments); |
|
222 | - |
|
223 | - if ( ! $result || ! isset($arguments[1]) || ! isset($arguments[2])) { |
|
224 | - throw new RuntimeException(sprintf( |
|
225 | - 'Could not parse the requirement "%1$s".', |
|
226 | - (string)$requirement |
|
227 | - )); |
|
228 | - } |
|
229 | - |
|
230 | - $operator = isset($arguments[1]) ? (string)$arguments[1] : '>='; |
|
231 | - $milestone = isset($arguments[2]) ? (string)$arguments[2] : '0.0.0'; |
|
232 | - |
|
233 | - $isSupported = (bool)version_compare($this->version->getVersion(), $milestone, $operator); |
|
234 | - |
|
235 | - if (null !== $minimumRequired) { |
|
236 | - $requiredVersion = $this->getRequiredVersion($milestone, $operator); |
|
237 | - if (version_compare($requiredVersion, $minimumRequired, '>')) { |
|
238 | - $minimumRequired = $requiredVersion; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - return $isSupported; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Get the required version for a single requirement. |
|
247 | - * |
|
248 | - * @since 0.2.0 |
|
249 | - * |
|
250 | - * @param string $milestone A version milestone that is used to define the |
|
251 | - * requirement. |
|
252 | - * @param string $operator An operator that gets applied to the milestone. |
|
253 | - * Possible values: '<=', 'lt', '<', 'le', '>=', |
|
254 | - * 'gt', '>', 'ge', '=', '==', 'eq', '!=', '<>', |
|
255 | - * 'ne' |
|
256 | - * @return string |
|
257 | - */ |
|
258 | - protected function getRequiredVersion($milestone, $operator) |
|
259 | - { |
|
260 | - |
|
261 | - // TODO: Algorithm is still missing, the `$operator` is simply ignored |
|
262 | - // and the pure `$milestone` is returned. |
|
263 | - return $milestone; |
|
264 | - } |
|
22 | + /** |
|
23 | + * RegEx pattern that matches the comparison string. |
|
24 | + * |
|
25 | + * @since 0.1.0 |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + const COMPARISON_PATTERN = '/^(?:(<=|lt|<|le|>=|gt|>|ge|=|==|eq|!=|<>|ne))([0-9].*)$/'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Reference to the Configuration object. |
|
33 | + * |
|
34 | + * @since 0.1.0 |
|
35 | + * |
|
36 | + * @var ConfigInterface |
|
37 | + */ |
|
38 | + protected $config; |
|
39 | + |
|
40 | + /** |
|
41 | + * Reference to the Version object. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * |
|
45 | + * @var SemanticVersion |
|
46 | + */ |
|
47 | + protected $version; |
|
48 | + |
|
49 | + /** |
|
50 | + * Instantiate a PHPFeature object. |
|
51 | + * |
|
52 | + * @since 0.1.0 |
|
53 | + * |
|
54 | + * @param SemanticVersion|string|int|null $phpVersion Version of PHP to |
|
55 | + * check the features |
|
56 | + * for. |
|
57 | + * @param ConfigInterface|null $config Configuration that |
|
58 | + * contains the known |
|
59 | + * features. |
|
60 | + * @throws RuntimeException If the PHP version could not be validated. |
|
61 | + */ |
|
62 | + public function __construct($phpVersion = null, ConfigInterface $config = null) |
|
63 | + { |
|
64 | + |
|
65 | + // TODO: Better way to bootstrap this while still allowing DI? |
|
66 | + if ( ! $config) { |
|
67 | + $config = new Config(include(__DIR__ . '/../config/known_features.php')); |
|
68 | + } |
|
69 | + |
|
70 | + $this->config = $config; |
|
71 | + |
|
72 | + if (null === $phpVersion) { |
|
73 | + $phpVersion = phpversion(); |
|
74 | + } |
|
75 | + |
|
76 | + if (is_integer($phpVersion)) { |
|
77 | + $phpVersion = (string)$phpVersion; |
|
78 | + } |
|
79 | + |
|
80 | + if (is_string($phpVersion)) { |
|
81 | + $phpVersion = new SemanticVersion($phpVersion, true); |
|
82 | + } |
|
83 | + |
|
84 | + $this->version = $phpVersion; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Check whether a feature or a collection of features is supported. |
|
89 | + * |
|
90 | + * Accepts either a string or an array of strings. Returns true if all the |
|
91 | + * passed-in features are supported, or false if at least one of them is |
|
92 | + * not. |
|
93 | + * |
|
94 | + * @since 0.1.0 |
|
95 | + * |
|
96 | + * @param string|array $features What features to check the support of. |
|
97 | + * @return bool |
|
98 | + * @throws InvalidArgumentException If the wrong type of argument is passed |
|
99 | + * in. |
|
100 | + * @throws RuntimeException If a requirement could not be parsed. |
|
101 | + */ |
|
102 | + public function isSupported($features) |
|
103 | + { |
|
104 | + |
|
105 | + if (is_string($features)) { |
|
106 | + $features = array($features); |
|
107 | + } |
|
108 | + |
|
109 | + if ( ! is_array($features)) { |
|
110 | + throw new InvalidArgumentException(sprintf( |
|
111 | + 'Wrong type of argument passed in to is_supported(): "%1$s".', |
|
112 | + gettype($features) |
|
113 | + )); |
|
114 | + } |
|
115 | + |
|
116 | + $isSupported = true; |
|
117 | + |
|
118 | + while ($isSupported && count($features) > 0) { |
|
119 | + $feature = array_pop($features); |
|
120 | + $isSupported &= (bool)$this->checkSupport($feature); |
|
121 | + } |
|
122 | + |
|
123 | + return (bool)$isSupported; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Get the minimum required version that supports all of the requested |
|
128 | + * features. |
|
129 | + * |
|
130 | + * Accepts either a string or an array of strings. Returns a |
|
131 | + * SemanticVersion object for the version number that is known to support |
|
132 | + * all the passed-in features, or false if at least one of |
|
133 | + * them is not supported by any known version. |
|
134 | + * |
|
135 | + * @since 0.2.0 |
|
136 | + * |
|
137 | + * @param string|array $features What features to check the support of. |
|
138 | + * @return SemanticVersion|false |
|
139 | + * @throws InvalidArgumentException If the wrong type of argument is passed |
|
140 | + * in. |
|
141 | + * @throws RuntimeException If a requirement could not be parsed. |
|
142 | + */ |
|
143 | + public function getMinimumRequired($features) |
|
144 | + { |
|
145 | + |
|
146 | + if (is_string($features)) { |
|
147 | + $features = array($features); |
|
148 | + } |
|
149 | + |
|
150 | + if ( ! is_array($features)) { |
|
151 | + throw new InvalidArgumentException(sprintf( |
|
152 | + 'Wrong type of argument passed in to get_minimum_required(): "%1$s".', |
|
153 | + gettype($features) |
|
154 | + )); |
|
155 | + } |
|
156 | + |
|
157 | + $minimumRequired = '0.0.0'; |
|
158 | + $isSupported = true; |
|
159 | + |
|
160 | + while (count($features) > 0) { |
|
161 | + $feature = array_pop($features); |
|
162 | + $isSupported &= (bool)$this->checkSupport($feature, $minimumRequired); |
|
163 | + } |
|
164 | + |
|
165 | + return $minimumRequired !== '0.0.0' ? new SemanticVersion($minimumRequired, true) : false; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Check whether a single feature is supported. |
|
170 | + * |
|
171 | + * @since 0.1.0 |
|
172 | + * |
|
173 | + * @param string $feature The feature to check. |
|
174 | + * @param string|null $minimumRequired Optional. Minimum required version that |
|
175 | + * supports all features. |
|
176 | + * @return bool |
|
177 | + * @throws RuntimeException If the requirement could not be parsed. |
|
178 | + */ |
|
179 | + protected function checkSupport($feature, &$minimumRequired = null) |
|
180 | + { |
|
181 | + |
|
182 | + if ( ! $this->config->hasKey($feature)) { |
|
183 | + return false; |
|
184 | + } |
|
185 | + |
|
186 | + $requirements = $this->config->getKey($feature); |
|
187 | + |
|
188 | + if ( ! is_array($requirements)) { |
|
189 | + $requirements = array($requirements); |
|
190 | + } |
|
191 | + |
|
192 | + $isSupported = true; |
|
193 | + |
|
194 | + while (($isSupported || null !== $minimumRequired) && count($requirements) > 0) { |
|
195 | + $requirement = array_pop($requirements); |
|
196 | + $isSupported &= (bool)$this->checkRequirement($requirement, $minimumRequired); |
|
197 | + } |
|
198 | + |
|
199 | + return (bool)$isSupported; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Check whether a single requirement is met. |
|
204 | + * |
|
205 | + * @since 0.1.0 |
|
206 | + * |
|
207 | + * @param string $requirement A requirement that is composed of an |
|
208 | + * operator and a version milestone. |
|
209 | + * @param string|null $minimumRequired Optional. Minimum required version that |
|
210 | + * supports all features. |
|
211 | + * @return bool |
|
212 | + * @throws RuntimeException If the requirement could not be parsed. |
|
213 | + */ |
|
214 | + protected function checkRequirement($requirement, &$minimumRequired = null) |
|
215 | + { |
|
216 | + |
|
217 | + $requirement = trim($requirement); |
|
218 | + $pattern = self::COMPARISON_PATTERN; |
|
219 | + |
|
220 | + $arguments = array(); |
|
221 | + $result = preg_match($pattern, $requirement, $arguments); |
|
222 | + |
|
223 | + if ( ! $result || ! isset($arguments[1]) || ! isset($arguments[2])) { |
|
224 | + throw new RuntimeException(sprintf( |
|
225 | + 'Could not parse the requirement "%1$s".', |
|
226 | + (string)$requirement |
|
227 | + )); |
|
228 | + } |
|
229 | + |
|
230 | + $operator = isset($arguments[1]) ? (string)$arguments[1] : '>='; |
|
231 | + $milestone = isset($arguments[2]) ? (string)$arguments[2] : '0.0.0'; |
|
232 | + |
|
233 | + $isSupported = (bool)version_compare($this->version->getVersion(), $milestone, $operator); |
|
234 | + |
|
235 | + if (null !== $minimumRequired) { |
|
236 | + $requiredVersion = $this->getRequiredVersion($milestone, $operator); |
|
237 | + if (version_compare($requiredVersion, $minimumRequired, '>')) { |
|
238 | + $minimumRequired = $requiredVersion; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + return $isSupported; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Get the required version for a single requirement. |
|
247 | + * |
|
248 | + * @since 0.2.0 |
|
249 | + * |
|
250 | + * @param string $milestone A version milestone that is used to define the |
|
251 | + * requirement. |
|
252 | + * @param string $operator An operator that gets applied to the milestone. |
|
253 | + * Possible values: '<=', 'lt', '<', 'le', '>=', |
|
254 | + * 'gt', '>', 'ge', '=', '==', 'eq', '!=', '<>', |
|
255 | + * 'ne' |
|
256 | + * @return string |
|
257 | + */ |
|
258 | + protected function getRequiredVersion($milestone, $operator) |
|
259 | + { |
|
260 | + |
|
261 | + // TODO: Algorithm is still missing, the `$operator` is simply ignored |
|
262 | + // and the pure `$milestone` is returned. |
|
263 | + return $milestone; |
|
264 | + } |
|
265 | 265 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * SemanticVersion Class |
|
4 | - * |
|
5 | - * @package phpfeature |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * SemanticVersion Class |
|
4 | + * |
|
5 | + * @package phpfeature |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Class SemanticVersion |
@@ -19,242 +19,242 @@ discard block |
||
19 | 19 | class SemanticVersion |
20 | 20 | { |
21 | 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-]*))?$/'; |
|
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 | 30 | |
31 | - /** |
|
32 | - * Version that is used. |
|
33 | - * |
|
34 | - * @since 0.1.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $version; |
|
31 | + /** |
|
32 | + * Version that is used. |
|
33 | + * |
|
34 | + * @since 0.1.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $version; |
|
39 | 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; |
|
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 | 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) |
|
63 | - { |
|
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) |
|
63 | + { |
|
64 | 64 | |
65 | - if (null === $version) { |
|
66 | - $version = '0.0.0'; |
|
67 | - } |
|
65 | + if (null === $version) { |
|
66 | + $version = '0.0.0'; |
|
67 | + } |
|
68 | 68 | |
69 | - $version = $this->validate($version, $partial); |
|
69 | + $version = $this->validate($version, $partial); |
|
70 | 70 | |
71 | - $this->version = $version; |
|
72 | - } |
|
71 | + $this->version = $version; |
|
72 | + } |
|
73 | 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) |
|
87 | - { |
|
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) |
|
87 | + { |
|
88 | 88 | |
89 | - $version = trim($version); |
|
90 | - $pattern = self::VERSION_PATTERN; |
|
89 | + $version = trim($version); |
|
90 | + $pattern = self::VERSION_PATTERN; |
|
91 | 91 | |
92 | - $components = array(); |
|
93 | - $result = preg_match($pattern, $version, $components); |
|
92 | + $components = array(); |
|
93 | + $result = preg_match($pattern, $version, $components); |
|
94 | 94 | |
95 | - if ( ! $result) { |
|
96 | - throw new RuntimeException(sprintf( |
|
97 | - 'Failed to validate version "%1$s".', |
|
98 | - (string)$version |
|
99 | - )); |
|
100 | - } |
|
95 | + if ( ! $result) { |
|
96 | + throw new RuntimeException(sprintf( |
|
97 | + 'Failed to validate version "%1$s".', |
|
98 | + (string)$version |
|
99 | + )); |
|
100 | + } |
|
101 | 101 | |
102 | - if ( ! $partial && ( ! isset($components[2]) || ! isset($components[3]))) { |
|
103 | - throw new RuntimeException(sprintf( |
|
104 | - 'Could not accept partial version "%1$s", requested full versions only.', |
|
105 | - (string)$version |
|
106 | - )); |
|
107 | - } |
|
102 | + if ( ! $partial && ( ! isset($components[2]) || ! isset($components[3]))) { |
|
103 | + throw new RuntimeException(sprintf( |
|
104 | + 'Could not accept partial version "%1$s", requested full versions only.', |
|
105 | + (string)$version |
|
106 | + )); |
|
107 | + } |
|
108 | 108 | |
109 | - $this->setComponent('major', isset($components[1]) ? (int)$components[1] : 0); |
|
110 | - $this->setComponent('minor', isset($components[2]) ? (int)$components[2] : 0); |
|
111 | - $this->setComponent('patch', isset($components[3]) ? (int)$components[3] : 0); |
|
112 | - $this->setComponent('pre-release', isset($components[4]) ? (string)$components[4] : ''); |
|
113 | - $this->setComponent('build', isset($components[5]) ? (string)$components[5] : ''); |
|
109 | + $this->setComponent('major', isset($components[1]) ? (int)$components[1] : 0); |
|
110 | + $this->setComponent('minor', isset($components[2]) ? (int)$components[2] : 0); |
|
111 | + $this->setComponent('patch', isset($components[3]) ? (int)$components[3] : 0); |
|
112 | + $this->setComponent('pre-release', isset($components[4]) ? (string)$components[4] : ''); |
|
113 | + $this->setComponent('build', isset($components[5]) ? (string)$components[5] : ''); |
|
114 | 114 | |
115 | - $version = $this->getVersionFromComponents(); |
|
115 | + $version = $this->getVersionFromComponents(); |
|
116 | 116 | |
117 | - return $version; |
|
118 | - } |
|
117 | + return $version; |
|
118 | + } |
|
119 | 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() |
|
128 | - { |
|
129 | - return (string)isset($this->version) ? $this->version : '0.0.0'; |
|
130 | - } |
|
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() |
|
128 | + { |
|
129 | + return (string)isset($this->version) ? $this->version : '0.0.0'; |
|
130 | + } |
|
131 | 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() |
|
140 | - { |
|
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() |
|
140 | + { |
|
141 | 141 | |
142 | - $pre_release = $this->getPreRelease() ? '-' . $this->getPreRelease() : ''; |
|
143 | - $build = $this->getBuild() ? '+' . $this->getBuild() : ''; |
|
142 | + $pre_release = $this->getPreRelease() ? '-' . $this->getPreRelease() : ''; |
|
143 | + $build = $this->getBuild() ? '+' . $this->getBuild() : ''; |
|
144 | 144 | |
145 | - $version = sprintf( |
|
146 | - '%1$s.%2$s.%3$s%4$s%5$s', |
|
147 | - $this->getMajor(), |
|
148 | - $this->getMinor(), |
|
149 | - $this->getPatch(), |
|
150 | - $pre_release, |
|
151 | - $build |
|
152 | - ); |
|
145 | + $version = sprintf( |
|
146 | + '%1$s.%2$s.%3$s%4$s%5$s', |
|
147 | + $this->getMajor(), |
|
148 | + $this->getMinor(), |
|
149 | + $this->getPatch(), |
|
150 | + $pre_release, |
|
151 | + $build |
|
152 | + ); |
|
153 | 153 | |
154 | - return $version; |
|
155 | - } |
|
154 | + return $version; |
|
155 | + } |
|
156 | 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() |
|
165 | - { |
|
166 | - return (int)$this->getComponent('major') ?: 0; |
|
167 | - } |
|
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() |
|
165 | + { |
|
166 | + return (int)$this->getComponent('major') ?: 0; |
|
167 | + } |
|
168 | 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() |
|
177 | - { |
|
178 | - return (int)$this->getComponent('minor') ?: 0; |
|
179 | - } |
|
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() |
|
177 | + { |
|
178 | + return (int)$this->getComponent('minor') ?: 0; |
|
179 | + } |
|
180 | 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() |
|
189 | - { |
|
190 | - return (int)$this->getComponent('patch') ?: 0; |
|
191 | - } |
|
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() |
|
189 | + { |
|
190 | + return (int)$this->getComponent('patch') ?: 0; |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Get the pre-release label. |
|
195 | - * |
|
196 | - * @since 0.1.0 |
|
197 | - * |
|
198 | - * @return string The patch version that is used. Empty string if not |
|
199 | - * defined. |
|
200 | - */ |
|
201 | - public function getPreRelease() |
|
202 | - { |
|
203 | - return (string)$this->getComponent('pre-release') ?: ''; |
|
204 | - } |
|
193 | + /** |
|
194 | + * Get the pre-release label. |
|
195 | + * |
|
196 | + * @since 0.1.0 |
|
197 | + * |
|
198 | + * @return string The patch version that is used. Empty string if not |
|
199 | + * defined. |
|
200 | + */ |
|
201 | + public function getPreRelease() |
|
202 | + { |
|
203 | + return (string)$this->getComponent('pre-release') ?: ''; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Get the build metadata. |
|
208 | - * |
|
209 | - * @since 0.1.0 |
|
210 | - * |
|
211 | - * @return string The build metadata for the version that is used. Empty |
|
212 | - * string if not defined. |
|
213 | - */ |
|
214 | - public function getBuild() |
|
215 | - { |
|
216 | - return (string)$this->getComponent('build') ?: ''; |
|
217 | - } |
|
206 | + /** |
|
207 | + * Get the build metadata. |
|
208 | + * |
|
209 | + * @since 0.1.0 |
|
210 | + * |
|
211 | + * @return string The build metadata for the version that is used. Empty |
|
212 | + * string if not defined. |
|
213 | + */ |
|
214 | + public function getBuild() |
|
215 | + { |
|
216 | + return (string)$this->getComponent('build') ?: ''; |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * Get a component of the version. |
|
221 | - * |
|
222 | - * @since 0.1.0 |
|
223 | - * |
|
224 | - * @param string $level What level of component to get. Possible values: |
|
225 | - * 'major', 'minor', 'patch' |
|
226 | - * @return int The requested version component. null if not defined. |
|
227 | - */ |
|
228 | - protected function getComponent($level) |
|
229 | - { |
|
230 | - return array_key_exists($level, $this->components) |
|
231 | - ? $this->components[$level] |
|
232 | - : null; |
|
233 | - } |
|
219 | + /** |
|
220 | + * Get a component of the version. |
|
221 | + * |
|
222 | + * @since 0.1.0 |
|
223 | + * |
|
224 | + * @param string $level What level of component to get. Possible values: |
|
225 | + * 'major', 'minor', 'patch' |
|
226 | + * @return int The requested version component. null if not defined. |
|
227 | + */ |
|
228 | + protected function getComponent($level) |
|
229 | + { |
|
230 | + return array_key_exists($level, $this->components) |
|
231 | + ? $this->components[$level] |
|
232 | + : null; |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Set a component of the version. |
|
237 | - * |
|
238 | - * @since 0.1.0 |
|
239 | - * |
|
240 | - * @param string $level What level of component to set. Possible values: |
|
241 | - * 'major', 'minor', 'patch' |
|
242 | - * @param int $version What version to set that component to. |
|
243 | - */ |
|
244 | - protected function setComponent($level, $version) |
|
245 | - { |
|
246 | - $this->components[$level] = $version; |
|
247 | - } |
|
235 | + /** |
|
236 | + * Set a component of the version. |
|
237 | + * |
|
238 | + * @since 0.1.0 |
|
239 | + * |
|
240 | + * @param string $level What level of component to set. Possible values: |
|
241 | + * 'major', 'minor', 'patch' |
|
242 | + * @param int $version What version to set that component to. |
|
243 | + */ |
|
244 | + protected function setComponent($level, $version) |
|
245 | + { |
|
246 | + $this->components[$level] = $version; |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Get a string representation of the object. |
|
251 | - * |
|
252 | - * @since 0.2.0 |
|
253 | - * |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - public function __toString() |
|
257 | - { |
|
258 | - return (string)$this->getVersion(); |
|
259 | - } |
|
249 | + /** |
|
250 | + * Get a string representation of the object. |
|
251 | + * |
|
252 | + * @since 0.2.0 |
|
253 | + * |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + public function __toString() |
|
257 | + { |
|
258 | + return (string)$this->getVersion(); |
|
259 | + } |
|
260 | 260 | } |