Enum.php 4.7 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. $this->value = $value->getValue();
  41. return;
  42. }
  43. if (!$this->isValid($value)) {
  44. throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class());
  45. }
  46. $this->value = $value;
  47. }
  48. /**
  49. * @return mixed
  50. */
  51. public function getValue()
  52. {
  53. return $this->value;
  54. }
  55. /**
  56. * Returns the enum key (i.e. the constant name).
  57. *
  58. * @return mixed
  59. */
  60. public function getKey()
  61. {
  62. return static::search($this->value);
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function __toString()
  68. {
  69. return (string)$this->value;
  70. }
  71. /**
  72. * Compares one Enum with another.
  73. *
  74. * This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
  75. *
  76. * @return bool True if Enums are equal, false if not equal
  77. */
  78. final public function equals(Enum $enum = null)
  79. {
  80. return $enum !== null && $this->getValue() === $enum->getValue() && \get_called_class() === \get_class($enum);
  81. }
  82. /**
  83. * Returns the names (keys) of all constants in the Enum class
  84. *
  85. * @return array
  86. */
  87. public static function keys()
  88. {
  89. return \array_keys(static::toArray());
  90. }
  91. /**
  92. * Returns instances of the Enum class of all Enum constants
  93. *
  94. * @return static[] Constant name in key, Enum instance in value
  95. */
  96. public static function values()
  97. {
  98. $values = array();
  99. foreach (static::toArray() as $key => $value) {
  100. $values[$key] = new static($value);
  101. }
  102. return $values;
  103. }
  104. /**
  105. * Returns all possible values as an array
  106. *
  107. * @return array Constant name in key, constant value in value
  108. */
  109. public static function toArray()
  110. {
  111. $class = \get_called_class();
  112. if (!isset(static::$cache[$class])) {
  113. $reflection = new \ReflectionClass($class);
  114. static::$cache[$class] = $reflection->getConstants();
  115. }
  116. return static::$cache[$class];
  117. }
  118. /**
  119. * Check if is valid enum value
  120. *
  121. * @param $value
  122. *
  123. * @return bool
  124. */
  125. public static function isValid($value)
  126. {
  127. return \in_array($value, static::toArray(), true);
  128. }
  129. /**
  130. * Check if is valid enum key
  131. *
  132. * @param $key
  133. *
  134. * @return bool
  135. */
  136. public static function isValidKey($key)
  137. {
  138. $array = static::toArray();
  139. return isset($array[$key]) || \array_key_exists($key, $array);
  140. }
  141. /**
  142. * Return key for value
  143. *
  144. * @param $value
  145. *
  146. * @return mixed
  147. */
  148. public static function search($value)
  149. {
  150. return \array_search($value, static::toArray(), true);
  151. }
  152. /**
  153. * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
  154. *
  155. * @param string $name
  156. * @param array $arguments
  157. *
  158. * @return static
  159. * @throws \BadMethodCallException
  160. */
  161. public static function __callStatic($name, $arguments)
  162. {
  163. $array = static::toArray();
  164. if (isset($array[$name]) || \array_key_exists($name, $array)) {
  165. return new static($array[$name]);
  166. }
  167. throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class());
  168. }
  169. /**
  170. * Specify data which should be serialized to JSON. This method returns data that can be serialized by json_encode()
  171. * natively.
  172. *
  173. * @return mixed
  174. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  175. */
  176. #[\ReturnTypeWillChange]
  177. public function jsonSerialize()
  178. {
  179. return $this->getValue();
  180. }
  181. }