MDL-70319 core: Upgrade php-enum to 1.7.7

This commit is contained in:
Ilya Tregubov 2021-01-19 10:02:00 +02:00
parent c381757f2a
commit c3eff2e3b4

View File

@ -37,6 +37,14 @@ abstract class Enum implements \JsonSerializable
*/
protected static $cache = [];
/**
* Cache of instances of the Enum class
*
* @var array
* @psalm-var array<class-string, array<string, static>>
*/
protected static $instances = [];
/**
* Creates a new value of some type
*
@ -211,17 +219,20 @@ abstract class Enum implements \JsonSerializable
* @param array $arguments
*
* @return static
* @psalm-pure
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
{
$array = static::toArray();
if (isset($array[$name]) || \array_key_exists($name, $array)) {
return new static($array[$name]);
$class = static::class;
if (!isset(self::$instances[$class][$name])) {
$array = static::toArray();
if (!isset($array[$name]) && !\array_key_exists($name, $array)) {
$message = "No static method or enum constant '$name' in class " . static::class;
throw new \BadMethodCallException($message);
}
return self::$instances[$class][$name] = new static($array[$name]);
}
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . static::class);
return clone self::$instances[$class][$name];
}
/**