PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
VersionString.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
26use function count;
27use function preg_match;
28
33 private int $major;
34 private int $minor;
35 private int $patch;
36 private string $suffix;
37
38 public function __construct(
39 private string $baseVersion,
40 private bool $isDevBuild = false,
41 private int $buildNumber = 0
42 ){
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");
46 }
47
48 $this->major = (int) $matches[1];
49 $this->minor = (int) $matches[2];
50 $this->patch = (int) $matches[3];
51 $this->suffix = $matches[4] ?? "";
52 }
53
54 public static function isValidBaseVersion(string $baseVersion) : bool{
55 return preg_match('/^\d+\.\d+\.\d+(?:-(.*))?$/', $baseVersion, $matches) === 1;
56 }
57
58 public function getNumber() : int{
59 return (($this->major * 1_000_000) + ($this->minor * 1_000) + $this->patch);
60 }
61
62 public function getBaseVersion() : string{
63 return $this->baseVersion;
64 }
65
66 public function getFullVersion(bool $build = false) : string{
67 $retval = $this->baseVersion;
68 if($this->isDevBuild){
69 $retval .= "+dev";
70 if($build && $this->buildNumber > 0){
71 $retval .= "." . $this->buildNumber;
72 }
73 }
74
75 return $retval;
76 }
77
78 public function getMajor() : int{
79 return $this->major;
80 }
81
82 public function getMinor() : int{
83 return $this->minor;
84 }
85
86 public function getPatch() : int{
87 return $this->patch;
88 }
89
90 public function getSuffix() : string{
91 return $this->suffix;
92 }
93
94 public function getBuild() : int{
95 return $this->buildNumber;
96 }
97
98 public function isDev() : bool{
99 return $this->isDevBuild;
100 }
101
102 public function __toString() : string{
103 return $this->getFullVersion();
104 }
105
106 public function compare(VersionString $target, bool $diff = false) : int{
107 $number = $this->getNumber();
108 $tNumber = $target->getNumber();
109 if($diff){
110 return $tNumber - $number;
111 }
112
113 if(($result = $tNumber <=> $number) !== 0){
114 return $result;
115 }
116 if($target->isDev() !== $this->isDev()){
117 return $this->isDev() ? 1 : -1; //Dev builds of the same version are always considered older than a release
118 }
119 if(($target->getSuffix() === "") !== ($this->suffix === "")){
120 return $this->suffix !== "" ? 1 : -1; //alpha/beta/whatever releases are always considered older than a non-suffixed version
121 }
122 return $target->getBuild() <=> $this->getBuild();
123 }
124}