Skip to content

Commit 07da9d1

Browse files
committed
Merge pull request #21 from lorenzomar/master
Change calls to method toArray() from self::toArray() to static::toArray()
2 parents 7c4cd65 + d99b9ff commit 07da9d1

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/Enum.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class Enum
2929
*
3030
* @var array
3131
*/
32-
private static $cache = array();
32+
protected static $cache = array();
3333

3434
/**
3535
* Creates a new value of some type
@@ -62,15 +62,15 @@ public function getValue()
6262
*/
6363
public function getKey()
6464
{
65-
return self::search($this->value);
65+
return static::search($this->value);
6666
}
6767

6868
/**
6969
* @return string
7070
*/
7171
public function __toString()
7272
{
73-
return (string) $this->value;
73+
return (string)$this->value;
7474
}
7575

7676
/**
@@ -80,7 +80,7 @@ public function __toString()
8080
*/
8181
public static function keys()
8282
{
83-
return array_keys(self::toArray());
83+
return array_keys(static::toArray());
8484
}
8585

8686
/**
@@ -92,7 +92,7 @@ public static function values()
9292
{
9393
$values = array();
9494

95-
foreach (self::toArray() as $key => $value) {
95+
foreach (static::toArray() as $key => $value) {
9696
$values[$key] = new static($value);
9797
}
9898

@@ -107,23 +107,24 @@ public static function values()
107107
public static function toArray()
108108
{
109109
$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();
113113
}
114114

115-
return self::$cache[$class];
115+
return static::$cache[$class];
116116
}
117117

118118
/**
119119
* Check if is valid enum value
120120
*
121121
* @param $value
122+
*
122123
* @return bool
123124
*/
124125
public static function isValid($value)
125126
{
126-
return in_array($value, self::toArray(), true);
127+
return in_array($value, static::toArray(), true);
127128
}
128129

129130
/**
@@ -135,7 +136,8 @@ public static function isValid($value)
135136
*/
136137
public static function isValidKey($key)
137138
{
138-
$array = self::toArray();
139+
$array = static::toArray();
140+
139141
return isset($array[$key]);
140142
}
141143

@@ -148,7 +150,7 @@ public static function isValidKey($key)
148150
*/
149151
public static function search($value)
150152
{
151-
return array_search($value, self::toArray(), true);
153+
return array_search($value, static::toArray(), true);
152154
}
153155

154156
/**
@@ -162,8 +164,9 @@ public static function search($value)
162164
*/
163165
public static function __callStatic($name, $arguments)
164166
{
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]);
167170
}
168171

169172
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());

0 commit comments

Comments
 (0)