-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version.php
127 lines (102 loc) · 3.29 KB
/
Version.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Version Class
* @Author: [email protected]
* @link: http://github.com/xiidea/version
*/
class Version
{
CONST DEFAULT_MAJOR = 0;
CONST DEFAULT_MINOR = 0;
CONST MODIFIER_SEPARATOR = '-';
private static $modifierRegex = '[._-]?(?:([a-z A-Z]+)(?:[.-]?(\d+))?)?';
private $_value = array(
'major' => self::DEFAULT_MAJOR,
'minor' => self::DEFAULT_MINOR,
'patch' => '',
);
private $_modifier = '';
public function __construct($version = null)
{
if ($version !== null) {
$this->parse($version);
}
}
public function parse($version)
{
$version = trim($version);
if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
$this->_value['major'] = $matches[1];
$this->_value['minor'] = ltrim($matches[2], '.');
$this->_value['patch'] = ltrim($matches[3], '.');
$index = 4;
} elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) { // match date-based versioning
$this->_value['major'] = preg_replace('{\D}', '-', $matches[1]);
$this->_value['minor'] = $this->_value['patch'] = '';
$index = 2;
}
if (isset($index)) {
if (!empty($matches[$index])) {
$this->_modifier = $matches[$index] . (!empty($matches[$index+1]) ? $matches[$index+1] : '');
}
return $this;
}
throw new \UnexpectedValueException('Invalid version string "' . $version . '"');
}
public function nextMajor()
{
$this->_value['major']++;
return $this;
}
public function nextMinor()
{
$this->_value['minor']++;
return $this;
}
public function nextPatch()
{
$this->_value['patch']++;
return $this;
}
public function setModifier($modifier = '')
{
$this->_modifier = $modifier;
return $this;
}
public function nextModifier()
{
preg_match('{\d}', $this->_modifier, $matches);
if(!isset($matches[0])){
$this->_modifier = $this->_modifier . '1';
}else{
$this->_modifier = preg_replace('{\d}', $matches[0]+1, $this->_modifier);
}
return $this;
}
/**
* Normalizes a version string to be able to perform comparisons on it
*
* @throws \UnexpectedValueException
* @return array
*/
public function normalize()
{
if (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $this->_value['major'], $matches)) { // match date-based versioning
$version = preg_replace('{\D}', '-', $matches[1]);
} else {
$version = implode('.', array_map('intval', $this->_value));
}
if (!empty($this->_modifier)) {
$version .= self::MODIFIER_SEPARATOR . $this->_modifier;
}
return $version;
}
public function __toString()
{
$version = implode('.', array_filter($this->_value));
if (!empty($this->_modifier)) {
$version .= self::MODIFIER_SEPARATOR . $this->_modifier;
}
return $version;
}
}