Skip to content

Commit a4d8ab2

Browse files
committed
Lists could not be exported because of error in Php-Enum lib #1662
1 parent 7d8d8b3 commit a4d8ab2

File tree

1 file changed

+78
-10
lines changed
  • adm_program/libs/server/myclabs/php-enum/src

1 file changed

+78
-10
lines changed

adm_program/libs/server/myclabs/php-enum/src/Enum.php

+78-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
*
1818
* @psalm-template T
1919
* @psalm-immutable
20+
* @psalm-consistent-constructor
2021
*/
21-
abstract class Enum implements \JsonSerializable
22+
abstract class Enum implements \JsonSerializable, \Stringable
2223
{
2324
/**
2425
* Enum value
@@ -28,6 +29,13 @@ abstract class Enum implements \JsonSerializable
2829
*/
2930
protected $value;
3031

32+
/**
33+
* Enum key, the constant name
34+
*
35+
* @var string
36+
*/
37+
private $key;
38+
3139
/**
3240
* Store existing constants in a static cache per object.
3341
*
@@ -51,7 +59,7 @@ abstract class Enum implements \JsonSerializable
5159
* @psalm-pure
5260
* @param mixed $value
5361
*
54-
* @psalm-param static<T>|T $value
62+
* @psalm-param T $value
5563
* @throws \UnexpectedValueException if incompatible type is given.
5664
*/
5765
public function __construct($value)
@@ -61,15 +69,40 @@ public function __construct($value)
6169
$value = $value->getValue();
6270
}
6371

64-
if (!$this->isValid($value)) {
65-
/** @psalm-suppress InvalidCast */
66-
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
67-
}
72+
/** @psalm-suppress ImplicitToStringCast assertValidValueReturningKey returns always a string but psalm has currently an issue here */
73+
$this->key = static::assertValidValueReturningKey($value);
6874

6975
/** @psalm-var T */
7076
$this->value = $value;
7177
}
7278

79+
/**
80+
* This method exists only for the compatibility reason when deserializing a previously serialized version
81+
* that didn't had the key property
82+
*/
83+
public function __wakeup()
84+
{
85+
/** @psalm-suppress DocblockTypeContradiction key can be null when deserializing an enum without the key */
86+
if ($this->key === null) {
87+
/**
88+
* @psalm-suppress InaccessibleProperty key is not readonly as marked by psalm
89+
* @psalm-suppress PossiblyFalsePropertyAssignmentValue deserializing a case that was removed
90+
*/
91+
$this->key = static::search($this->value);
92+
}
93+
}
94+
95+
/**
96+
* @param mixed $value
97+
* @return static
98+
*/
99+
public static function from($value): self
100+
{
101+
$key = static::assertValidValueReturningKey($value);
102+
103+
return self::__callStatic($key, []);
104+
}
105+
73106
/**
74107
* @psalm-pure
75108
* @return mixed
@@ -84,11 +117,11 @@ public function getValue()
84117
* Returns the enum key (i.e. the constant name).
85118
*
86119
* @psalm-pure
87-
* @return mixed
120+
* @return string
88121
*/
89122
public function getKey()
90123
{
91-
return static::search($this->value);
124+
return $this->key;
92125
}
93126

94127
/**
@@ -163,7 +196,9 @@ public static function toArray()
163196
$class = static::class;
164197

165198
if (!isset(static::$cache[$class])) {
199+
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
166200
$reflection = new \ReflectionClass($class);
201+
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
167202
static::$cache[$class] = $reflection->getConstants();
168203
}
169204

@@ -176,13 +211,43 @@ public static function toArray()
176211
* @param $value
177212
* @psalm-param mixed $value
178213
* @psalm-pure
214+
* @psalm-assert-if-true T $value
179215
* @return bool
180216
*/
181217
public static function isValid($value)
182218
{
183219
return \in_array($value, static::toArray(), true);
184220
}
185221

222+
/**
223+
* Asserts valid enum value
224+
*
225+
* @psalm-pure
226+
* @psalm-assert T $value
227+
* @param mixed $value
228+
*/
229+
public static function assertValidValue($value): void
230+
{
231+
self::assertValidValueReturningKey($value);
232+
}
233+
234+
/**
235+
* Asserts valid enum value
236+
*
237+
* @psalm-pure
238+
* @psalm-assert T $value
239+
* @param mixed $value
240+
* @return string
241+
*/
242+
private static function assertValidValueReturningKey($value): string
243+
{
244+
if (false === ($key = static::search($value))) {
245+
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
246+
}
247+
248+
return $key;
249+
}
250+
186251
/**
187252
* Check if is valid enum key
188253
*
@@ -201,11 +266,11 @@ public static function isValidKey($key)
201266
/**
202267
* Return key for value
203268
*
204-
* @param $value
269+
* @param mixed $value
205270
*
206271
* @psalm-param mixed $value
207272
* @psalm-pure
208-
* @return mixed
273+
* @return string|false
209274
*/
210275
public static function search($value)
211276
{
@@ -220,6 +285,8 @@ public static function search($value)
220285
*
221286
* @return static
222287
* @throws \BadMethodCallException
288+
*
289+
* @psalm-pure
223290
*/
224291
public static function __callStatic($name, $arguments)
225292
{
@@ -243,6 +310,7 @@ public static function __callStatic($name, $arguments)
243310
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
244311
* @psalm-pure
245312
*/
313+
#[\ReturnTypeWillChange]
246314
public function jsonSerialize()
247315
{
248316
return $this->getValue();

0 commit comments

Comments
 (0)