PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.4.2
WP 2FA – Two-factor authentication for WordPress v2.4.2
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / vendor / myclabs / php-enum / src / Enum.php
wp-2fa / vendor / myclabs / php-enum / src Last commit date
PHPUnit 3 years ago Enum.php 3 years ago
Enum.php
288 lines
1 <?php
2
3 /**
4 * @link http://github.com/myclabs/php-enum
5 * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
6 */
7 namespace WP2FA_Vendor\MyCLabs\Enum;
8
9 /**
10 * Base Enum class
11 *
12 * Create an enum by implementing this class and adding class constants.
13 *
14 * @author Matthieu Napoli <matthieu@mnapoli.fr>
15 * @author Daniel Costa <danielcosta@gmail.com>
16 * @author Mirosław Filip <mirfilip@gmail.com>
17 *
18 * @psalm-template T
19 * @psalm-immutable
20 * @psalm-consistent-constructor
21 */
22 abstract class Enum implements \JsonSerializable, \Stringable
23 {
24 /**
25 * Enum value
26 *
27 * @var mixed
28 * @psalm-var T
29 */
30 protected $value;
31 /**
32 * Enum key, the constant name
33 *
34 * @var string
35 */
36 private $key;
37 /**
38 * Store existing constants in a static cache per object.
39 *
40 *
41 * @var array
42 * @psalm-var array<class-string, array<string, mixed>>
43 */
44 protected static $cache = [];
45 /**
46 * Cache of instances of the Enum class
47 *
48 * @var array
49 * @psalm-var array<class-string, array<string, static>>
50 */
51 protected static $instances = [];
52 /**
53 * Creates a new value of some type
54 *
55 * @psalm-pure
56 * @param mixed $value
57 *
58 * @psalm-param T $value
59 * @throws \UnexpectedValueException if incompatible type is given.
60 */
61 public function __construct($value)
62 {
63 if ($value instanceof static) {
64 /** @psalm-var T */
65 $value = $value->getValue();
66 }
67 /** @psalm-suppress ImplicitToStringCast assertValidValueReturningKey returns always a string but psalm has currently an issue here */
68 $this->key = static::assertValidValueReturningKey($value);
69 /** @psalm-var T */
70 $this->value = $value;
71 }
72 /**
73 * This method exists only for the compatibility reason when deserializing a previously serialized version
74 * that didn't had the key property
75 */
76 public function __wakeup()
77 {
78 /** @psalm-suppress DocblockTypeContradiction key can be null when deserializing an enum without the key */
79 if ($this->key === null) {
80 /**
81 * @psalm-suppress InaccessibleProperty key is not readonly as marked by psalm
82 * @psalm-suppress PossiblyFalsePropertyAssignmentValue deserializing a case that was removed
83 */
84 $this->key = static::search($this->value);
85 }
86 }
87 /**
88 * @param mixed $value
89 * @return static
90 */
91 public static function from($value) : self
92 {
93 $key = static::assertValidValueReturningKey($value);
94 return self::__callStatic($key, []);
95 }
96 /**
97 * @psalm-pure
98 * @return mixed
99 * @psalm-return T
100 */
101 public function getValue()
102 {
103 return $this->value;
104 }
105 /**
106 * Returns the enum key (i.e. the constant name).
107 *
108 * @psalm-pure
109 * @return string
110 */
111 public function getKey()
112 {
113 return $this->key;
114 }
115 /**
116 * @psalm-pure
117 * @psalm-suppress InvalidCast
118 * @return string
119 */
120 public function __toString()
121 {
122 return (string) $this->value;
123 }
124 /**
125 * Determines if Enum should be considered equal with the variable passed as a parameter.
126 * Returns false if an argument is an object of different class or not an object.
127 *
128 * This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
129 *
130 * @psalm-pure
131 * @psalm-param mixed $variable
132 * @return bool
133 */
134 public final function equals($variable = null) : bool
135 {
136 return $variable instanceof self && $this->getValue() === $variable->getValue() && static::class === \get_class($variable);
137 }
138 /**
139 * Returns the names (keys) of all constants in the Enum class
140 *
141 * @psalm-pure
142 * @psalm-return list<string>
143 * @return array
144 */
145 public static function keys()
146 {
147 return \array_keys(static::toArray());
148 }
149 /**
150 * Returns instances of the Enum class of all Enum constants
151 *
152 * @psalm-pure
153 * @psalm-return array<string, static>
154 * @return static[] Constant name in key, Enum instance in value
155 */
156 public static function values()
157 {
158 $values = array();
159 /** @psalm-var T $value */
160 foreach (static::toArray() as $key => $value) {
161 $values[$key] = new static($value);
162 }
163 return $values;
164 }
165 /**
166 * Returns all possible values as an array
167 *
168 * @psalm-pure
169 * @psalm-suppress ImpureStaticProperty
170 *
171 * @psalm-return array<string, mixed>
172 * @return array Constant name in key, constant value in value
173 */
174 public static function toArray()
175 {
176 $class = static::class;
177 if (!isset(static::$cache[$class])) {
178 /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
179 $reflection = new \ReflectionClass($class);
180 /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
181 static::$cache[$class] = $reflection->getConstants();
182 }
183 return static::$cache[$class];
184 }
185 /**
186 * Check if is valid enum value
187 *
188 * @param $value
189 * @psalm-param mixed $value
190 * @psalm-pure
191 * @psalm-assert-if-true T $value
192 * @return bool
193 */
194 public static function isValid($value)
195 {
196 return \in_array($value, static::toArray(), \true);
197 }
198 /**
199 * Asserts valid enum value
200 *
201 * @psalm-pure
202 * @psalm-assert T $value
203 * @param mixed $value
204 */
205 public static function assertValidValue($value) : void
206 {
207 self::assertValidValueReturningKey($value);
208 }
209 /**
210 * Asserts valid enum value
211 *
212 * @psalm-pure
213 * @psalm-assert T $value
214 * @param mixed $value
215 * @return string
216 */
217 private static function assertValidValueReturningKey($value) : string
218 {
219 if (\false === ($key = static::search($value))) {
220 throw new \UnexpectedValueException("Value '{$value}' is not part of the enum " . static::class);
221 }
222 return $key;
223 }
224 /**
225 * Check if is valid enum key
226 *
227 * @param $key
228 * @psalm-param string $key
229 * @psalm-pure
230 * @return bool
231 */
232 public static function isValidKey($key)
233 {
234 $array = static::toArray();
235 return isset($array[$key]) || \array_key_exists($key, $array);
236 }
237 /**
238 * Return key for value
239 *
240 * @param mixed $value
241 *
242 * @psalm-param mixed $value
243 * @psalm-pure
244 * @return string|false
245 */
246 public static function search($value)
247 {
248 return \array_search($value, static::toArray(), \true);
249 }
250 /**
251 * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
252 *
253 * @param string $name
254 * @param array $arguments
255 *
256 * @return static
257 * @throws \BadMethodCallException
258 *
259 * @psalm-pure
260 */
261 public static function __callStatic($name, $arguments)
262 {
263 $class = static::class;
264 if (!isset(self::$instances[$class][$name])) {
265 $array = static::toArray();
266 if (!isset($array[$name]) && !\array_key_exists($name, $array)) {
267 $message = "No static method or enum constant '{$name}' in class " . static::class;
268 throw new \BadMethodCallException($message);
269 }
270 return self::$instances[$class][$name] = new static($array[$name]);
271 }
272 return clone self::$instances[$class][$name];
273 }
274 /**
275 * Specify data which should be serialized to JSON. This method returns data that can be serialized by json_encode()
276 * natively.
277 *
278 * @return mixed
279 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
280 * @psalm-pure
281 */
282 #[\ReturnTypeWillChange]
283 public function jsonSerialize()
284 {
285 return $this->getValue();
286 }
287 }
288