PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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{
74 $indexedVersions = [];
75
76 foreach($versions as $str){
77 $v = new VersionString($str);
78 if($v->getSuffix() !== ""){ //suffix is always unambiguous
79 continue;
80 }
81 if(!isset($indexedVersions[$v->getMajor()])){
82 $indexedVersions[$v->getMajor()] = [$v];
83 }else{
84 $indexedVersions[$v->getMajor()][] = $v;
85 }
86 }
87
89 $result = [];
90 foreach($indexedVersions as $major => $list){
91 if(count($list) > 1){
92 array_push($result, ...$list);
93 }
94 }
95
96 usort($result, static function(VersionString $string1, VersionString $string2) : int{ return $string1->compare($string2); });
97
98 return array_map(static function(VersionString $string) : string{ return $string->getBaseVersion(); }, $result);
99 }
100}
static isCompatible(string $myVersionStr, array $wantVersionsStr)
Definition: ApiVersion.php:41
static checkAmbiguousVersions(array $versions)
Definition: ApiVersion.php:72