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