From 759b33e44c95ca0e854a2e0c80bd13ab6525ec7a Mon Sep 17 00:00:00 2001 From: Viktor Masicek Date: Tue, 18 Jun 2024 13:46:55 +0200 Subject: [PATCH 1/2] Support older PHP and Nette --- composer.json | 2 +- src/ApiRoute.php | 4 ++-- src/Exception/FormatInputException.php | 3 +-- src/Exception/FormatSchemaException.php | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 518cd6a..8ebedc8 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": ">=8.0", + "php": ">=7.4", "ext-json": "*", "contributte/api-router": "^4.0|^5.0", "opis/json-schema": "^2.3" diff --git a/src/ApiRoute.php b/src/ApiRoute.php index 83a3606..1e1891d 100755 --- a/src/ApiRoute.php +++ b/src/ApiRoute.php @@ -23,7 +23,7 @@ class ApiRoute extends \Contributte\ApiRouter\ApiRoute * @param array $data See parent constructor * @param ?array $bodySchema JSON schema as returned when parsed by json_decode */ - public function __construct(array|string $path, string $presenter, array $data, ?array $bodySchema = null) + public function __construct($path, string $presenter, array $data, ?array $bodySchema = null) { parent::__construct($path, $presenter, $data); $this->bodySchema = $bodySchema; @@ -100,7 +100,7 @@ public function resolveMethod(Nette\Http\IRequest $request): string if (!in_array($request->getMethod(), $this->getMethods(), true)) { throw new ClientException( 'Allowed methods: ' . implode(', ', $this->getMethods()) . '.', - Nette\Http\IResponse::S405_MethodNotAllowed + 405 // MethodNotAllowed ); } diff --git a/src/Exception/FormatInputException.php b/src/Exception/FormatInputException.php index 65e03d4..4cf4d77 100755 --- a/src/Exception/FormatInputException.php +++ b/src/Exception/FormatInputException.php @@ -5,12 +5,11 @@ namespace ADT\ApiJsonRouter\Exception; use Exception; -use Nette\Http\IResponse; class FormatInputException extends Exception implements ApiJsonRouterException { public function __construct(string $message) { - parent::__construct($message, IResponse::S400_BadRequest); + parent::__construct($message, 400); } } diff --git a/src/Exception/FormatSchemaException.php b/src/Exception/FormatSchemaException.php index 5348a04..aba529d 100755 --- a/src/Exception/FormatSchemaException.php +++ b/src/Exception/FormatSchemaException.php @@ -5,12 +5,11 @@ namespace ADT\ApiJsonRouter\Exception; use Exception; -use Nette\Http\IResponse; class FormatSchemaException extends Exception implements ApiJsonRouterException { public function __construct(string $message) { - parent::__construct($message, IResponse::S500_InternalServerError); + parent::__construct($message, 500); } } From b69c27f7eb208d6bdd41257b8e964da4e1df2c6b Mon Sep 17 00:00:00 2001 From: Viktor Masicek Date: Tue, 9 Jul 2024 10:24:55 +0200 Subject: [PATCH 2/2] Log used HTTP method on not allowed method (error 405) --- src/ApiRoute.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApiRoute.php b/src/ApiRoute.php index 1e1891d..0bcaa06 100755 --- a/src/ApiRoute.php +++ b/src/ApiRoute.php @@ -99,7 +99,7 @@ public function resolveMethod(Nette\Http\IRequest $request): string { if (!in_array($request->getMethod(), $this->getMethods(), true)) { throw new ClientException( - 'Allowed methods: ' . implode(', ', $this->getMethods()) . '.', + 'Allowed methods: ' . implode(', ', $this->getMethods()) . '. Used method: ' . $request->getMethod() . '.', 405 // MethodNotAllowed ); }