1 | <?php |
||
14 | class VersionParser |
||
15 | { |
||
16 | use ValidatesSemVer; |
||
17 | |||
18 | /** |
||
19 | * Parse the semantic version strings. |
||
20 | * |
||
21 | * @param string $versionStr |
||
22 | * |
||
23 | * @throws InvalidVersionException |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | 10 | public static function parse($versionStr) |
|
28 | { |
||
29 | 10 | if (!static::validateVersion($versionStr)) { |
|
30 | 1 | throw new InvalidVersionException("Invalid version string: {$versionStr}"); |
|
31 | } |
||
32 | |||
33 | 9 | $buildMetadata = []; |
|
34 | 9 | $preRelease = []; |
|
35 | |||
36 | 9 | if (false !== strpos($versionStr, '+')) { |
|
37 | 4 | list($versionStr, $buildMetadata) = explode('+', $versionStr); |
|
38 | 4 | $buildMetadata = explode('.', $buildMetadata); |
|
39 | 4 | } |
|
40 | |||
41 | 9 | if (false !== ($pos = strpos($versionStr, '-'))) { |
|
42 | 8 | $original = $versionStr; |
|
43 | 8 | $versionStr = substr($versionStr, 0, $pos); |
|
44 | 8 | $preRelease = explode('.', substr($original, $pos + 1)); |
|
45 | 8 | } |
|
46 | |||
47 | 9 | list($major, $minor, $patch) = array_map('intval', explode('.', $versionStr)); |
|
48 | |||
49 | 9 | return compact('major', 'minor', 'patch', 'preRelease', 'buildMetadata'); |
|
50 | } |
||
52 |