fork(1) download
  1. <?php
  2. /**
  3.  * @link http://g...content-available-to-author-only...b.com/myclabs/php-enum
  4.  * @license http://w...content-available-to-author-only...e.org/licenses/mit-license.php MIT (see the LICENSE file)
  5.  */
  6.  
  7. /**
  8.  * Base Enum class
  9.  *
  10.  * Create an enum by implementing this class and adding class constants.
  11.  *
  12.  * @author Matthieu Napoli <matthieu@mnapoli.fr>
  13.  * @author Daniel Costa <danielcosta@gmail.com
  14.  */
  15. abstract class Enum
  16. {
  17. /**
  18.   * Enum value
  19.   *
  20.   * @var mixed
  21.   */
  22. protected $value;
  23.  
  24. /**
  25.   * Store existing constants in a static cache per object.
  26.   *
  27.   * @var array
  28.   */
  29. private static $cache = array();
  30.  
  31. /**
  32.   * Creates a new value of some type
  33.   *
  34.   * @param mixed $value
  35.   *
  36.   * @throws \UnexpectedValueException if incompatible type is given.
  37.   */
  38. public function __construct($value)
  39. {
  40. if (!in_array($value, self::toArray())) {
  41. throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
  42. }
  43.  
  44. $this->value = $value;
  45. }
  46.  
  47. /**
  48.   * @return mixed
  49.   */
  50. public function getValue()
  51. {
  52. return $this->value;
  53. }
  54.  
  55. /**
  56.   * Returns the enum key (i.e. the constant name).
  57.   *
  58.   * @return mixed
  59.   */
  60. public function getKey()
  61. {
  62. return self::search($this->value);
  63. }
  64.  
  65. /**
  66.   * @return string
  67.   */
  68. public function __toString()
  69. {
  70. return (string) $this->value;
  71. }
  72.  
  73. /**
  74.   * Returns the names (keys) of all constants in the Enum class
  75.   *
  76.   * @return array
  77.   */
  78. public static function keys()
  79. {
  80. return array_keys(static::toArray());
  81. }
  82.  
  83. /**
  84.   * Returns all possible values as an array
  85.   *
  86.   * @return array Constant name in key, constant value in value
  87.   */
  88. public static function toArray()
  89. {
  90. $class = get_called_class();
  91. if (!array_key_exists($class, self::$cache)) {
  92. $reflection = new \ReflectionClass($class);
  93. self::$cache[$class] = $reflection->getConstants();
  94. }
  95.  
  96. return self::$cache[$class];
  97. }
  98.  
  99. /**
  100.   * Check if is valid enum value
  101.   *
  102.   * @param $value
  103.   * @return bool
  104.   */
  105. public static function isValid($value)
  106. {
  107. return in_array($value, self::toArray());
  108. }
  109.  
  110. /**
  111.   * Check if is valid enum key
  112.   *
  113.   * @param $key
  114.   *
  115.   * @return bool
  116.   */
  117. public static function isValidKey($key)
  118. {
  119. return in_array($key, self::keys());
  120. }
  121.  
  122. /**
  123.   * Return key for value
  124.   *
  125.   * @param $value
  126.   *
  127.   * @return mixed
  128.   */
  129. public static function search($value)
  130. {
  131. return array_search($value, array_combine(self::keys(), self::toArray()));
  132. }
  133.  
  134. /**
  135.   * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
  136.   *
  137.   * @param string $name
  138.   * @param array $arguments
  139.   *
  140.   * @return static
  141.   * @throws \BadMethodCallException
  142.   */
  143. public static function __callStatic($name, $arguments)
  144. {
  145. if (defined("static::$name")) {
  146. return new static(constant("static::$name"));
  147. }
  148.  
  149. throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
  150. }
  151. }
  152.  
  153. ###########################
  154. class MyEnum extends Enum {
  155. const A = 0;
  156. const B = 1;
  157. }
  158.  
  159.  
  160. try {
  161. new MyEnum('any string');
  162. echo "I have successfully created 'any string' enum";
  163. }
  164. catch (\UnexpectedValueException $e) {
  165. echo "I should be displayed because 'any string is not a valid enum member";
  166. }
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
I have successfully created 'any string' enum