-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicEnum.php
More file actions
executable file
·52 lines (46 loc) · 1.62 KB
/
BasicEnum.php
File metadata and controls
executable file
·52 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace TestPlugin {
/**
* Class BasicEnum
* Because PHP doesn't natively support enums, this was made.
* Subclass this to make new enum types
*
* This was made from a comment made by the user "mohanrajnr at gmail dot com"
*
* https://www.php.net/manual/en/class.splenum.php
*
* @package TestPlugin
*/
abstract class BasicEnum
{
private static $constCacheArray = NULL;
private function __construct()
{
/* Prevent instancing */
}
private static function getConstants()
{
if (self::$constCacheArray == NULL) {
self::$constCacheArray = [];
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict = true);
}
}
}