Skip to content

Change calls to method toArray() from self::toArray() to static::toArray() #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class Enum
*
* @var array
*/
private static $cache = array();
protected static $cache = array();

/**
* Creates a new value of some type
Expand Down Expand Up @@ -62,15 +62,15 @@ public function getValue()
*/
public function getKey()
{
return self::search($this->value);
return static::search($this->value);
}

/**
* @return string
*/
public function __toString()
{
return (string) $this->value;
return (string)$this->value;
}

/**
Expand All @@ -80,7 +80,7 @@ public function __toString()
*/
public static function keys()
{
return array_keys(self::toArray());
return array_keys(static::toArray());
}

/**
Expand All @@ -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);
}

Expand All @@ -107,23 +107,24 @@ public static function values()
public static function toArray()
{
$class = get_called_class();
if (!array_key_exists($class, self::$cache)) {
$reflection = new \ReflectionClass($class);
self::$cache[$class] = $reflection->getConstants();
if (!array_key_exists($class, static::$cache)) {
$reflection = new \ReflectionClass($class);
static::$cache[$class] = $reflection->getConstants();
}

return self::$cache[$class];
return static::$cache[$class];
}

/**
* Check if is valid enum value
*
* @param $value
*
* @return bool
*/
public static function isValid($value)
{
return in_array($value, self::toArray(), true);
return in_array($value, static::toArray(), true);
}

/**
Expand All @@ -135,7 +136,8 @@ public static function isValid($value)
*/
public static function isValidKey($key)
{
$array = self::toArray();
$array = static::toArray();

return isset($array[$key]);
}

Expand All @@ -148,7 +150,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);
}

/**
Expand All @@ -162,8 +164,9 @@ public static function search($value)
*/
public static function __callStatic($name, $arguments)
{
if (defined("static::$name")) {
return new static(constant("static::$name"));
$array = static::toArray();
if (isset($array[$name])) {
return new static($array[$name]);
}

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