Skip to content

Commit

Permalink
Recognize versions like: 2.5.0-2011-12-21-2
Browse files Browse the repository at this point in the history
  • Loading branch information
amouhzi committed Nov 14, 2019
1 parent 32e2d03 commit 25cf3fb
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 88 deletions.
70 changes: 41 additions & 29 deletions src/Version/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class Constraint
* Indicate if this constraint matches another constraint
*
* @param Constraint $constraint
*
* @return bool
*/
abstract public function matches(Constraint $constraint);
Expand All @@ -20,33 +21,37 @@ abstract public function matches(Constraint $constraint);
* Parse a string and return a Constraint.
*
* @param string $input
*
* @return Constraint
*
* @throws \Exception
*/
public static function parse($input)
{
$input = trim($input);

if (strlen($input) == 0) {
if ('' === $input) {
throw new \UnexpectedValueException('Empty.');
}

$input = explode(',', $input);
if (count($input) > 1) {
$inputParts = explode(',', $input);

if (count($inputParts) > 1) {
$and = true;
} else {
$input = explode('|', $input[0]);
$inputParts = explode('|', $inputParts[0]);
$and = false;
}

if (count($input) > 1) {
if (count($inputParts) > 1) {
$constraints = array();
foreach ($input as $constraint) {
foreach ($inputParts as $constraint) {
$constraints[] = self::parse($constraint);
}
return new MultiConstraint($constraints, $and);
}

$input = $input[0];
$inputParts = $inputParts[0];

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

if (preg_match($regex, $input, $matches)) {
if (preg_match($regex, $inputParts, $matches)) {
return new AnythingConstraint();
}

$regex = '/^' .
'(?:(' . Operator::REGEX . '))? *' .
'(' . Operator::REGEX . ')? *' .
'(?:(\d+|\*|x)\.)?' .
'(?:(\d+|\*|x)\.)?' .
'(?:(\d+|\*|x)\.)?' .
'(?:(\d+|\*|x))?' .
'(\d+|\*|x)?' .
'(?:' . Stability::REGEX . ')?' .
'$/i';

if (!preg_match($regex, $input, $matches)) {
throw new \UnexpectedValueException('Invalid type: ' . $input);
if (!preg_match($regex, $inputParts, $matches)) {
throw new \UnexpectedValueException('Invalid type: ' . $inputParts);
}

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

$parts = array();

if (isset($matches[2]) && strlen($matches[2]) > 0) {
if (isset($matches[2]) && '' !== $matches[2]) {
$parts[] = $matches[2];
}
if (isset($matches[3]) && strlen($matches[3]) > 0) {
if (isset($matches[3]) && '' !== $matches[3]) {
$parts[] = $matches[3];
}
if (isset($matches[4]) && strlen($matches[4]) > 0) {
if (isset($matches[4]) && '' !== $matches[4]) {
$parts[] = $matches[4];
}
if (isset($matches[5]) && strlen($matches[5]) > 0) {
if (isset($matches[5]) && '' !== $matches[5]) {
$parts[] = $matches[5];
}

if ((string)$operator == '~') {
if ((string)$operator === '~') {
$end = count($parts);
} else {
$end = null;
Expand All @@ -107,15 +112,15 @@ public static function parse($input)
$max = $parts;

if ($end) {
if ($end == 1) {
if ($end === 1) {
$max[0]++;
} elseif ($end == 2) {
} elseif ($end === 2) {
$max[0]++;
$max[1] = 0;
} elseif ($end == 3) {
} elseif ($end === 3) {
$max[1]++;
$max[2] = 0;
} elseif ($end == 4) {
} elseif ($end === 4) {
$max[2]++;
$max[3] = 0;
} else {
Expand All @@ -142,6 +147,7 @@ public static function parse($input)
}

$version = new Version($parts[0]);

if (isset($parts[1])) {
$version->setMinor($parts[1]);
}
Expand All @@ -152,40 +158,46 @@ public static function parse($input)
$version->setMicro($parts[3]);
}

if (isset($matches[6]) && strlen($matches[6]) > 0) {
if (strtolower($matches[5]) == 'rc') {
if (isset($matches[6]) && '' !== $matches[6]) {
if (strtolower($matches[5]) === 'rc') {
$stability = 'RC';
} elseif (in_array(strtolower($matches[6]), array('pl', 'patch', 'p'))) {
$stability = 'patch';
} elseif (in_array(strtolower($matches[6]), array('beta', 'b'))) {
$stability = 'beta';
} elseif (strtolower($matches[6]) == 'stable') {
} elseif (strtolower($matches[6]) === 'stable') {
$stability = 'stable';
} else {
throw new \UnexpectedValueException('Invalid type: ' . $input);
throw new \UnexpectedValueException('Invalid type: ' . $inputParts);
}

$version->setStability(new Stability($stability, $matches[7]));
}

foreach ($parts as $k => $v) {
if ($v != $max[$k]) {
if ($v !== $max[$k]) {
$maxVersion = new Version($max[0]);

if (isset($max[1])) {
$maxVersion->setMinor($max[1]);
}

if (isset($max[2])) {
$maxVersion->setRevision($max[2]);
}

if (isset($max[3])) {
$maxVersion->setMicro($max[3]);
}

if ((string)$version == '0.0.0.0') {
if ((string)$version === '0.0.0.0') {
return new SimpleConstraint(new Operator('<'), $maxVersion);
}
if (isset($matches[6]) && strtolower($matches[6]) == 'stable') {

if (isset($matches[6]) && strtolower($matches[6]) === 'stable') {
$version->setStability(new Stability());
}

return new MultiConstraint(array(
new SimpleConstraint(new Operator('>='), $version),
new SimpleConstraint(new Operator('<'), $maxVersion)
Expand Down
13 changes: 12 additions & 1 deletion src/Version/Stability.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Stability
{
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])))';
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*))';

/**
* @var string
Expand All @@ -21,7 +21,9 @@ public function __construct($stability = 'stable', $number = null)
if ('' === $stability) {
$stability = 'stable';
}

$stability = strtolower($stability);

switch ($stability) {
case 'rc':
$stability = 'RC';
Expand All @@ -44,6 +46,7 @@ public function __construct($stability = 'stable', $number = null)
$stability = 'dev';
break;
}

$this->stability = $stability;
$this->number = $number;
}
Expand Down Expand Up @@ -102,4 +105,12 @@ private function toInt($stability)
throw new \InvalidArgumentException('Invalid stability: ' . $stability);
}
}

/**
* @return int
*/
public function getNumber()
{
return $this->number;
}
}
93 changes: 57 additions & 36 deletions src/Version/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Version
*/
private $date;

/**
* @var int
*/
private $dateVersion;

/**
* @var bool
*/
Expand Down Expand Up @@ -127,7 +132,7 @@ public static function parse($input)
}

if (isset($matches['date']) && '' !== $matches['date']) {
$version->setDate($matches['date']);
$version->setDate($matches['date'], $matches['dateVersion']);
}

return $version;
Expand Down Expand Up @@ -231,44 +236,12 @@ public function getDate()

/**
* @param string $date
* @param int $dateVersion
*/
public function setDate($date)
public function setDate($date, $dateVersion)
{
$this->date = $date;
}

public function __toString()
{
if ($this->regular) {
$version =
$this->major . '.' .
$this->minor . '.' .
$this->revision;
if ($this->micro !== null) {
$version .= '.' . $this->micro;
}
} else {
$version = $this->major;
if ($this->minor) {
$version .= '-' . $this->minor;
}
if ($this->revision) {
$version .= '-' . $this->revision;
}
if ($this->micro) {
$version .= '-' . $this->micro;
}
}

if ($this->date) {
$version .= '-' . $this->date;
}

if (!$this->stability->isStable()) {
$version .= '-' . $this->stability;
}

return (string)$version;
$this->dateVersion = $dateVersion;
}

/**
Expand All @@ -282,9 +255,11 @@ public function isRegular()
public function getVersionStability()
{
$stability = $this->getStability()->getStability();

if ($stability === 'patch') {
return 'stable';
}

return $stability;
}

Expand Down Expand Up @@ -328,6 +303,52 @@ public function compare(Version $version)
return 1;
}

if (($this->dateVersion ?: 0) < ($version->dateVersion ?: 0)) {
return -1;
}

if (($this->dateVersion ?: 0) > ($version->dateVersion ?: 0)) {
return 1;
}

return $this->stability->compare($version->stability);
}

public function __toString()
{
if ($this->regular) {
$version =
$this->major . '.' .
$this->minor . '.' .
$this->revision;
if ($this->micro !== null) {
$version .= '.' . $this->micro;
}
} else {
$version = $this->major;
if ($this->minor) {
$version .= '-' . $this->minor;
}
if ($this->revision) {
$version .= '-' . $this->revision;
}
if ($this->micro) {
$version .= '-' . $this->micro;
}
}

if ($this->date) {
$version .= '-' . $this->date;

if ($this->dateVersion) {
$version .= '-' . $this->dateVersion;
}
}

if (!$this->stability->isStable()) {
$version .= '-' . $this->stability;
}

return (string)$version;
}
}
Loading

0 comments on commit 25cf3fb

Please sign in to comment.