diff --git a/VERSION b/VERSION index c60bc60..42087a9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2503.72020 +0.2503.72025 diff --git a/src/Http/Routing/Router.php b/src/Http/Routing/Router.php index 29425c4..b745b62 100644 --- a/src/Http/Routing/Router.php +++ b/src/Http/Routing/Router.php @@ -6,6 +6,7 @@ namespace Index\Http\Routing; use Stringable; +use InvalidArgumentException; use JsonSerializable; use Index\Bencode\{Bencode,BencodeSerializable}; use Index\Http\{HttpRequest,HttpStream}; @@ -37,14 +38,14 @@ class Router implements RequestHandlerInterface { /** * @param string $charSet Default character set to specify when none is present. - * @param ErrorHandler|string $errorHandler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. + * @param ErrorHandler|'html'|'plain' $errorHandler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. */ public function __construct( string $charSet = '', ErrorHandler|string $errorHandler = 'html' ) { $this->charSetValue = $charSet; - $this->errorHandler = $errorHandler; + $this->setErrorHandler($errorHandler); } /** @@ -68,33 +69,37 @@ class Router implements RequestHandlerInterface { /** * Error handler instance. - * - * @var ErrorHandler */ - public ErrorHandler $errorHandler { - get => $this->errorHandler; - set(ErrorHandler|string $handler) { - if($handler instanceof ErrorHandler) - $this->errorHandler = $handler; - elseif($handler === 'html') - $this->setHtmlErrorHandler(); - else // plain - $this->setPlainErrorHandler(); - } + public ErrorHandler $errorHandler; + + /** + * Sets an error handler. + * + * @param ErrorHandler|'html'|'plain' $handler + */ + public function setErrorHandler(ErrorHandler|string $handler): void { + if($handler instanceof ErrorHandler) + $this->errorHandler = $handler; + elseif($handler === 'html') + $this->errorHandler = new HtmlErrorHandler; + elseif($handler === 'plain') + $this->errorHandler = new PlainErrorHandler; + else + throw new InvalidArgumentException('$handler must be an instance of ErrorHandler or "html" or "plain"'); } /** * Set the error handler to the basic HTML one. */ public function setHtmlErrorHandler(): void { - $this->errorHandler = new HtmlErrorHandler; + $this->setErrorHandler('html'); } /** * Set the error handler to the plain text one. */ public function setPlainErrorHandler(): void { - $this->errorHandler = new PlainErrorHandler; + $this->setErrorHandler('plain'); } /**