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 | if (false !== strpos($version, '+')) { |
||||
70 | list($version, $build) = explode('+', $version); |
||||
71 | |||||
72 | $build = explode('.', $build); |
||||
73 | } |
||||
74 | |||||
75 | 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'], |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
109 | $components['minor'], |
||||
0 ignored issues
–
show
It seems like
$components['minor'] can also be of type array ; however, parameter $minor of Deployer\Component\PharU...\Version::__construct() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
110 | $components['patch'], |
||||
0 ignored issues
–
show
It seems like
$components['patch'] can also be of type array ; however, parameter $patch of Deployer\Component\PharU...\Version::__construct() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
111 | $components['pre'], |
||||
0 ignored issues
–
show
It seems like
$components['pre'] can also be of type integer ; however, parameter $pre of Deployer\Component\PharU...\Version::__construct() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
112 | $components['build'] |
||||
0 ignored issues
–
show
It seems like
$components['build'] can also be of type integer ; however, parameter $build of Deployer\Component\PharU...\Version::__construct() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
113 | ); |
||||
114 | } |
||||
115 | } |
||||
116 |