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 ()
 @psalm-pure More...
 
 getKey ()
 Returns the enum key (i.e. More...
 
 __toString ()
 @psalm-pure @psalm-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

@psalm-template T @psalm-immutable @psalm-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.

@psalm-pure

Parameters
mixed$value

@psalm-param T $value

Exceptions

UnexpectedValueException if incompatible type is given.

@psalm-var T

@psalm-var T

Definition at line 65 of file Enum.php.

66 {
67 if ($value instanceof static) {
69 $value = $value->getValue();
70 }
71
72 $this->key = static::assertValidValueReturningKey($value);
73
75 $this->value = $value;
76 }

References MyCLabs\Enum\Enum\$value.

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

BadMethodCallException

@psalm-pure

Definition at line 279 of file Enum.php.

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.

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

Referenced by MyCLabs\Enum\Enum\from().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __toString()

MyCLabs\Enum\Enum::__toString ( )

@psalm-pure @psalm-suppress InvalidCast

Returns
string

Definition at line 123 of file Enum.php.

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

References MyCLabs\Enum\Enum\$value.

◆ __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.

@psalm-pure @psalm-assert T $value

Definition at line 219 of file Enum.php.

219 : void
220 {
222 }
static assertValidValueReturningKey($value)
Asserts valid enum value.
Definition: Enum.php:230

References MyCLabs\Enum\Enum\$value, and MyCLabs\Enum\Enum\assertValidValueReturningKey().

+ Here is the call graph for this function:

◆ assertValidValueReturningKey()

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

Asserts valid enum value.

@psalm-pure @psalm-assert T $value

Definition at line 230 of file Enum.php.

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 }

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

Referenced by MyCLabs\Enum\Enum\assertValidValue().

+ Here is the caller graph for this function:

◆ 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

@psalm-pure @psalm-param mixed $variable

Returns
bool

Definition at line 138 of file Enum.php.

138 : bool
139 {
140 return $variable instanceof self
141 && $this->getValue() === $variable->getValue()
142 && static::class === \get_class($variable);
143 }
getValue()
@psalm-pure
Definition: Enum.php:102

References MyCLabs\Enum\Enum\getValue().

+ Here is the call graph for this function:

◆ from()

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

Definition at line 90 of file Enum.php.

90 : self
91 {
92 $key = static::assertValidValueReturningKey($value);
93
94 return self::__callStatic($key, []);
95 }
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

References MyCLabs\Enum\Enum\$key, MyCLabs\Enum\Enum\$value, and MyCLabs\Enum\Enum\__callStatic().

+ Here is the call graph for this function:

◆ getKey()

MyCLabs\Enum\Enum::getKey ( )

Returns the enum key (i.e.

the constant name).

@psalm-pure

Returns
string

Definition at line 113 of file Enum.php.

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

References MyCLabs\Enum\Enum\$key.

◆ getValue()

MyCLabs\Enum\Enum::getValue ( )

@psalm-pure

Returns
mixed @psalm-return T

Definition at line 102 of file Enum.php.

103 {
104 return $this->value;
105 }

References MyCLabs\Enum\Enum\$value.

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

+ Here is the caller graph for this function:

◆ isValid()

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

Check if is valid enum value.

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

Definition at line 208 of file Enum.php.

209 {
210 return \in_array($value, static::toArray(), true);
211 }

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

+ Here is the call graph for this function:

◆ isValidKey()

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

Check if is valid enum key.

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

Definition at line 247 of file Enum.php.

248 {
249 $array = static::toArray();
250
251 return isset($array[$key]) || \array_key_exists($key, $array);
252 }

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

+ 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.

302 {
303 return $this->getValue();
304 }

References MyCLabs\Enum\Enum\getValue().

+ 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.

@psalm-pure @psalm-return list<string>

Returns
array

Definition at line 152 of file Enum.php.

153 {
154 return \array_keys(static::toArray());
155 }

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

+ Here is the call graph for this function:

◆ search()

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

Return key for value.

Parameters
mixed$value

@psalm-param mixed $value @psalm-pure

Returns
string|false

Definition at line 263 of file Enum.php.

264 {
265 return \array_search($value, static::toArray(), true);
266 }

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

+ Here is the call graph for this function:

◆ toArray()

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

Returns all possible values as an array.

@psalm-pure @psalm-suppress ImpureStaticProperty

@psalm-return array<string, mixed>

Returns
array Constant name in key, constant value in value

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

@psalm-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.

@psalm-pure @psalm-return array<string, static>

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

@psalm-var T $value

Definition at line 164 of file Enum.php.

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

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

+ 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: