Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
50 / 50 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Version | |
100.00% |
50 / 50 |
|
100.00% |
5 / 5 |
24 | |
100.00% |
1 / 1 |
asString | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
isCompatible | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
guessFromFileName | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
split | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
compare | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
10 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | |
5 | class Version { |
6 | static function asString(int $major, int $minor, int $patch, string $label): ?string { |
7 | $version = implode('-', array_filter(["$major.$minor.$patch", $label])); |
8 | return ($version == '0.0.0') ? null : $version; |
9 | } |
10 | |
11 | static function isCompatible(string $subject, ?string $object = null): bool { |
12 | if (!$object and !$subject) { |
13 | return true; |
14 | } |
15 | |
16 | if (!$object) { |
17 | $object = SystemConfig::$system_version; |
18 | } |
19 | |
20 | $object = Version::split($object); |
21 | $subject = Version::split($subject); |
22 | |
23 | if ($object['major'] != $subject['major']) { |
24 | return false; |
25 | } |
26 | |
27 | return ($object['minor'] >= $subject['minor']); |
28 | } |
29 | |
30 | static function guessFromFileName(string $fileName): array { |
31 | // this regex includes some naming habits from verona 2 to 4 times |
32 | $regex = "/^(\D+?)[@V-]?((\d+)(\.\d+)?(\.\d+)?(-\S+?)?)?(.\D{3,4})?$/"; |
33 | $matches = []; |
34 | preg_match($regex, $fileName, $matches); |
35 | return [ |
36 | 'module' => $matches[1] ?? '', |
37 | 'full' => $matches[2] ?? '', |
38 | 'major' => (int) ($matches[3] ?? '0'), |
39 | 'minor' => isset($matches[4]) ? ((int) substr($matches[4], 1)) : 0, |
40 | 'patch' => isset($matches[5]) ? ((int) substr($matches[5], 1)) : 0, |
41 | 'label' => isset($matches[6]) ? substr($matches[6], 1) : '', |
42 | ]; |
43 | } |
44 | |
45 | static function split(string $versionString): array { |
46 | $objectVersionParts = preg_split("/[.-]/", $versionString); |
47 | |
48 | return [ |
49 | 'major' => (int) $objectVersionParts[0], |
50 | 'minor' => isset($objectVersionParts[1]) ? (int) $objectVersionParts[1] : 0, |
51 | 'patch' => isset($objectVersionParts[2]) ? (int) $objectVersionParts[2] : 0, |
52 | 'label' => $objectVersionParts[3] ?? "" |
53 | ]; |
54 | } |
55 | |
56 | static function compare(string $subject, ?string $object = null): int { |
57 | if (!$object) { |
58 | $object = SystemConfig::$system_version; |
59 | } |
60 | |
61 | $object = Version::split($object); |
62 | $subject = Version::split($subject); |
63 | |
64 | if ($subject['major'] > $object['major']) { |
65 | return 1; |
66 | } |
67 | |
68 | if ($subject['major'] < $object['major']) { |
69 | return -1; |
70 | } |
71 | |
72 | if ($subject['minor'] > $object['minor']) { |
73 | return 1; |
74 | } |
75 | |
76 | if ($subject['minor'] < $object['minor']) { |
77 | return -1; |
78 | } |
79 | |
80 | if ($subject['patch'] > $object['patch']) { |
81 | return 1; |
82 | } |
83 | |
84 | if ($subject['patch'] < $object['patch']) { |
85 | return -1; |
86 | } |
87 | |
88 | if (strcasecmp($subject['label'], $object['label']) > 0) { |
89 | return 1; |
90 | } |
91 | |
92 | if (strcasecmp($subject['label'], $object['label']) < 0) { |
93 | return -1; |
94 | } |
95 | |
96 | return 0; |
97 | } |
98 | } |