1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deployer\Component\PharUpdate\Version; |
4
|
|
|
|
5
|
|
|
use Deployer\Component\PharUpdate\Version\Exception\InvalidStringRepresentationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parses the string representation of a version number. |
9
|
|
|
* |
10
|
|
|
* @author Kevin Herrera <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Parser |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The build metadata component. |
16
|
|
|
*/ |
17
|
|
|
const BUILD = 'build'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The major version number component. |
21
|
|
|
*/ |
22
|
|
|
const MAJOR = 'major'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The minor version number component. |
26
|
|
|
*/ |
27
|
|
|
const MINOR = 'minor'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The patch version number component. |
31
|
|
|
*/ |
32
|
|
|
const PATCH = 'patch'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The pre-release version number component. |
36
|
|
|
*/ |
37
|
|
|
const PRE_RELEASE = 'pre'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a Version builder for the string representation. |
41
|
|
|
* |
42
|
|
|
* @param string $version The string representation. |
43
|
|
|
* |
44
|
|
|
* @return Builder A Version builder. |
45
|
|
|
*/ |
46
|
|
|
public static function toBuilder($version) |
47
|
|
|
{ |
48
|
|
|
return Builder::create()->importComponents( |
49
|
|
|
self::toComponents($version) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the components of the string representation. |
55
|
|
|
* |
56
|
|
|
* @param string $version The string representation. |
57
|
|
|
* |
58
|
|
|
* @return array The components of the version. |
59
|
|
|
* |
60
|
|
|
* @throws InvalidStringRepresentationException If the string representation |
61
|
|
|
* is invalid. |
62
|
|
|
*/ |
63
|
|
|
public static function toComponents($version) |
64
|
|
|
{ |
65
|
|
|
if (!Validator::isVersion($version)) { |
66
|
|
|
throw new InvalidStringRepresentationException($version); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if (false !== strpos($version, '+')) { |
70
|
|
|
list($version, $build) = explode('+', $version); |
71
|
|
|
|
72
|
|
|
$build = explode('.', $build); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if (false !== strpos($version, '-')) { |
76
|
|
|
list($version, $pre) = explode('-', $version); |
77
|
|
|
|
78
|
|
|
$pre = explode('.', $pre); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
list( |
82
|
|
|
$major, |
83
|
|
|
$minor, |
84
|
|
|
$patch |
85
|
|
|
) = explode('.', $version); |
86
|
|
|
|
87
|
|
|
return array( |
88
|
|
|
self::MAJOR => intval($major), |
89
|
|
|
self::MINOR => intval($minor), |
90
|
|
|
self::PATCH => intval($patch), |
91
|
|
|
self::PRE_RELEASE => isset($pre) ? $pre : array(), |
92
|
|
|
self::BUILD => isset($build) ? $build : array(), |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a Version instance for the string representation. |
98
|
|
|
* |
99
|
|
|
* @param string $version The string representation. |
100
|
|
|
* |
101
|
|
|
* @return Version A Version instance. |
102
|
|
|
*/ |
103
|
|
|
public static function toVersion($version) |
104
|
|
|
{ |
105
|
|
|
$components = self::toComponents($version); |
106
|
|
|
|
107
|
|
|
return new Version( |
108
|
|
|
$components['major'], |
|
|
|
|
109
|
|
|
$components['minor'], |
|
|
|
|
110
|
|
|
$components['patch'], |
|
|
|
|
111
|
|
|
$components['pre'], |
|
|
|
|
112
|
|
|
$components['build'] |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.