Merge branch 'MDL-70319-311' of git://github.com/ilyatregubov/moodle into MOODLE_311_STABLE

This commit is contained in:
Jun Pataleta 2021-01-20 22:40:35 +08:00
commit e4527dc68f
2 changed files with 18 additions and 7 deletions

View file

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

View file

@ -349,7 +349,7 @@
<location>php-enum</location> <location>php-enum</location>
<name>php-enum</name> <name>php-enum</name>
<license>MIT</license> <license>MIT</license>
<version>1.7.6</version> <version>1.7.7</version>
</library> </library>
<library> <library>
<location>http-message</location> <location>http-message</location>