Reverted errorHandler setter to a method.

This commit is contained in:
flash 2025-03-07 20:25:24 +00:00
parent fcfa275706
commit 6fec3889a6
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
2 changed files with 22 additions and 17 deletions
VERSION
src/Http/Routing

View file

@ -1 +1 @@
0.2503.72020
0.2503.72025

View file

@ -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');
}
/**