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