Skip to content

Commit 25cf3fb

Browse files
committed
Recognize versions like: 2.5.0-2011-12-21-2
1 parent 32e2d03 commit 25cf3fb

File tree

5 files changed

+132
-88
lines changed

5 files changed

+132
-88
lines changed

src/Version/Constraint.php

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ abstract class Constraint
1212
* Indicate if this constraint matches another constraint
1313
*
1414
* @param Constraint $constraint
15+
*
1516
* @return bool
1617
*/
1718
abstract public function matches(Constraint $constraint);
@@ -20,33 +21,37 @@ abstract public function matches(Constraint $constraint);
2021
* Parse a string and return a Constraint.
2122
*
2223
* @param string $input
24+
*
2325
* @return Constraint
26+
*
27+
* @throws \Exception
2428
*/
2529
public static function parse($input)
2630
{
2731
$input = trim($input);
2832

29-
if (strlen($input) == 0) {
33+
if ('' === $input) {
3034
throw new \UnexpectedValueException('Empty.');
3135
}
3236

33-
$input = explode(',', $input);
34-
if (count($input) > 1) {
37+
$inputParts = explode(',', $input);
38+
39+
if (count($inputParts) > 1) {
3540
$and = true;
3641
} else {
37-
$input = explode('|', $input[0]);
42+
$inputParts = explode('|', $inputParts[0]);
3843
$and = false;
3944
}
4045

41-
if (count($input) > 1) {
46+
if (count($inputParts) > 1) {
4247
$constraints = array();
43-
foreach ($input as $constraint) {
48+
foreach ($inputParts as $constraint) {
4449
$constraints[] = self::parse($constraint);
4550
}
4651
return new MultiConstraint($constraints, $and);
4752
}
4853

49-
$input = $input[0];
54+
$inputParts = $inputParts[0];
5055

5156
$regex = '/^' .
5257
'(?:([\*|x])\.)?' .
@@ -55,24 +60,24 @@ public static function parse($input)
5560
'(?:([\*|x]))?' .
5661
'$/';
5762

58-
if (preg_match($regex, $input, $matches)) {
63+
if (preg_match($regex, $inputParts, $matches)) {
5964
return new AnythingConstraint();
6065
}
6166

6267
$regex = '/^' .
63-
'(?:(' . Operator::REGEX . '))? *' .
68+
'(' . Operator::REGEX . ')? *' .
6469
'(?:(\d+|\*|x)\.)?' .
6570
'(?:(\d+|\*|x)\.)?' .
6671
'(?:(\d+|\*|x)\.)?' .
67-
'(?:(\d+|\*|x))?' .
72+
'(\d+|\*|x)?' .
6873
'(?:' . Stability::REGEX . ')?' .
6974
'$/i';
7075

71-
if (!preg_match($regex, $input, $matches)) {
72-
throw new \UnexpectedValueException('Invalid type: ' . $input);
76+
if (!preg_match($regex, $inputParts, $matches)) {
77+
throw new \UnexpectedValueException('Invalid type: ' . $inputParts);
7378
}
7479

75-
if (isset($matches[1]) && strlen($matches[1]) > 0) {
80+
if (isset($matches[1]) && '' !== $matches[1]) {
7681
$operator = $matches[1];
7782
} else {
7883
$operator = '=';
@@ -81,20 +86,20 @@ public static function parse($input)
8186

8287
$parts = array();
8388

84-
if (isset($matches[2]) && strlen($matches[2]) > 0) {
89+
if (isset($matches[2]) && '' !== $matches[2]) {
8590
$parts[] = $matches[2];
8691
}
87-
if (isset($matches[3]) && strlen($matches[3]) > 0) {
92+
if (isset($matches[3]) && '' !== $matches[3]) {
8893
$parts[] = $matches[3];
8994
}
90-
if (isset($matches[4]) && strlen($matches[4]) > 0) {
95+
if (isset($matches[4]) && '' !== $matches[4]) {
9196
$parts[] = $matches[4];
9297
}
93-
if (isset($matches[5]) && strlen($matches[5]) > 0) {
98+
if (isset($matches[5]) && '' !== $matches[5]) {
9499
$parts[] = $matches[5];
95100
}
96101

97-
if ((string)$operator == '~') {
102+
if ((string)$operator === '~') {
98103
$end = count($parts);
99104
} else {
100105
$end = null;
@@ -107,15 +112,15 @@ public static function parse($input)
107112
$max = $parts;
108113

109114
if ($end) {
110-
if ($end == 1) {
115+
if ($end === 1) {
111116
$max[0]++;
112-
} elseif ($end == 2) {
117+
} elseif ($end === 2) {
113118
$max[0]++;
114119
$max[1] = 0;
115-
} elseif ($end == 3) {
120+
} elseif ($end === 3) {
116121
$max[1]++;
117122
$max[2] = 0;
118-
} elseif ($end == 4) {
123+
} elseif ($end === 4) {
119124
$max[2]++;
120125
$max[3] = 0;
121126
} else {
@@ -142,6 +147,7 @@ public static function parse($input)
142147
}
143148

144149
$version = new Version($parts[0]);
150+
145151
if (isset($parts[1])) {
146152
$version->setMinor($parts[1]);
147153
}
@@ -152,40 +158,46 @@ public static function parse($input)
152158
$version->setMicro($parts[3]);
153159
}
154160

155-
if (isset($matches[6]) && strlen($matches[6]) > 0) {
156-
if (strtolower($matches[5]) == 'rc') {
161+
if (isset($matches[6]) && '' !== $matches[6]) {
162+
if (strtolower($matches[5]) === 'rc') {
157163
$stability = 'RC';
158164
} elseif (in_array(strtolower($matches[6]), array('pl', 'patch', 'p'))) {
159165
$stability = 'patch';
160166
} elseif (in_array(strtolower($matches[6]), array('beta', 'b'))) {
161167
$stability = 'beta';
162-
} elseif (strtolower($matches[6]) == 'stable') {
168+
} elseif (strtolower($matches[6]) === 'stable') {
163169
$stability = 'stable';
164170
} else {
165-
throw new \UnexpectedValueException('Invalid type: ' . $input);
171+
throw new \UnexpectedValueException('Invalid type: ' . $inputParts);
166172
}
173+
167174
$version->setStability(new Stability($stability, $matches[7]));
168175
}
169176

170177
foreach ($parts as $k => $v) {
171-
if ($v != $max[$k]) {
178+
if ($v !== $max[$k]) {
172179
$maxVersion = new Version($max[0]);
180+
173181
if (isset($max[1])) {
174182
$maxVersion->setMinor($max[1]);
175183
}
184+
176185
if (isset($max[2])) {
177186
$maxVersion->setRevision($max[2]);
178187
}
188+
179189
if (isset($max[3])) {
180190
$maxVersion->setMicro($max[3]);
181191
}
182192

183-
if ((string)$version == '0.0.0.0') {
193+
if ((string)$version === '0.0.0.0') {
184194
return new SimpleConstraint(new Operator('<'), $maxVersion);
185195
}
186-
if (isset($matches[6]) && strtolower($matches[6]) == 'stable') {
196+
197+
if (isset($matches[6]) && strtolower($matches[6]) === 'stable') {
187198
$version->setStability(new Stability());
188199
}
200+
189201
return new MultiConstraint(array(
190202
new SimpleConstraint(new Operator('>='), $version),
191203
new SimpleConstraint(new Operator('<'), $maxVersion)

src/Version/Stability.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Stability
66
{
7-
const REGEX = '[-_\.]?(?:(?P<stability>rc|pl|a|alpha|beta|b|patch|stable|p|dev|d)\.?(?P<stabilityVersion>\d*)|(?P<date>[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])))';
7+
const REGEX = '[-_\.]?(?:(?P<stability>rc|pl|a|alpha|beta|b|patch|stable|p|dev|d)\.?(?P<stabilityVersion>\d*)|(?P<date>[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1]))[\.-]?(?P<dateVersion>\d*))';
88

99
/**
1010
* @var string
@@ -21,7 +21,9 @@ public function __construct($stability = 'stable', $number = null)
2121
if ('' === $stability) {
2222
$stability = 'stable';
2323
}
24+
2425
$stability = strtolower($stability);
26+
2527
switch ($stability) {
2628
case 'rc':
2729
$stability = 'RC';
@@ -44,6 +46,7 @@ public function __construct($stability = 'stable', $number = null)
4446
$stability = 'dev';
4547
break;
4648
}
49+
4750
$this->stability = $stability;
4851
$this->number = $number;
4952
}
@@ -102,4 +105,12 @@ private function toInt($stability)
102105
throw new \InvalidArgumentException('Invalid stability: ' . $stability);
103106
}
104107
}
108+
109+
/**
110+
* @return int
111+
*/
112+
public function getNumber()
113+
{
114+
return $this->number;
115+
}
105116
}

src/Version/Version.php

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Version
3434
*/
3535
private $date;
3636

37+
/**
38+
* @var int
39+
*/
40+
private $dateVersion;
41+
3742
/**
3843
* @var bool
3944
*/
@@ -127,7 +132,7 @@ public static function parse($input)
127132
}
128133

129134
if (isset($matches['date']) && '' !== $matches['date']) {
130-
$version->setDate($matches['date']);
135+
$version->setDate($matches['date'], $matches['dateVersion']);
131136
}
132137

133138
return $version;
@@ -231,44 +236,12 @@ public function getDate()
231236

232237
/**
233238
* @param string $date
239+
* @param int $dateVersion
234240
*/
235-
public function setDate($date)
241+
public function setDate($date, $dateVersion)
236242
{
237243
$this->date = $date;
238-
}
239-
240-
public function __toString()
241-
{
242-
if ($this->regular) {
243-
$version =
244-
$this->major . '.' .
245-
$this->minor . '.' .
246-
$this->revision;
247-
if ($this->micro !== null) {
248-
$version .= '.' . $this->micro;
249-
}
250-
} else {
251-
$version = $this->major;
252-
if ($this->minor) {
253-
$version .= '-' . $this->minor;
254-
}
255-
if ($this->revision) {
256-
$version .= '-' . $this->revision;
257-
}
258-
if ($this->micro) {
259-
$version .= '-' . $this->micro;
260-
}
261-
}
262-
263-
if ($this->date) {
264-
$version .= '-' . $this->date;
265-
}
266-
267-
if (!$this->stability->isStable()) {
268-
$version .= '-' . $this->stability;
269-
}
270-
271-
return (string)$version;
244+
$this->dateVersion = $dateVersion;
272245
}
273246

274247
/**
@@ -282,9 +255,11 @@ public function isRegular()
282255
public function getVersionStability()
283256
{
284257
$stability = $this->getStability()->getStability();
258+
285259
if ($stability === 'patch') {
286260
return 'stable';
287261
}
262+
288263
return $stability;
289264
}
290265

@@ -328,6 +303,52 @@ public function compare(Version $version)
328303
return 1;
329304
}
330305

306+
if (($this->dateVersion ?: 0) < ($version->dateVersion ?: 0)) {
307+
return -1;
308+
}
309+
310+
if (($this->dateVersion ?: 0) > ($version->dateVersion ?: 0)) {
311+
return 1;
312+
}
313+
331314
return $this->stability->compare($version->stability);
332315
}
316+
317+
public function __toString()
318+
{
319+
if ($this->regular) {
320+
$version =
321+
$this->major . '.' .
322+
$this->minor . '.' .
323+
$this->revision;
324+
if ($this->micro !== null) {
325+
$version .= '.' . $this->micro;
326+
}
327+
} else {
328+
$version = $this->major;
329+
if ($this->minor) {
330+
$version .= '-' . $this->minor;
331+
}
332+
if ($this->revision) {
333+
$version .= '-' . $this->revision;
334+
}
335+
if ($this->micro) {
336+
$version .= '-' . $this->micro;
337+
}
338+
}
339+
340+
if ($this->date) {
341+
$version .= '-' . $this->date;
342+
343+
if ($this->dateVersion) {
344+
$version .= '-' . $this->dateVersion;
345+
}
346+
}
347+
348+
if (!$this->stability->isStable()) {
349+
$version .= '-' . $this->stability;
350+
}
351+
352+
return (string)$version;
353+
}
333354
}

0 commit comments

Comments
 (0)