From 1e25f30cf0bbd08595c5ac310ab3576a3f463f8b Mon Sep 17 00:00:00 2001 From: Lorenzo Marzullo Date: Tue, 21 Jul 2015 14:53:02 +0200 Subject: [PATCH 1/4] Change call to method toArray() from self::toArray() to static::toArray() --- src/Enum.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index f44fdbd..5ccdd8e 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -80,7 +80,7 @@ public function __toString() */ public static function keys() { - return array_keys(self::toArray()); + return array_keys(static::toArray()); } /** @@ -92,7 +92,7 @@ public static function values() { $values = array(); - foreach (self::toArray() as $key => $value) { + foreach (static::toArray() as $key => $value) { $values[$key] = new static($value); } @@ -123,7 +123,7 @@ public static function toArray() */ public static function isValid($value) { - return in_array($value, self::toArray(), true); + return in_array($value, static::toArray(), true); } /** @@ -135,7 +135,7 @@ public static function isValid($value) */ public static function isValidKey($key) { - $array = self::toArray(); + $array = static::toArray(); return isset($array[$key]); } @@ -148,7 +148,7 @@ public static function isValidKey($key) */ public static function search($value) { - return array_search($value, self::toArray(), true); + return array_search($value, static::toArray(), true); } /** From cc43c4a110c93eb359ea32986d8328669b677580 Mon Sep 17 00:00:00 2001 From: Lorenzo Marzullo Date: Tue, 21 Jul 2015 15:25:39 +0200 Subject: [PATCH 2/4] Update __callStatic to use data provided by toArray() instead of constants list --- src/Enum.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index 5ccdd8e..34a6e6f 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -70,7 +70,7 @@ public function getKey() */ public function __toString() { - return (string) $this->value; + return (string)$this->value; } /** @@ -108,7 +108,7 @@ public static function toArray() { $class = get_called_class(); if (!array_key_exists($class, self::$cache)) { - $reflection = new \ReflectionClass($class); + $reflection = new \ReflectionClass($class); self::$cache[$class] = $reflection->getConstants(); } @@ -119,6 +119,7 @@ public static function toArray() * Check if is valid enum value * * @param $value + * * @return bool */ public static function isValid($value) @@ -136,6 +137,7 @@ public static function isValid($value) public static function isValidKey($key) { $array = static::toArray(); + return isset($array[$key]); } @@ -162,8 +164,10 @@ public static function search($value) */ public static function __callStatic($name, $arguments) { - if (defined("static::$name")) { - return new static(constant("static::$name")); + if (static::isValidKey($name)) { + $array = static::toArray(); + + return new static($array[$name]); } throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class()); From 9951a06df30af8fd019df7a84b8e7fb060aae8d3 Mon Sep 17 00:00:00 2001 From: Lorenzo Marzullo Date: Wed, 22 Jul 2015 10:46:25 +0200 Subject: [PATCH 3/4] Cache from private to protected --- src/Enum.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index 34a6e6f..e95c577 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -29,7 +29,7 @@ abstract class Enum * * @var array */ - private static $cache = array(); + protected static $cache = array(); /** * Creates a new value of some type @@ -62,7 +62,7 @@ public function getValue() */ public function getKey() { - return self::search($this->value); + return static::search($this->value); } /** @@ -107,12 +107,12 @@ public static function values() public static function toArray() { $class = get_called_class(); - if (!array_key_exists($class, self::$cache)) { + if (!array_key_exists($class, static::$cache)) { $reflection = new \ReflectionClass($class); - self::$cache[$class] = $reflection->getConstants(); + static::$cache[$class] = $reflection->getConstants(); } - return self::$cache[$class]; + return static::$cache[$class]; } /** From d99b9ff661f13c84b9072be0791543b96df1f3de Mon Sep 17 00:00:00 2001 From: Lorenzo Marzullo Date: Wed, 22 Jul 2015 17:26:06 +0200 Subject: [PATCH 4/4] Little update to optimize __callStatic() --- src/Enum.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index e95c577..2ad5aea 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -108,7 +108,7 @@ public static function toArray() { $class = get_called_class(); if (!array_key_exists($class, static::$cache)) { - $reflection = new \ReflectionClass($class); + $reflection = new \ReflectionClass($class); static::$cache[$class] = $reflection->getConstants(); } @@ -164,9 +164,8 @@ public static function search($value) */ public static function __callStatic($name, $arguments) { - if (static::isValidKey($name)) { - $array = static::toArray(); - + $array = static::toArray(); + if (isset($array[$name])) { return new static($array[$name]); }