PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
PluginGraylist.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\plugin;
25
27use function array_flip;
28use function is_array;
29use function is_float;
30use function is_int;
31use function is_string;
32
34
36 private array $plugins;
37 private bool $isWhitelist = false;
38
42 public function __construct(array $plugins = [], bool $whitelist = false){
43 $this->plugins = array_flip($plugins);
44 $this->isWhitelist = $whitelist;
45 }
46
50 public function getPlugins() : array{
51 return array_flip($this->plugins);
52 }
53
54 public function isWhitelist() : bool{
55 return $this->isWhitelist;
56 }
57
61 public function isAllowed(string $name) : bool{
62 return $this->isWhitelist() === isset($this->plugins[$name]);
63 }
64
68 public static function fromArray(array $array) : PluginGraylist{
69 if(!isset($array["mode"]) || ($array["mode"] !== "whitelist" && $array["mode"] !== "blacklist")){
70 throw new \InvalidArgumentException("\"mode\" must be set");
71 }
72 $isWhitelist = match($array["mode"]){
73 "whitelist" => true,
74 "blacklist" => false
75 };
76 $plugins = [];
77 if(isset($array["plugins"])){
78 if(!is_array($array["plugins"])){
79 throw new \InvalidArgumentException("\"plugins\" must be an array");
80 }
81 foreach(Utils::promoteKeys($array["plugins"]) as $k => $v){
82 if(!is_string($v) && !is_int($v) && !is_float($v)){
83 throw new \InvalidArgumentException("\"plugins\" contains invalid element at position $k");
84 }
85 $plugins[] = (string) $v;
86 }
87 }
88 return new PluginGraylist($plugins, $isWhitelist);
89 }
90
95 public function toArray() : array{
96 return [
97 "mode" => $this->isWhitelist ? 'whitelist' : 'blacklist',
98 "plugins" => $this->plugins
99 ];
100 }
101}
__construct(array $plugins=[], bool $whitelist=false)