PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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_fill_keys;
28use function array_keys;
29use function is_array;
30use function is_float;
31use function is_int;
32use function is_string;
33
35
40 private array $plugins;
41 private bool $isWhitelist = false;
42
47 public function __construct(array $plugins = [], bool $whitelist = false){
48 $this->plugins = array_fill_keys($plugins, true);
49 $this->isWhitelist = $whitelist;
50 }
51
56 public function getPlugins() : array{
57 return array_keys($this->plugins);
58 }
59
60 public function isWhitelist() : bool{
61 return $this->isWhitelist;
62 }
63
67 public function isAllowed(string $name) : bool{
68 return $this->isWhitelist() === isset($this->plugins[$name]);
69 }
70
74 public static function fromArray(array $array) : PluginGraylist{
75 if(!isset($array["mode"]) || ($array["mode"] !== "whitelist" && $array["mode"] !== "blacklist")){
76 throw new \InvalidArgumentException("\"mode\" must be set");
77 }
78 $isWhitelist = match($array["mode"]){
79 "whitelist" => true,
80 "blacklist" => false
81 };
82 $plugins = [];
83 if(isset($array["plugins"])){
84 if(!is_array($array["plugins"])){
85 throw new \InvalidArgumentException("\"plugins\" must be an array");
86 }
87 foreach(Utils::promoteKeys($array["plugins"]) as $k => $v){
88 if(!is_string($v) && !is_int($v) && !is_float($v)){
89 throw new \InvalidArgumentException("\"plugins\" contains invalid element at position $k");
90 }
91 $plugins[] = (string) $v;
92 }
93 }
94 return new PluginGraylist($plugins, $isWhitelist);
95 }
96
101 public function toArray() : array{
102 return [
103 "mode" => $this->isWhitelist ? 'whitelist' : 'blacklist',
104 "plugins" => $this->plugins
105 ];
106 }
107}
__construct(array $plugins=[], bool $whitelist=false)