|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Version\Extension; |
|
6
|
|
|
|
|
7
|
|
|
use Version\Assert\VersionAssert; |
|
8
|
|
|
|
|
9
|
|
|
class PreRelease extends BaseExtension |
|
10
|
|
|
{ |
|
11
|
60 |
|
public static function empty(): PreRelease |
|
12
|
|
|
{ |
|
13
|
60 |
|
static $noPreRelease = null; |
|
14
|
|
|
|
|
15
|
60 |
|
if (null === $noPreRelease) { |
|
16
|
|
|
$noPreRelease = new self(...[]); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
60 |
|
return $noPreRelease; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
30 |
|
protected function validate(string $identifier): void |
|
23
|
|
|
{ |
|
24
|
30 |
|
VersionAssert::that($identifier)->regex( |
|
25
|
30 |
|
'/^[0-9A-Za-z\-]+$/', |
|
26
|
30 |
|
'Pre-release version is not valid; identifiers must include only alphanumerics and hyphen' |
|
27
|
|
|
); |
|
28
|
29 |
|
} |
|
29
|
|
|
|
|
30
|
21 |
|
public function compareTo(PreRelease $preRelease): int |
|
31
|
|
|
{ |
|
32
|
21 |
|
$preRelease1Ids = array_values($this->getIdentifiers()); |
|
33
|
21 |
|
$preRelease2Ids = array_values($preRelease->getIdentifiers()); |
|
34
|
|
|
|
|
35
|
21 |
|
$preRelease1IdsCount = count($preRelease1Ids); |
|
36
|
21 |
|
$preRelease2IdsCount = count($preRelease2Ids); |
|
37
|
|
|
|
|
38
|
21 |
|
$limit = min($preRelease1IdsCount, $preRelease2IdsCount); |
|
39
|
|
|
|
|
40
|
21 |
|
for ($i = 0; $i < $limit; $i++) { |
|
41
|
10 |
|
if ($preRelease1Ids[$i] === $preRelease2Ids[$i]) { |
|
42
|
5 |
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
7 |
|
return $this->compareIdentifiers($preRelease1Ids[$i], $preRelease2Ids[$i]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
14 |
|
return $preRelease1IdsCount - $preRelease2IdsCount; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
7 |
|
private function compareIdentifiers($identifier1, $identifier2): int |
|
52
|
|
|
{ |
|
53
|
7 |
|
$identifier1IsNumber = ctype_digit($identifier1); |
|
54
|
7 |
|
$identifier2IsNumber = ctype_digit($identifier2); |
|
55
|
|
|
|
|
56
|
7 |
|
if ($identifier1IsNumber xor $identifier2IsNumber) { |
|
57
|
1 |
|
return $identifier1IsNumber ? -1 : 1; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
if ($identifier1IsNumber && $identifier2IsNumber) { |
|
61
|
2 |
|
return (int) $identifier1 - (int) $identifier2; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
return strcmp($identifier1, $identifier2); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|