Open Journal Systems  3.3.0
Enum.php
1 <?php
7 namespace MyCLabs\Enum;
8 
21 abstract class Enum implements \JsonSerializable
22 {
29  protected $value;
30 
38  protected static $cache = [];
39 
49  public function __construct($value)
50  {
51  if ($value instanceof static) {
53  $value = $value->getValue();
54  }
55 
56  if (!$this->isValid($value)) {
58  throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
59  }
60 
62  $this->value = $value;
63  }
64 
70  public function getValue()
71  {
72  return $this->value;
73  }
74 
81  public function getKey()
82  {
83  return static::search($this->value);
84  }
85 
91  public function __toString()
92  {
93  return (string)$this->value;
94  }
95 
106  final public function equals($variable = null): bool
107  {
108  return $variable instanceof self
109  && $this->getValue() === $variable->getValue()
110  && static::class === \get_class($variable);
111  }
112 
120  public static function keys()
121  {
122  return \array_keys(static::toArray());
123  }
124 
132  public static function values()
133  {
134  $values = array();
135 
137  foreach (static::toArray() as $key => $value) {
138  $values[$key] = new static($value);
139  }
140 
141  return $values;
142  }
143 
153  public static function toArray()
154  {
155  $class = static::class;
156 
157  if (!isset(static::$cache[$class])) {
158  $reflection = new \ReflectionClass($class);
159  static::$cache[$class] = $reflection->getConstants();
160  }
161 
162  return static::$cache[$class];
163  }
164 
173  public static function isValid($value)
174  {
175  return \in_array($value, static::toArray(), true);
176  }
177 
186  public static function isValidKey($key)
187  {
188  $array = static::toArray();
189 
190  return isset($array[$key]) || \array_key_exists($key, $array);
191  }
192 
202  public static function search($value)
203  {
204  return \array_search($value, static::toArray(), true);
205  }
206 
217  public static function __callStatic($name, $arguments)
218  {
219  $array = static::toArray();
220  if (isset($array[$name]) || \array_key_exists($name, $array)) {
221  return new static($array[$name]);
222  }
223 
224  throw new \BadMethodCallException("No static method or enum constant '$name' in class " . static::class);
225  }
226 
235  public function jsonSerialize()
236  {
237  return $this->getValue();
238  }
239 }
MyCLabs\Enum\Enum\getKey
getKey()
Definition: Enum.php:84
MyCLabs\Enum\Enum
Definition: Enum.php:21
MyCLabs\Enum\Enum\$value
$value
Definition: Enum.php:32
MyCLabs\Enum\Enum\__toString
__toString()
Definition: Enum.php:94
MyCLabs\Enum\Enum\jsonSerialize
jsonSerialize()
Definition: Enum.php:238
MyCLabs\Enum\Enum\equals
equals($variable=null)
Definition: Enum.php:109
MyCLabs\Enum\Enum\keys
static keys()
Definition: Enum.php:123
MyCLabs\Enum\Enum\isValidKey
static isValidKey($key)
Definition: Enum.php:189
MyCLabs\Enum\Enum\$cache
static $cache
Definition: Enum.php:41
MyCLabs\Enum\Enum\__callStatic
static __callStatic($name, $arguments)
Definition: Enum.php:220
MyCLabs\Enum\Enum\isValid
static isValid($value)
Definition: Enum.php:176
MyCLabs\Enum
Definition: Enum.php:7
MyCLabs\Enum\Enum\toArray
static toArray()
Definition: Enum.php:156
MyCLabs\Enum\Enum\getValue
getValue()
Definition: Enum.php:73
MyCLabs\Enum\Enum\values
static values()
Definition: Enum.php:135
MyCLabs\Enum\Enum\__construct
__construct($value)
Definition: Enum.php:52
MyCLabs\Enum\Enum\search
static search($value)
Definition: Enum.php:205