Enum.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @link http://github.com/myclabs/php-enum
  4. * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
  5. */
  6. namespace MyCLabs\Enum;
  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. * @author Mirosław Filip <mirfilip@gmail.com>
  15. */
  16. abstract class Enum implements \JsonSerializable
  17. {
  18. /**
  19. * Enum value
  20. *
  21. * @var mixed
  22. */
  23. protected $value;
  24. /**
  25. * Store existing constants in a static cache per object.
  26. *
  27. * @var array
  28. */
  29. protected static $cache = [];
  30. /**
  31. * Creates a new value of some type
  32. *
  33. * @param mixed $value
  34. *
  35. * @throws \UnexpectedValueException if incompatible type is given.
  36. */
  37. public function __construct($value)
  38. {
  39. if ($value instanceof static) {
  40. $value = $value->getValue();
  41. }
  42. if (!$this->isValid($value)) {
  43. throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class());
  44. }
  45. $this->value = $value;
  46. }
  47. /**
  48. * @return mixed
  49. */
  50. public function getValue()
  51. {
  52. return $this->value;
  53. }
  54. /**
  55. * Returns the enum key (i.e. the constant name).
  56. *
  57. * @return mixed
  58. */
  59. public function getKey()
  60. {
  61. return static::search($this->value);
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function __toString()
  67. {
  68. return (string)$this->value;
  69. }
  70. /**
  71. * Determines if Enum should be considered equal with the variable passed as a parameter.
  72. * Returns false if an argument is an object of different class or not an object.
  73. *
  74. * This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
  75. *
  76. * @return bool
  77. */
  78. final public function equals($variable = null): bool
  79. {
  80. return $variable instanceof self
  81. && $this->getValue() === $variable->getValue()
  82. && \get_called_class() === \get_class($variable);
  83. }
  84. /**
  85. * Returns the names (keys) of all constants in the Enum class
  86. *
  87. * @return array
  88. */
  89. public static function keys()
  90. {
  91. return \array_keys(static::toArray());
  92. }
  93. /**
  94. * Returns instances of the Enum class of all Enum constants
  95. *
  96. * @return static[] Constant name in key, Enum instance in value
  97. */
  98. public static function values()
  99. {
  100. $values = array();
  101. foreach (static::toArray() as $key => $value) {
  102. $values[$key] = new static($value);
  103. }
  104. return $values;
  105. }
  106. /**
  107. * Returns all possible values as an array
  108. *
  109. * @return array Constant name in key, constant value in value
  110. */
  111. public static function toArray()
  112. {
  113. $class = \get_called_class();
  114. if (!isset(static::$cache[$class])) {
  115. $reflection = new \ReflectionClass($class);
  116. static::$cache[$class] = $reflection->getConstants();
  117. }
  118. return static::$cache[$class];
  119. }
  120. /**
  121. * Check if is valid enum value
  122. *
  123. * @param $value
  124. *
  125. * @return bool
  126. */
  127. public static function isValid($value)
  128. {
  129. return \in_array($value, static::toArray(), true);
  130. }
  131. /**
  132. * Check if is valid enum key
  133. *
  134. * @param $key
  135. *
  136. * @return bool
  137. */
  138. public static function isValidKey($key)
  139. {
  140. $array = static::toArray();
  141. return isset($array[$key]) || \array_key_exists($key, $array);
  142. }
  143. /**
  144. * Return key for value
  145. *
  146. * @param $value
  147. *
  148. * @return mixed
  149. */
  150. public static function search($value)
  151. {
  152. return \array_search($value, static::toArray(), true);
  153. }
  154. /**
  155. * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
  156. *
  157. * @param string $name
  158. * @param array $arguments
  159. *
  160. * @return static
  161. * @throws \BadMethodCallException
  162. */
  163. public static function __callStatic($name, $arguments)
  164. {
  165. $array = static::toArray();
  166. if (isset($array[$name]) || \array_key_exists($name, $array)) {
  167. return new static($array[$name]);
  168. }
  169. throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class());
  170. }
  171. /**
  172. * Specify data which should be serialized to JSON. This method returns data that can be serialized by json_encode()
  173. * natively.
  174. *
  175. * @return mixed
  176. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  177. */
  178. public function jsonSerialize()
  179. {
  180. return $this->getValue();
  181. }
  182. }