StringToTParser: added registerAlias()

This commit is contained in:
Dylan K. Taylor 2023-08-23 15:24:29 +01:00
parent df96e023dc
commit 20a41b00ba
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -58,6 +58,21 @@ abstract class StringToTParser{
$this->callbackMap[$this->reprocess($alias)] = $callback;
}
/**
* Registers a new alias for an existing known alias.
*/
public function registerAlias(string $existing, string $alias) : void{
$existingKey = $this->reprocess($existing);
if(!isset($this->callbackMap[$existingKey])){
throw new \InvalidArgumentException("Cannot register new alias for unknown existing alias \"$existing\"");
}
$newKey = $this->reprocess($alias);
if(isset($this->callbackMap[$newKey])){
throw new \InvalidArgumentException("Alias \"$newKey\" is already registered");
}
$this->callbackMap[$newKey] = $this->callbackMap[$existingKey];
}
/**
* Tries to parse the specified string into a corresponding instance of T.
* @phpstan-return T|null