ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MyCLabs\Enum\Enum Class Reference

Base Enum class. More...

+ Inheritance diagram for MyCLabs\Enum\Enum:
+ Collaboration diagram for MyCLabs\Enum\Enum:

Public Member Functions

 __construct ($value)
 Creates a new value of some type. More...
 
 __wakeup ()
 
 getValue ()
 -pure More...
 
 getKey ()
 Returns the enum key (i.e. More...
 
 __toString ()
 -pure -suppress InvalidCast More...
 
 equals ($variable=null)
 Determines if Enum should be considered equal with the variable passed as a parameter. More...
 
 jsonSerialize ()
 Specify data which should be serialized to JSON. More...
 

Static Public Member Functions

static from ($value)
 
static keys ()
 Returns the names (keys) of all constants in the Enum class. More...
 
static values ()
 Returns instances of the Enum class of all Enum constants. More...
 
static toArray ()
 Returns all possible values as an array. More...
 
static isValid ($value)
 Check if is valid enum value. More...
 
static assertValidValue ($value)
 Asserts valid enum value. More...
 
static isValidKey ($key)
 Check if is valid enum key. More...
 
static search ($value)
 Return key for value. More...
 
static __callStatic ($name, $arguments)
 Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant. More...
 

Protected Attributes

 $value
 

Static Protected Attributes

static $cache = []
 
static $instances = []
 

Static Private Member Functions

static assertValidValueReturningKey ($value)
 Asserts valid enum value. More...
 

Private Attributes

 $key
 

Detailed Description

Base Enum class.

Create an enum by implementing this class and adding class constants.

Author
Matthieu Napoli matth.nosp@m.ieu@.nosp@m.mnapo.nosp@m.li.f.nosp@m.r
Daniel Costa danie.nosp@m.lcos.nosp@m.ta@gm.nosp@m.ail..nosp@m.com
Mirosław Filip mirfi.nosp@m.lip@.nosp@m.gmail.nosp@m..com

-template T -immutable -consistent-constructor

Definition at line 22 of file Enum.php.

Constructor & Destructor Documentation

◆ __construct()

MyCLabs\Enum\Enum::__construct (   $value)

Creates a new value of some type.

-pure

Parameters
mixed$value-param T $value
Exceptions

-var T

-var T

Definition at line 65 of file Enum.php.

References MyCLabs\Enum\Enum\$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  }

Member Function Documentation

◆ __callStatic()

static MyCLabs\Enum\Enum::__callStatic (   $name,
  $arguments 
)
static

Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant.

Parameters
string$name
array$arguments
Returns
static
Exceptions

Definition at line 279 of file Enum.php.

References $message, $name, and ILIAS\UI\Implementation\Component\toArray().

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  }
catch(Exception $e) $message
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

◆ __toString()

MyCLabs\Enum\Enum::__toString ( )

-pure -suppress InvalidCast

Returns
string

Definition at line 123 of file Enum.php.

References MyCLabs\Enum\Enum\$value.

124  {
125  return (string)$this->value;
126  }

◆ __wakeup()

MyCLabs\Enum\Enum::__wakeup ( )

Definition at line 78 of file Enum.php.

79  {
80  if ($this->key === null) {
81  $this->key = static::search($this->value);
82  }
83  }

◆ assertValidValue()

static MyCLabs\Enum\Enum::assertValidValue (   $value)
static

Asserts valid enum value.

-pure -assert T $value

Definition at line 219 of file Enum.php.

References MyCLabs\Enum\Enum\$value.

219  : void
220  {
221  self::assertValidValueReturningKey($value);
222  }

◆ assertValidValueReturningKey()

static MyCLabs\Enum\Enum::assertValidValueReturningKey (   $value)
staticprivate

Asserts valid enum value.

-pure -assert T $value

Definition at line 230 of file Enum.php.

References MyCLabs\Enum\Enum\$key, and MyCLabs\Enum\Enum\$value.

230  : 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  }

◆ equals()

MyCLabs\Enum\Enum::equals (   $variable = null)
final

Determines if Enum should be considered equal with the variable passed as a parameter.

Returns false if an argument is an object of different class or not an object.

This method is final, for more information read https://github.com/myclabs/php-enum/issues/4

-pure -param mixed $variable

Returns
bool

Definition at line 138 of file Enum.php.

References MyCLabs\Enum\Enum\getValue().

138  : bool
139  {
140  return $variable instanceof self
141  && $this->getValue() === $variable->getValue()
142  && static::class === \get_class($variable);
143  }
getValue()
-pure
Definition: Enum.php:102
+ Here is the call graph for this function:

◆ from()

static MyCLabs\Enum\Enum::from (   $value)
static
Parameters
mixed$value
Returns
static -return static<T>

Definition at line 90 of file Enum.php.

References MyCLabs\Enum\Enum\$key, and MyCLabs\Enum\Enum\$value.

90  : self
91  {
92  $key = static::assertValidValueReturningKey($value);
93 
94  return self::__callStatic($key, []);
95  }

◆ getKey()

MyCLabs\Enum\Enum::getKey ( )

Returns the enum key (i.e.

the constant name).

-pure

Returns
string

Definition at line 113 of file Enum.php.

References MyCLabs\Enum\Enum\$key.

114  {
115  return $this->key;
116  }

◆ getValue()

MyCLabs\Enum\Enum::getValue ( )

-pure

Returns
mixed -return T

Definition at line 102 of file Enum.php.

References MyCLabs\Enum\Enum\$value.

Referenced by MyCLabs\Enum\Enum\equals(), and MyCLabs\Enum\Enum\jsonSerialize().

103  {
104  return $this->value;
105  }
+ Here is the caller graph for this function:

◆ isValid()

static MyCLabs\Enum\Enum::isValid (   $value)
static

Check if is valid enum value.

Parameters
$value-param mixed $value -pure -assert-if-true T $value
Returns
bool

Definition at line 208 of file Enum.php.

References MyCLabs\Enum\Enum\$value, and ILIAS\UI\Implementation\Component\toArray().

209  {
210  return \in_array($value, static::toArray(), true);
211  }
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

◆ isValidKey()

static MyCLabs\Enum\Enum::isValidKey (   $key)
static

Check if is valid enum key.

Parameters
$key-param string $key -pure
Returns
bool

Definition at line 247 of file Enum.php.

References MyCLabs\Enum\Enum\$key, and ILIAS\UI\Implementation\Component\toArray().

248  {
249  $array = static::toArray();
250 
251  return isset($array[$key]) || \array_key_exists($key, $array);
252  }
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

◆ jsonSerialize()

MyCLabs\Enum\Enum::jsonSerialize ( )

Specify data which should be serialized to JSON.

This method returns data that can be serialized by json_encode() natively.

Returns
mixed -pure

Definition at line 301 of file Enum.php.

References MyCLabs\Enum\Enum\getValue().

302  {
303  return $this->getValue();
304  }
getValue()
-pure
Definition: Enum.php:102
+ Here is the call graph for this function:

◆ keys()

static MyCLabs\Enum\Enum::keys ( )
static

Returns the names (keys) of all constants in the Enum class.

-pure -return list<string>

Returns
array

Definition at line 152 of file Enum.php.

References ILIAS\UI\Implementation\Component\toArray().

153  {
154  return \array_keys(static::toArray());
155  }
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

◆ search()

static MyCLabs\Enum\Enum::search (   $value)
static

Return key for value.

Parameters
mixed$value-param mixed $value -pure
Returns
string|false

Definition at line 263 of file Enum.php.

References MyCLabs\Enum\Enum\$value, and ILIAS\UI\Implementation\Component\toArray().

264  {
265  return \array_search($value, static::toArray(), true);
266  }
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

◆ toArray()

static MyCLabs\Enum\Enum::toArray ( )
static

Returns all possible values as an array.

-pure -suppress ImpureStaticProperty

-return array<string, mixed>

Returns
array Constant name in key, constant value in value

-suppress ImpureMethodCall this reflection API usage has no side-effects here

-suppress ImpureMethodCall this reflection API usage has no side-effects here

Definition at line 185 of file Enum.php.

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  }

◆ values()

static MyCLabs\Enum\Enum::values ( )
static

Returns instances of the Enum class of all Enum constants.

-pure -return array<string, static>

Returns
static[] Constant name in key, Enum instance in value

-var T $value

Definition at line 164 of file Enum.php.

References MyCLabs\Enum\Enum\$key, MyCLabs\Enum\Enum\$value, $values, and ILIAS\UI\Implementation\Component\toArray().

165  {
166  $values = array();
167 
169  foreach (static::toArray() as $key => $value) {
170  $values[$key] = new static($value);
171  }
172 
173  return $values;
174  }
$values
toArray($value)
Wrap the given value in an array if it is no array.
+ Here is the call graph for this function:

Field Documentation

◆ $cache

MyCLabs\Enum\Enum::$cache = []
staticprotected

Definition at line 46 of file Enum.php.

◆ $instances

MyCLabs\Enum\Enum::$instances = []
staticprotected

Definition at line 54 of file Enum.php.

◆ $key

◆ $value


The documentation for this class was generated from the following file: