diff --git a/composer.json b/composer.json index 73dd3bb..6f43412 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ } }, "require": { - "php": ">=7.4", - "contributte/api-router": "^4.0", + "php": ">= 7.1.0", + "contributte/api-router": "^2.0", "opis/json-schema": "^2.3" }, "require-dev": { diff --git a/src/ApiRouteFormat.php b/src/ApiRouteFormat.php index 1c60dfc..5736409 100644 --- a/src/ApiRouteFormat.php +++ b/src/ApiRouteFormat.php @@ -2,7 +2,7 @@ namespace ADT\ApiJsonRouter; -use Contributte\ApiRouter\ApiRoute; +use Ublaboo\ApiRouter\ApiRoute; use Nette; use Opis\JsonSchema\Errors\ErrorFormatter; use Opis\JsonSchema\Exceptions\ParseException; @@ -84,7 +84,7 @@ protected function verifyBodyFormat($body, $schema) { } } - public function match(Nette\Http\IRequest $httpRequest): ?array { + public function match(Nette\Http\IRequest $httpRequest): ?Nette\Application\Request { $result = parent::match($httpRequest); if ($result === NULL || $this->bodySchema === NULL) { @@ -104,24 +104,31 @@ public function match(Nette\Http\IRequest $httpRequest): ?array { } $this->verifyBodyFormat($body, $this->bodySchema); if (isset($this->bodySchema['properties'])) { + $parameters = $result->getParameters(); foreach ($this->bodySchema['properties'] as $key => $value) { if (isset($body->$key)) { - $result["_$key"] = $body->$key; + $parameters["_$key"] = $body->$key; } } + $result->setParameters($parameters); } } catch (FormatSchemaError | FormatInputError $e) { if (self::$throwErrors) { throw $e; } else { - return [ - 'presenter' => self::$errorPresenter, - 'action' => self::$errorAction, - 'secured' => FALSE, - 'error' => $e::ERROR_MESSAGE, - 'code' => $e::ERROR_CODE, - 'message' => $e->getMessage(), - ]; + return new Nette\Application\Request( + self::$errorPresenter, + Nette\Application\Request::FORWARD, + [ + 'action' => self::$errorAction, + 'error' => $e::ERROR_MESSAGE, + 'code' => $e::ERROR_CODE, + 'message' => $e->getMessage(), + ], + $httpRequest->getPost(), + $httpRequest->getFiles(), + [Nette\Application\Request::SECURED => $httpRequest->isSecured()] + ); } } return $result; diff --git a/tests/unit/ApiRouterFormat/MatchTest.php b/tests/unit/ApiRouterFormat/MatchTest.php index d18c381..f3cee04 100644 --- a/tests/unit/ApiRouterFormat/MatchTest.php +++ b/tests/unit/ApiRouterFormat/MatchTest.php @@ -32,7 +32,7 @@ public function testInvalidBody() { $url = new UrlScript('http://www.example.com/api/item', '/'); $bodyJson = 'wrong json body'; - $request = new Request($url, null, null, null, null, 'GET', null, null, function () use ($bodyJson) {return $bodyJson;}); + $request = new Request($url, null, null, null, null, null, 'GET', null, null, function () use ($bodyJson) {return $bodyJson;}); $appRequest = $route->match($request); $this->assertRequestHasParamWithValue('error', 'INVALID_FORMAT', $appRequest); diff --git a/tests/unit/BaseUnit.php b/tests/unit/BaseUnit.php index 50db2ce..951cc46 100644 --- a/tests/unit/BaseUnit.php +++ b/tests/unit/BaseUnit.php @@ -20,7 +20,7 @@ public static function getRoute(?array $body) { public static function getRequest(?array $body) { $url = new UrlScript('http://www.example.com/api/item', '/'); $bodyJson = json_encode($body); - return new Request($url, null, null, null, null, 'GET', null, null, function () use ($bodyJson) {return $bodyJson;}); + return new Request($url, null, null, null, null, null, 'GET', null, null, function () use ($bodyJson) {return $bodyJson;}); } protected function assertNotError($appRequest) { @@ -51,8 +51,8 @@ protected function assertRequestHasParamWithValue($expectedParamName, $expectedP /** * This method is for better merging into branches with old Nette */ - protected function getAppRequestParameters($appRequest) { - return $appRequest; + protected function getAppRequestParameters(\Nette\Application\Request $appRequest) { + return $appRequest->getParameters(); } }