enum.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Base Enum class
  4. *
  5. * Create an enum by implementing this class and adding class constants.
  6. *
  7. */
  8. abstract class Enum {
  9. /**
  10. * Enum value
  11. *
  12. * @var mixed
  13. */
  14. protected $value;
  15. /**
  16. * Store existing constants in a static cache per object.
  17. *
  18. * @var array
  19. */
  20. private static $cache = array();
  21. /**
  22. * Creates a new value of some type
  23. *
  24. * @param mixed $value
  25. *
  26. * @throws \UnexpectedValueException if incompatible type is given.
  27. */
  28. public function __construct($value)
  29. {
  30. if (!$this->isValid($value)) {
  31. throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
  32. }
  33. $this->value = $value;
  34. }
  35. /**
  36. * @return mixed
  37. */
  38. public function getValue()
  39. {
  40. return $this->value;
  41. }
  42. /**
  43. * Returns the enum key (i.e. the constant name).
  44. *
  45. * @return mixed
  46. */
  47. public function getKey()
  48. {
  49. return self::search($this->value);
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function __toString()
  55. {
  56. return (string) $this->value;
  57. }
  58. /**
  59. * Returns the names (keys) of all constants in the Enum class
  60. *
  61. * @return array
  62. */
  63. public static function keys()
  64. {
  65. return array_keys(self::toArray());
  66. }
  67. /**
  68. * Returns instances of the Enum class of all Enum constants
  69. *
  70. * @return array Constant name in key, Enum instance in value
  71. */
  72. public static function values()
  73. {
  74. $values = array();
  75. foreach (self::toArray() as $key => $value) {
  76. $values[$key] = new static($value);
  77. }
  78. return $values;
  79. }
  80. /**
  81. * Returns all possible values as an array
  82. *
  83. * @return array Constant name in key, constant value in value
  84. */
  85. public static function toArray()
  86. {
  87. $class = get_called_class();
  88. if (!array_key_exists($class, self::$cache)) {
  89. $reflection = new \ReflectionClass($class);
  90. self::$cache[$class] = $reflection->getConstants();
  91. }
  92. return self::$cache[$class];
  93. }
  94. /**
  95. * Check if is valid enum value
  96. *
  97. * @param $value
  98. * @return bool
  99. */
  100. public static function isValid($value)
  101. {
  102. return in_array($value, self::toArray(), true);
  103. }
  104. /**
  105. * Check if is valid enum key
  106. *
  107. * @param $key
  108. *
  109. * @return bool
  110. */
  111. public static function isValidKey($key)
  112. {
  113. $array = self::toArray();
  114. return isset($array[$key]);
  115. }
  116. /**
  117. * Return key for value
  118. *
  119. * @param $value
  120. *
  121. * @return mixed
  122. */
  123. public static function search($value)
  124. {
  125. return array_search($value, self::toArray(), true);
  126. }
  127. /**
  128. * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
  129. *
  130. * @param string $name
  131. * @param array $arguments
  132. *
  133. * @return static
  134. * @throws \BadMethodCallException
  135. */
  136. public static function __callStatic($name, $arguments)
  137. {
  138. if (defined("static::$name")) {
  139. return new static(constant("static::$name"));
  140. }
  141. throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
  142. }
  143. }