<?php
/**
* @link http://g...content-available-to-author-only...b.com/myclabs/php-enum
* @license http://w...content-available-to-author-only...e.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
/**
* Base Enum class
*
* Create an enum by implementing this class and adding class constants.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
* @author Daniel Costa <danielcosta@gmail.com
*/
abstract class Enum
{
/**
* Enum value
*
* @var mixed
*/
protected $value;
/**
* Store existing constants in a static cache per object.
*
* @var array
*/
private static
$cache = array();
/**
* Creates a new value of some type
*
* @param mixed $value
*
* @throws \UnexpectedValueException if incompatible type is given.
*/
public function __construct($value)
{
if (!in_array($value, self::toArray())) { throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
}
$this->value = $value;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Returns the enum key (i.e. the constant name).
*
* @return mixed
*/
public function getKey()
{
return self::search($this->value);
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->value;
}
/**
* Returns the names (keys) of all constants in the Enum class
*
* @return array
*/
public static function keys()
{
}
/**
* Returns all possible values as an array
*
* @return array Constant name in key, constant value in value
*/
public static function toArray()
{
$class = get_called_class();
$reflection = new \ReflectionClass($class);
self::$cache[$class] = $reflection->getConstants();
}
return self::$cache[$class];
}
/**
* Check if is valid enum value
*
* @param $value
* @return bool
*/
public static function isValid($value)
{
return in_array($value, self::toArray()); }
/**
* Check if is valid enum key
*
* @param $key
*
* @return bool
*/
public static function isValidKey($key)
{
}
/**
* Return key for value
*
* @param $value
*
* @return mixed
*/
public static function search($value)
{
}
/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
*
* @param string $name
* @param array $arguments
*
* @return static
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
{
return new static
(constant("static::$name")); }
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
}
}
###########################
class MyEnum extends Enum {
const A = 0;
const B = 1;
}
try {
new MyEnum('any string');
echo "I have successfully created 'any string' enum";
}
catch (\UnexpectedValueException $e) {
echo "I should be displayed because 'any string is not a valid enum member";
}