ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Enum.php
Go to the documentation of this file.
1 <?php
7 namespace MyCLabs\Enum;
8 
22 abstract class Enum implements \JsonSerializable
23 {
30  protected $value;
31 
37  private $key;
38 
46  protected static $cache = [];
47 
54  protected static $instances = [];
55 
65  public function __construct($value)
66  {
67  if ($value instanceof static) {
69  $value = $value->getValue();
70  }
71 
72  $this->key = static::assertValidValueReturningKey($value);
73 
75  $this->value = $value;
76  }
77 
78  public function __wakeup()
79  {
80  if ($this->key === null) {
81  $this->key = static::search($this->value);
82  }
83  }
84 
90  public static function from($value): self
91  {
92  $key = static::assertValidValueReturningKey($value);
93 
94  return self::__callStatic($key, []);
95  }
96 
102  public function getValue()
103  {
104  return $this->value;
105  }
106 
113  public function getKey()
114  {
115  return $this->key;
116  }
117 
123  public function __toString()
124  {
125  return (string)$this->value;
126  }
127 
138  final public function equals($variable = null): bool
139  {
140  return $variable instanceof self
141  && $this->getValue() === $variable->getValue()
142  && static::class === \get_class($variable);
143  }
144 
152  public static function keys()
153  {
154  return \array_keys(static::toArray());
155  }
156 
164  public static function values()
165  {
166  $values = array();
167 
169  foreach (static::toArray() as $key => $value) {
170  $values[$key] = new static($value);
171  }
172 
173  return $values;
174  }
175 
185  public static function toArray()
186  {
187  $class = static::class;
188 
189  if (!isset(static::$cache[$class])) {
191  $reflection = new \ReflectionClass($class);
193  static::$cache[$class] = $reflection->getConstants();
194  }
195 
196  return static::$cache[$class];
197  }
198 
208  public static function isValid($value)
209  {
210  return \in_array($value, static::toArray(), true);
211  }
212 
219  public static function assertValidValue($value): void
220  {
221  self::assertValidValueReturningKey($value);
222  }
223 
230  private static function assertValidValueReturningKey($value): string
231  {
232  if (false === ($key = static::search($value))) {
233  throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
234  }
235 
236  return $key;
237  }
238 
247  public static function isValidKey($key)
248  {
249  $array = static::toArray();
250 
251  return isset($array[$key]) || \array_key_exists($key, $array);
252  }
253 
263  public static function search($value)
264  {
265  return \array_search($value, static::toArray(), true);
266  }
267 
279  public static function __callStatic($name, $arguments)
280  {
281  $class = static::class;
282  if (!isset(self::$instances[$class][$name])) {
283  $array = static::toArray();
284  if (!isset($array[$name]) && !\array_key_exists($name, $array)) {
285  $message = "No static method or enum constant '$name' in class " . static::class;
286  throw new \BadMethodCallException($message);
287  }
288  return self::$instances[$class][$name] = new static($array[$name]);
289  }
290  return clone self::$instances[$class][$name];
291  }
292 
301  public function jsonSerialize()
302  {
303  return $this->getValue();
304  }
305 }
static toArray()
Returns all possible values as an array.
Definition: Enum.php:185
static assertValidValueReturningKey($value)
Asserts valid enum value.
Definition: Enum.php:230
__construct($value)
Creates a new value of some type.
Definition: Enum.php:65
http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
Definition: Enum.php:7
static __callStatic($name, $arguments)
Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class cons...
Definition: Enum.php:279
static keys()
Returns the names (keys) of all constants in the Enum class.
Definition: Enum.php:152
static isValidKey($key)
Check if is valid enum key.
Definition: Enum.php:247
catch(Exception $e) $message
equals($variable=null)
Determines if Enum should be considered equal with the variable passed as a parameter.
Definition: Enum.php:138
$values
static from($value)
Definition: Enum.php:90
jsonSerialize()
Specify data which should be serialized to JSON.
Definition: Enum.php:301
getValue()
-pure
Definition: Enum.php:102
static $instances
Definition: Enum.php:54
static $cache
Definition: Enum.php:46
static values()
Returns instances of the Enum class of all Enum constants.
Definition: Enum.php:164
Base Enum class.
Definition: Enum.php:22
toArray($value)
Wrap the given value in an array if it is no array.
static search($value)
Return key for value.
Definition: Enum.php:263
getKey()
Returns the enum key (i.e.
Definition: Enum.php:113
static assertValidValue($value)
Asserts valid enum value.
Definition: Enum.php:219
static isValid($value)
Check if is valid enum value.
Definition: Enum.php:208
__toString()
-pure -suppress InvalidCast
Definition: Enum.php:123