*/ private array $callbackMap = []; /** @phpstan-param \Closure(string $input) : T $callback */ public function register(string $alias, \Closure $callback) : void{ $key = $this->reprocess($alias); if(isset($this->callbackMap[$key])){ throw new \InvalidArgumentException("Alias \"$key\" is already registered"); } $this->callbackMap[$key] = $callback; } /** @phpstan-param \Closure(string $input) : T $callback */ public function override(string $alias, \Closure $callback) : void{ $this->callbackMap[$this->reprocess($alias)] = $callback; } /** * Tries to parse the specified string into an enchantment. * @phpstan-return T|null */ public function parse(string $input){ $key = $this->reprocess($input); if(isset($this->callbackMap[$key])){ return ($this->callbackMap[$key])($input); } return null; } protected function reprocess(string $input) : string{ return strtolower(str_replace([" ", "minecraft:"], ["_", ""], trim($input))); } /** @return string[]|int[] */ public function getKnownAliases() : array{ return array_keys($this->callbackMap); } }