@ 2014-10-22T18:07:19Z <?php
class Version
{
const STAGE_ALPHA = 1;
const STAGE_BETA = 2;
const STAGE_RC = 3;
const STAGE_RELEASE = 4;
public $major;
public $minor;
public $release;
public $stageName;
public $stageNo;
private $stageMap = [
'alpha' => self::STAGE_ALPHA,
'beta' => self::STAGE_BETA,
'rc' => self::STAGE_RC,
];
/**
* @param string $version
* @throws \Exception
*/
public function __construct($version)
{
static $tagNameExpr = '#
^
v?
(?P<major>[0-9]+)
\.
(?P<minor>[0-9]+)
(?:\.(?P<release>[0-9]+))?
(?:-
(?P<stage>alpha|beta|rc)
(?:(?P<stageno>\d+))?
)?
$
#ix';
if (!preg_match($tagNameExpr, trim(strtolower($version)), $match)) {
throw new \Exception('Invalid version spec string');
}
$this->major = (int)$match['major'];
$this->minor = (int)$match['minor'];
$this->release = isset($match['release']) ? (int)$match['release'] : null;
$this->stageName = isset($match['stage']) ? $match['stage'] : '';
$this->stageNo = isset($match['stageno']) ? (int)$match['stageno'] : null;
}
/**
* @return string
*/
public function __toString()
{
return "$this->major.$this->minor."
. ($this->release ?: 0)
. ($this->stageNo ? '-' . $this->stageName . $this->stageNo : '');
}
/**
* @param string[] $stages
* @return bool
*/
public function isValidNextStage(array $stages)
{
$latestStageType = $latestStagePoint = 0;
foreach ($stages as $stage) {
if ($stage === '') {
// a release already exists
return false;
}
if (preg_match('#^(alpha|beta|rc)(\d+)$#', strtolower($stage), $match)) {
if ($this->stageMap[$match[1]] > $latestStageType) {
$latestStageType = $this->stageMap[$match[1]];
$latestStagePoint = 0;
} else if ($this->stageMap[$match[1]] === $latestStageType && $match[2] > $latestStagePoint) {
$latestStagePoint = (int)$match[2];
}
}
}
if (!$this->stageName) {
// this is a release and one does not yet exist
return true;
}
if (!isset($this->stageMap[$this->stageName])) {
// invalid new stage
return false;
}
if ($this->stageMap[$this->stageName] < $latestStageType) {
// new stage lower than latest
return false;
}
if ($this->stageMap[$this->stageName] > $latestStageType) {
// new stage greater than latest, number must be 1
if (!isset($this->stageNo)) {
$this->stageNo = 1;
return true;
}
return $this->stageNo === 1;
}
if (!isset($this->stageNo)) {
$this->stageNo = $latestStagePoint + 1;
return true;
}
return $this->stageNo === $latestStagePoint + 1;
}
}
var_dump(new Version('1.12-beta'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.10 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 object(Version)#1 (6) {
["major"]=>
int(1)
["minor"]=>
int(12)
["release"]=>
int(0)
["stageName"]=>
string(4) "beta"
["stageNo"]=>
NULL
["stageMap":"Version":private]=>
array(3) {
["alpha"]=>
int(1)
["beta"]=>
int(2)
["rc"]=>
int(3)
}
}
Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/pvldm on line 16
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/pvldm on line 16
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/pvldm on line 5
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/pvldm on line 5
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/pvldm on line 5
Process exited with code 255 . preferences:dark mode live preview
193.57 ms | 401 KiB | 311 Q