Enum.php 5.6 KB

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