36 private string $suffix;
38 public function __construct(
39 private string $baseVersion,
40 private bool $isDevBuild =
false,
41 private int $buildNumber = 0
43 preg_match(
'/^(\d+)\.(\d+)\.(\d+)(?:-(.*))?$/', $this->baseVersion, $matches);
44 if(count($matches) < 4){
45 throw new \InvalidArgumentException(
"Invalid base version \"$baseVersion\", should contain at least 3 version digits");
48 $this->major = (int) $matches[1];
49 $this->minor = (int) $matches[2];
50 $this->patch = (int) $matches[3];
51 $this->suffix = $matches[4] ??
"";
54 public static function isValidBaseVersion(
string $baseVersion) :
bool{
55 return preg_match(
'/^\d+\.\d+\.\d+(?:-(.*))?$/', $baseVersion, $matches) === 1;
58 public function getNumber() :
int{
59 return (($this->major * 1_000_000) + ($this->minor * 1_000) + $this->patch);
62 public function getBaseVersion() :
string{
63 return $this->baseVersion;
66 public function getFullVersion(
bool $build =
false) :
string{
67 $retval = $this->baseVersion;
68 if($this->isDevBuild){
70 if($build && $this->buildNumber > 0){
71 $retval .=
"." . $this->buildNumber;
78 public function getMajor() :
int{
82 public function getMinor() :
int{
86 public function getPatch() :
int{
90 public function getSuffix() :
string{
94 public function getBuild() :
int{
95 return $this->buildNumber;
98 public function isDev() :
bool{
99 return $this->isDevBuild;
102 public function __toString() :
string{
103 return $this->getFullVersion();
106 public function compare(
VersionString $target,
bool $diff =
false) :
int{
107 $number = $this->getNumber();
108 $tNumber = $target->getNumber();
110 return $tNumber - $number;
113 if(($result = $tNumber <=> $number) !== 0){
116 if($target->isDev() !== $this->isDev()){
117 return $this->isDev() ? 1 : -1;
119 if(($target->getSuffix() ===
"") !== ($this->suffix ===
"")){
120 return $this->suffix !==
"" ? 1 : -1;
122 return $target->getBuild() <=> $this->getBuild();