1
|
|
|
<?php namespace BuildR\Utils\Enumeration; |
2
|
|
|
|
3
|
|
|
use \ReflectionClass; |
4
|
|
|
use BuildR\Foundation\Object\StringConvertibleInterface; |
5
|
|
|
use BuildR\Utils\Enumeration\Exception\EnumerationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Enumeration support for PHP |
9
|
|
|
* |
10
|
|
|
* BuildR PHP Framework |
11
|
|
|
* |
12
|
|
|
* @author Zoltán Borsos <[email protected]> |
13
|
|
|
* @package Utils |
14
|
|
|
* @subpackage Enumeration |
15
|
|
|
* |
16
|
|
|
* @copyright Copyright 2016, Zoltán Borsos. |
17
|
|
|
* @license https://github.com/BuildrPHP/Utils/blob/master/LICENSE.md |
18
|
|
|
* @link https://github.com/BuildrPHP/Utils |
19
|
|
|
*/ |
20
|
|
|
class EnumerationBase implements StringConvertibleInterface { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @type mixed |
24
|
|
|
*/ |
25
|
|
|
public $value; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @type array |
29
|
|
|
*/ |
30
|
|
|
private static $cache = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* EnumerationBase constructor |
34
|
|
|
* |
35
|
|
|
* @param mixed $value |
36
|
|
|
* |
37
|
|
|
* @throws \BuildR\Utils\Enumeration\Exception\EnumerationException |
38
|
|
|
*/ |
39
|
21 |
|
public function __construct($value) { |
40
|
21 |
|
if(!$this->isValid($value)) { |
41
|
4 |
|
throw EnumerationException::invalidValue($value); |
42
|
|
|
} |
43
|
|
|
|
44
|
21 |
|
$this->value = $value; |
45
|
21 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Determines that the current value in the enumeration is valid. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
21 |
|
public function isValid($value) { |
55
|
21 |
|
return (bool) in_array($value, self::toArray()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the enumeration current value |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
1 |
|
public function getValue() { |
64
|
1 |
|
return $this->value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Translate the current enumeration to an associative array |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
21 |
|
public static function toArray() { |
73
|
21 |
|
$enumClass = get_called_class(); |
74
|
|
|
|
75
|
21 |
|
if(!array_key_exists($enumClass, self::$cache)) { |
76
|
2 |
|
$reflector = new ReflectionClass($enumClass); |
77
|
2 |
|
self::$cache[$enumClass] = $reflector->getConstants(); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
21 |
|
return self::$cache[$enumClass]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an indexed array that contains all key from this enumeration as value. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
2 |
|
public static function getKeys() { |
89
|
2 |
|
return array_keys(self::toArray()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determines that the given key is exist in the current enumeration or not. |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
* |
99
|
|
|
* @throws \BuildR\Utils\Enumeration\Exception\EnumerationException |
100
|
|
|
*/ |
101
|
7 |
|
public static function isValidKey($key) { |
102
|
7 |
|
if(!is_string($key)) { |
103
|
6 |
|
throw EnumerationException::invalidKeyType(gettype($key)); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return array_key_exists($key, self::toArray()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the sie of the current enumeration |
111
|
|
|
* |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
1 |
|
public static function size() { |
115
|
1 |
|
return count(self::toArray()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* This is a proxy method. This is called when the enumeration property called as a method. |
120
|
|
|
* This method creates a new instance from the parent class with the value defined by |
121
|
|
|
* the constant. |
122
|
|
|
* |
123
|
|
|
* @param string $name The constant name |
124
|
|
|
* @param array $arguments This function not take any arguments |
125
|
|
|
* |
126
|
|
|
* @return object |
127
|
|
|
*/ |
128
|
21 |
|
public static function __callStatic($name, $arguments) { |
129
|
21 |
|
$calledClass = get_called_class(); |
130
|
21 |
|
$args[] = $name; |
|
|
|
|
131
|
|
|
|
132
|
21 |
|
if(array_key_exists(EnumerationFieldDefinitionInterface::class, class_implements($calledClass))) { |
133
|
2 |
|
$fields = static::defineFields()[$name]; |
134
|
2 |
|
array_unshift($fields, $args[0]); |
135
|
|
|
|
136
|
2 |
|
$args = $fields; |
137
|
2 |
|
} |
138
|
|
|
|
139
|
21 |
|
$reflector = new ReflectionClass($calledClass); |
140
|
21 |
|
return $reflector->newInstanceArgs($args); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// ========================================== |
144
|
|
|
// StringConvertibleInterface implementation |
145
|
|
|
// ========================================== |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
1 |
|
public function __toString() { |
151
|
1 |
|
return $this->value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
1 |
|
public function toString() { |
158
|
1 |
|
return $this->value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.