|
1
|
|
|
<?php namespace BuildR\Utils\Enumeration; |
|
2
|
|
|
|
|
3
|
|
|
use \ReflectionClass; |
|
4
|
|
|
use \Countable; |
|
5
|
|
|
use BuildR\Foundation\Object\StringConvertibleInterface; |
|
6
|
|
|
use BuildR\Utils\Enumeration\Exception\EnumerationException; |
|
7
|
|
|
|
|
8
|
|
|
class EnumerationBase implements StringConvertibleInterface, Countable { |
|
9
|
|
|
|
|
10
|
|
|
public $value; |
|
11
|
|
|
|
|
12
|
|
|
private static $cache = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($value) { |
|
15
|
|
|
if(!$this->isValid($value)) { |
|
16
|
|
|
throw EnumerationException::invalidValue($value); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$this->value = $value; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function isValid($value) { |
|
23
|
|
|
return (bool) in_array($value, self::toArray()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getValue() { |
|
27
|
|
|
return $this->value; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function toArray() { |
|
31
|
|
|
$enumClass = get_called_class(); |
|
32
|
|
|
|
|
33
|
|
|
if(!array_key_exists($enumClass, self::$cache)) { |
|
34
|
|
|
$reflector = new ReflectionClass($enumClass); |
|
35
|
|
|
self::$cache[$enumClass] = $reflector->getConstants(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return self::$cache[$enumClass]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function getKeys() { |
|
42
|
|
|
return array_keys(self::toArray()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function isValidKey($key) { |
|
46
|
|
|
if(!is_string($key)) { |
|
47
|
|
|
throw EnumerationException::invalidKeyType(gettype($key)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return array_key_exists($key, self::toArray()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function __callStatic($name, $arguments) { |
|
|
|
|
|
|
54
|
|
|
$calledClass = get_called_class(); |
|
55
|
|
|
$args[] = $name; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
if(array_key_exists(EnumerationFieldDefinitionInterface::class, class_implements($calledClass))) { |
|
58
|
|
|
$fields = static::defineFields()[$name]; |
|
59
|
|
|
array_unshift($fields, $args[0]); |
|
60
|
|
|
|
|
61
|
|
|
$args = $fields; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$reflector = new ReflectionClass($calledClass); |
|
65
|
|
|
return $reflector->newInstanceArgs($args); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// ========================================== |
|
69
|
|
|
// StringConvertibleInterface implementation |
|
70
|
|
|
// ========================================== |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __toString() { |
|
76
|
|
|
return $this->value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function toString() { |
|
83
|
|
|
return $this->value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// =================================== |
|
87
|
|
|
// Countable interface implementation |
|
88
|
|
|
// =================================== |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function count() { |
|
94
|
|
|
return count(self::toArray()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.