-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase-enum.js
81 lines (69 loc) · 1.66 KB
/
base-enum.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module.exports = `<?php
namespace App\\Enums;
use ReflectionClass;
abstract class BaseEnum
{
/**
* Store existing constants in a static cache per object.
*
*
* @var array
*/
protected static $cache = [];
/**
* Get an array of constants in class. Constant name in key, constant value in value.
*
* @return array
*/
public static function pairs()
{
$class = static::class;
if (!isset(static::$cache[$class])) {
static::$cache[$class] = (new ReflectionClass($class))->getConstants();
}
return static::$cache[$class];
}
/**
* Get array containing keys of constants in this class.
*
* @return array
*/
public static function keys()
{
return \\array_keys(static::pairs());
}
/**
* Get array containing values of constants in this class.
*
* @return array
*/
public static function values()
{
return \\array_values(static::pairs());
}
/**
* Check if value is a valid key.
*/
public static function isValidKey($value, $strict = \\false)
{
return \\in_array($value, static::keys(), $strict);
}
/**
* Check if value is a valid value.
*/
public static function isValidValue($value, $strict = \\false)
{
return \\in_array($value, static::values(), $strict);
}
/**
* Check if value is a valid key or value.
*
* @param mixed $valueOrKey
* @return boolean
*/
public static function isValid($valueOrKey)
{
return static::isValidKey($valueOrKey) || static::isValidValue($valueOrKey);
}
}
`