22declare(strict_types=1);
26use
function array_keys;
27use
function str_replace;
28use
function strtolower;
45 private array $callbackMap = [];
48 public function register(
string $alias, \Closure $callback) :
void{
49 $key = $this->reprocess($alias);
50 if(isset($this->callbackMap[$key])){
51 throw new \InvalidArgumentException(
"Alias \"$key\" is already registered");
53 $this->callbackMap[$key] = $callback;
57 public function override(
string $alias, \Closure $callback) :
void{
58 $this->callbackMap[$this->reprocess($alias)] = $callback;
65 $existingKey = $this->reprocess($existing);
66 if(!isset($this->callbackMap[$existingKey])){
67 throw new \InvalidArgumentException(
"Cannot register new alias for unknown existing alias \"$existing\"");
69 $newKey = $this->reprocess($alias);
70 if(isset($this->callbackMap[$newKey])){
71 throw new \InvalidArgumentException(
"Alias \"$newKey\" is already registered");
73 $this->callbackMap[$newKey] = $this->callbackMap[$existingKey];
80 public function parse(
string $input){
81 $key = $this->reprocess($input);
82 if(isset($this->callbackMap[$key])){
83 return ($this->callbackMap[$key])($input);
89 protected function reprocess(
string $input) : string{
90 return strtolower(str_replace([
" ",
"minecraft:"], [
"_",
""], trim($input)));
95 return array_keys($this->callbackMap);
registerAlias(string $existing, string $alias)