75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
namespace Patchouli;
|
|
|
|
use FWIF\FWIFSerializable;
|
|
use InvalidArgumentException;
|
|
|
|
class Version implements FWIFSerializable {
|
|
private int $major;
|
|
private int $minor;
|
|
private int $patch;
|
|
private string $suffix;
|
|
|
|
public function __construct(string $version) {
|
|
$parts = explode('.', $version, 3);
|
|
if(count($parts) < 3)
|
|
throw new InvalidArgumentException('Invalid version string.');
|
|
|
|
$lastParts = explode('-', $parts[2], 2);
|
|
$this->major = (int)$parts[0];
|
|
$this->minor = (int)$parts[1];
|
|
$this->patch = (int)$lastParts[0];
|
|
$this->suffix = $lastParts[1] ?? '';
|
|
}
|
|
|
|
public function getMajor(): int {
|
|
return $this->major;
|
|
}
|
|
|
|
public function getMinor(): int {
|
|
return $this->minor;
|
|
}
|
|
|
|
public function getPatch(): int {
|
|
return $this->patch;
|
|
}
|
|
|
|
public function getSuffix(): string {
|
|
return $this->suffix;
|
|
}
|
|
|
|
public function match(Version $other): bool {
|
|
return $this->getMajor() === $other->getMajor()
|
|
&& $this->getMinor() === $other->getMinor()
|
|
&& $this->getPatch() === $other->getPatch()
|
|
&& $this->getSuffix() === $other->getSuffix();
|
|
}
|
|
|
|
public function compareTo(Version $other): int {
|
|
if($this->getMajor() !== $other->getMajor())
|
|
return $this->getMajor() - $other->getMajor();
|
|
if($this->getMinor() !== $other->getMinor())
|
|
return $this->getMinor() - $other->getMinor();
|
|
if($this->getPatch() !== $other->getPatch())
|
|
return $this->getPatch() - $other->getPatch();
|
|
if($this->getSuffix() === $this->getSuffix())
|
|
return 0;
|
|
if($this->getSuffix() === '')
|
|
return 1;
|
|
if($other->getSuffix() === '')
|
|
return -1;
|
|
return strcmp($this->getSuffix(), $this->getSuffix());
|
|
}
|
|
|
|
public function __toString(): string {
|
|
$version = "{$this->getMajor()}.{$this->getMinor()}.{$this->getPatch()}";
|
|
if(!empty($suffix = $this->getSuffix()))
|
|
$version .= "-{$suffix}";
|
|
return $version;
|
|
}
|
|
|
|
public function fwifSerialize(): string {
|
|
return (string)$this;
|
|
}
|
|
}
|