PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ApiVersion.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
25
27use function array_map;
28use function array_push;
29use function count;
30use function usort;
31
32final class ApiVersion{
33
34 private function __construct(){
35 //NOOP
36 }
37
41 public static function isCompatible(string $myVersionStr, array $wantVersionsStr) : bool{
42 $myVersion = new VersionString($myVersionStr);
43 foreach($wantVersionsStr as $versionStr){
44 $version = new VersionString($versionStr);
45 //Format: majorVersion.minorVersion.patch (3.0.0)
46 // or: majorVersion.minorVersion.patch-devBuild (3.0.0-alpha1)
47 if($version->getBaseVersion() !== $myVersion->getBaseVersion()){
48 if($version->getMajor() !== $myVersion->getMajor()){
49 continue;
50 }
51
52 if($version->getMinor() > $myVersion->getMinor()){ //If the plugin requires new API features, being backwards compatible
53 continue;
54 }
55
56 if($version->getMinor() === $myVersion->getMinor() && $version->getPatch() > $myVersion->getPatch()){ //If the plugin requires bug fixes in patches, being backwards compatible
57 continue;
58 }
59 }
60
61 return true;
62 }
63
64 return false;
65 }
66
72 public static function checkAmbiguousVersions(array $versions) : array{
73 $indexedVersions = [];
74
75 foreach($versions as $str){
76 $v = new VersionString($str);
77 if($v->getSuffix() !== ""){ //suffix is always unambiguous
78 continue;
79 }
80 if(!isset($indexedVersions[$v->getMajor()])){
81 $indexedVersions[$v->getMajor()] = [$v];
82 }else{
83 $indexedVersions[$v->getMajor()][] = $v;
84 }
85 }
86
87 $result = [];
88 foreach($indexedVersions as $list){
89 if(count($list) > 1){
90 array_push($result, ...$list);
91 }
92 }
93
94 usort($result, static function(VersionString $string1, VersionString $string2) : int{ return $string1->compare($string2); });
95
96 return array_map(static function(VersionString $string) : string{ return $string->getBaseVersion(); }, $result);
97 }
98}
static isCompatible(string $myVersionStr, array $wantVersionsStr)
static checkAmbiguousVersions(array $versions)