PreReleaseSuffix.php
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace PharIo\Version;
class PreReleaseSuffix {
private $valueScoreMap = [
'dev' => 0,
'a' => 1,
'alpha' => 1,
'b' => 2,
'beta' => 2,
'rc' => 3,
'p' => 4,
'patch' => 4,
];
/**
* @var string
*/
private $value;
/**
* @var int
*/
private $valueScore;
/**
* @var int
*/
private $number = 0;
/**
* @param string $value
*/
public function __construct($value) {
$this->parseValue($value);
}
/**
* @return string
*/
public function getValue() {
return $this->value;
}
/**
* @return int|null
*/
public function getNumber() {
return $this->number;
}
/**
* @param PreReleaseSuffix $suffix
*
* @return bool
*/
public function isGreaterThan(PreReleaseSuffix $suffix) {
if ($this->valueScore > $suffix->valueScore) {
return true;
}
if ($this->valueScore < $suffix->valueScore) {
return false;
}
return $this->getNumber() > $suffix->getNumber();
}
/**
* @param $value
*
* @return int
*/
private function mapValueToScore($value) {
if (array_key_exists($value, $this->valueScoreMap)) {
return $this->valueScoreMap[$value];
}
return 0;
}
private function parseValue($value) {
$regex = '/-?(dev|beta|b|rc|alpha|a|patch|p)\.?(\d*).*$/i';
if (preg_match($regex, $value, $matches) !== 1) {
throw new InvalidPreReleaseSuffixException(sprintf('Invalid label %s', $value));
}
$this->value = $matches[1];
if (isset($matches[2])) {
$this->number = (int)$matches[2];
}
$this->valueScore = $this->mapValueToScore($this->value);
}
}