@@ -29,7 +29,7 @@ abstract class Enum
29
29
*
30
30
* @var array
31
31
*/
32
- private static $ cache = array ();
32
+ protected static $ cache = array ();
33
33
34
34
/**
35
35
* Creates a new value of some type
@@ -62,15 +62,15 @@ public function getValue()
62
62
*/
63
63
public function getKey ()
64
64
{
65
- return self ::search ($ this ->value );
65
+ return static ::search ($ this ->value );
66
66
}
67
67
68
68
/**
69
69
* @return string
70
70
*/
71
71
public function __toString ()
72
72
{
73
- return (string ) $ this ->value ;
73
+ return (string )$ this ->value ;
74
74
}
75
75
76
76
/**
@@ -80,7 +80,7 @@ public function __toString()
80
80
*/
81
81
public static function keys ()
82
82
{
83
- return array_keys (self ::toArray ());
83
+ return array_keys (static ::toArray ());
84
84
}
85
85
86
86
/**
@@ -92,7 +92,7 @@ public static function values()
92
92
{
93
93
$ values = array ();
94
94
95
- foreach (self ::toArray () as $ key => $ value ) {
95
+ foreach (static ::toArray () as $ key => $ value ) {
96
96
$ values [$ key ] = new static ($ value );
97
97
}
98
98
@@ -107,23 +107,24 @@ public static function values()
107
107
public static function toArray ()
108
108
{
109
109
$ class = get_called_class ();
110
- if (!array_key_exists ($ class , self ::$ cache )) {
111
- $ reflection = new \ReflectionClass ($ class );
112
- self ::$ cache [$ class ] = $ reflection ->getConstants ();
110
+ if (!array_key_exists ($ class , static ::$ cache )) {
111
+ $ reflection = new \ReflectionClass ($ class );
112
+ static ::$ cache [$ class ] = $ reflection ->getConstants ();
113
113
}
114
114
115
- return self ::$ cache [$ class ];
115
+ return static ::$ cache [$ class ];
116
116
}
117
117
118
118
/**
119
119
* Check if is valid enum value
120
120
*
121
121
* @param $value
122
+ *
122
123
* @return bool
123
124
*/
124
125
public static function isValid ($ value )
125
126
{
126
- return in_array ($ value , self ::toArray (), true );
127
+ return in_array ($ value , static ::toArray (), true );
127
128
}
128
129
129
130
/**
@@ -135,7 +136,8 @@ public static function isValid($value)
135
136
*/
136
137
public static function isValidKey ($ key )
137
138
{
138
- $ array = self ::toArray ();
139
+ $ array = static ::toArray ();
140
+
139
141
return isset ($ array [$ key ]);
140
142
}
141
143
@@ -148,7 +150,7 @@ public static function isValidKey($key)
148
150
*/
149
151
public static function search ($ value )
150
152
{
151
- return array_search ($ value , self ::toArray (), true );
153
+ return array_search ($ value , static ::toArray (), true );
152
154
}
153
155
154
156
/**
@@ -162,8 +164,9 @@ public static function search($value)
162
164
*/
163
165
public static function __callStatic ($ name , $ arguments )
164
166
{
165
- if (defined ("static:: $ name " )) {
166
- return new static (constant ("static:: $ name " ));
167
+ $ array = static ::toArray ();
168
+ if (isset ($ array [$ name ])) {
169
+ return new static ($ array [$ name ]);
167
170
}
168
171
169
172
throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . get_called_class ());
0 commit comments