22declare(strict_types=1);
26use
function array_map;
28use
function mb_strtoupper;
29use
function preg_match;
43 private static $members =
null;
45 private static function verifyName(
string $name) :
void{
46 if(preg_match(
'/^(?!\d)[A-Za-z\d_]+$/u', $name) === 0){
47 throw new \InvalidArgumentException(
"Invalid member name \"$name\", should only contain letters, numbers and underscores, and must not start with a number");
56 private static function _registryRegister(
string $name,
object $member) :
void{
57 if(self::$members ===
null){
60 self::verifyName($name);
61 $upperName = mb_strtoupper($name);
62 if(isset(self::$members[$upperName])){
63 throw new \InvalidArgumentException(
"\"$upperName\" is already reserved");
65 self::$members[$upperName] = $member;
73 abstract protected static function setup() : void;
80 protected static function checkInit() : void{
81 if(self::$members === null){
90 private static function _registryFromString(
string $name) : object{
92 if(self::$members ===
null){
93 throw new AssumptionFailedError(self::class .
"::checkInit() did not initialize self::\$members correctly");
95 $upperName = mb_strtoupper($name);
96 if(!isset(self::$members[$upperName])){
97 throw new \InvalidArgumentException(
"No such registry member: " . self::class .
"::" . $upperName);
99 return self::preprocessMember(self::$members[$upperName]);
102 protected static function preprocessMember(
object $member) : object{
110 public static function __callStatic(
string $name, array $arguments) : object{
111 if(count($arguments) > 0){
112 throw new \ArgumentCountError(
"Expected exactly 0 arguments, " . count($arguments) .
" passed");
116 if(self::$members !==
null && isset(self::$members[$name])){
117 return self::preprocessMember(self::$members[$name]);
122 return self::_registryFromString($name);
123 }
catch(\InvalidArgumentException $e){
124 throw new \Error($e->getMessage(), 0, $e);
132 private static function _registryGetAll() : array{
134 return array_map(self::preprocessMember(...), self::$members ??
throw new AssumptionFailedError(self::class .
"::checkInit() did not initialize self::\$members correctly"));
static __callStatic(string $name, array $arguments)