@@ -18,91 +18,91 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Enum |
| 20 | 20 | { |
| 21 | - /** @var int|double|string|boolean The value. */ |
|
| 22 | - private $value; |
|
| 21 | + /** @var int|double|string|boolean The value. */ |
|
| 22 | + private $value; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Construct a enum with the given value. |
|
| 26 | - * |
|
| 27 | - * @param int|double|string|boolean $value |
|
| 28 | - * @throws \UnexpectedValueException |
|
| 29 | - */ |
|
| 30 | - public function __construct($value) |
|
| 31 | - { |
|
| 32 | - if (!in_array($value, static::toArray())) { |
|
| 33 | - throw new \UnexpectedValueException($value . ' is not a valid enum value.'); |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * Construct a enum with the given value. |
|
| 26 | + * |
|
| 27 | + * @param int|double|string|boolean $value |
|
| 28 | + * @throws \UnexpectedValueException |
|
| 29 | + */ |
|
| 30 | + public function __construct($value) |
|
| 31 | + { |
|
| 32 | + if (!in_array($value, static::toArray())) { |
|
| 33 | + throw new \UnexpectedValueException($value . ' is not a valid enum value.'); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - $this->value = $value; |
|
| 37 | - } |
|
| 36 | + $this->value = $value; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Returns the string representation of the enum object. |
|
| 41 | - * |
|
| 42 | - * @return string the string representation of the enum object. |
|
| 43 | - */ |
|
| 44 | - public function __toString() |
|
| 45 | - { |
|
| 46 | - return $this->getName(); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * Returns the string representation of the enum object. |
|
| 41 | + * |
|
| 42 | + * @return string the string representation of the enum object. |
|
| 43 | + */ |
|
| 44 | + public function __toString() |
|
| 45 | + { |
|
| 46 | + return $this->getName(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Returns the ordinal. |
|
| 51 | - * |
|
| 52 | - * @return int the ordinal. |
|
| 53 | - */ |
|
| 54 | - public function getOrdinal() |
|
| 55 | - { |
|
| 56 | - $ordinal = array_search($this->value, array_values(static::toArray())); |
|
| 49 | + /** |
|
| 50 | + * Returns the ordinal. |
|
| 51 | + * |
|
| 52 | + * @return int the ordinal. |
|
| 53 | + */ |
|
| 54 | + public function getOrdinal() |
|
| 55 | + { |
|
| 56 | + $ordinal = array_search($this->value, array_values(static::toArray())); |
|
| 57 | 57 | |
| 58 | - return $ordinal !== false ? $ordinal : -1; |
|
| 59 | - } |
|
| 58 | + return $ordinal !== false ? $ordinal : -1; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Returns the name. |
|
| 63 | - * |
|
| 64 | - * @return string the name. |
|
| 65 | - */ |
|
| 66 | - public function getName() |
|
| 67 | - { |
|
| 68 | - $name = array_search($this->value, static::toArray()); |
|
| 61 | + /** |
|
| 62 | + * Returns the name. |
|
| 63 | + * |
|
| 64 | + * @return string the name. |
|
| 65 | + */ |
|
| 66 | + public function getName() |
|
| 67 | + { |
|
| 68 | + $name = array_search($this->value, static::toArray()); |
|
| 69 | 69 | |
| 70 | - return $name !== false ? $name : ''; |
|
| 71 | - } |
|
| 70 | + return $name !== false ? $name : ''; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Returns the value. |
|
| 75 | - * |
|
| 76 | - * @return mixed the value; |
|
| 77 | - */ |
|
| 78 | - public function getValue() |
|
| 79 | - { |
|
| 80 | - return $this->value; |
|
| 81 | - } |
|
| 73 | + /** |
|
| 74 | + * Returns the value. |
|
| 75 | + * |
|
| 76 | + * @return mixed the value; |
|
| 77 | + */ |
|
| 78 | + public function getValue() |
|
| 79 | + { |
|
| 80 | + return $this->value; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Returns the enum objects. |
|
| 85 | - * |
|
| 86 | - * @return array<string,integer|double|string|boolean> the enum objects. |
|
| 87 | - */ |
|
| 88 | - public static function toArray() |
|
| 89 | - { |
|
| 90 | - return (new \ReflectionClass(\get_called_class()))->getConstants(); |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Returns the enum objects. |
|
| 85 | + * |
|
| 86 | + * @return array<string,integer|double|string|boolean> the enum objects. |
|
| 87 | + */ |
|
| 88 | + public static function toArray() |
|
| 89 | + { |
|
| 90 | + return (new \ReflectionClass(\get_called_class()))->getConstants(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Returns the enum object with the specified name. |
|
| 95 | - * |
|
| 96 | - * @param string $name |
|
| 97 | - * @return Enum the enum object with the specified name. |
|
| 98 | - * @throws \UnexpectedValueException |
|
| 99 | - */ |
|
| 100 | - public static function valueOf($name) |
|
| 101 | - { |
|
| 102 | - if (!defined('static::' . $name)) { |
|
| 103 | - throw new \UnexpectedValueException($name . ' is not a valid enum name.'); |
|
| 104 | - } |
|
| 93 | + /** |
|
| 94 | + * Returns the enum object with the specified name. |
|
| 95 | + * |
|
| 96 | + * @param string $name |
|
| 97 | + * @return Enum the enum object with the specified name. |
|
| 98 | + * @throws \UnexpectedValueException |
|
| 99 | + */ |
|
| 100 | + public static function valueOf($name) |
|
| 101 | + { |
|
| 102 | + if (!defined('static::' . $name)) { |
|
| 103 | + throw new \UnexpectedValueException($name . ' is not a valid enum name.'); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - return new static(constant('static::' . $name)); |
|
| 107 | - } |
|
| 106 | + return new static(constant('static::' . $name)); |
|
| 107 | + } |
|
| 108 | 108 | } |