diff --git a/source/app/src/DiBinder.php b/source/app/src/DiBinder.php index bcac0f1..cafbf70 100644 --- a/source/app/src/DiBinder.php +++ b/source/app/src/DiBinder.php @@ -27,7 +27,6 @@ use Aura\SqlQuery\QueryFactory; use Koriym\QueryLocator\QueryLocator; use Koriym\QueryLocator\QueryLocatorInterface; -use Laminas\Diactoros\ServerRequestFactory; use MyVendor\MyPackage\Auth\AdminAuthenticationHandler; use MyVendor\MyPackage\Auth\AdminAuthenticator; use MyVendor\MyPackage\Auth\AdminAuthenticatorInterface; @@ -44,6 +43,7 @@ use MyVendor\MyPackage\Responder\WebResponder; use MyVendor\MyPackage\Router\CliRouter; use MyVendor\MyPackage\Router\RouterInterface; +use MyVendor\MyPackage\Router\ServerRequestFactory; use MyVendor\MyPackage\Router\WebRouter; use MyVendor\MyPackage\TemplateEngine\QiqCustomHelper; use MyVendor\MyPackage\TemplateEngine\QiqRenderer; diff --git a/source/app/src/Router/ServerRequestFactory.php b/source/app/src/Router/ServerRequestFactory.php new file mode 100644 index 0000000..4aec3bb --- /dev/null +++ b/source/app/src/Router/ServerRequestFactory.php @@ -0,0 +1,73 @@ +getHeader('content-type'), + true, + ); + if ($isFormUrlEncoded) { + return self::parseFormUrlEncoded($serverRequest); + } + + $isJson = in_array( + 'application/json', + $serverRequest->getHeader('content-type'), + true, + ); + if (! $isJson) { + return $serverRequest; + } + + return self::parseJson($serverRequest); + } + + private static function parseFormUrlEncoded(ServerRequestInterface $serverRequest): ServerRequestInterface + { + parse_str((string) $serverRequest->getBody(), $parsedBody); + + return $serverRequest->withParsedBody($parsedBody); + } + + private static function parseJson(ServerRequestInterface $serverRequest): ServerRequestInterface + { + $parsedBody = json_decode( + (string) $serverRequest->getBody(), + true, + 512, + JSON_THROW_ON_ERROR, + ); + + $error = json_last_error(); + if ($error !== JSON_ERROR_NONE) { + throw new InvalidRequestException(json_last_error_msg()); + } + + assert(is_array($parsedBody)); + + return $serverRequest->withParsedBody($parsedBody); + } +} diff --git a/source/app/src/Router/WebRouter.php b/source/app/src/Router/WebRouter.php index ca56b94..85bc719 100644 --- a/source/app/src/Router/WebRouter.php +++ b/source/app/src/Router/WebRouter.php @@ -8,17 +8,6 @@ use Aura\Router\RouterContainer; use Psr\Http\Message\ServerRequestInterface; -use function assert; -use function in_array; -use function is_array; -use function json_decode; -use function json_last_error; -use function json_last_error_msg; -use function parse_str; - -use const JSON_ERROR_NONE; -use const JSON_THROW_ON_ERROR; - final class WebRouter implements RouterInterface { public function __construct( @@ -30,66 +19,12 @@ public function match(ServerRequestInterface $serverRequest): RouterMatch { $matcher = $this->routerContainer->getMatcher(); - $isFormUrlEncoded = in_array( - 'application/x-www-form-urlencoded', - $serverRequest->getHeader('content-type'), - true, - ); - if ($isFormUrlEncoded) { - return new RouterMatch( - $serverRequest->getMethod(), - $serverRequest->getUri()->getPath(), - $matcher->match($serverRequest), - $this->parseFormUrlEncoded($serverRequest), - ); - } - - $isJson = in_array( - 'application/json', - $serverRequest->getHeader('content-type'), - true, - ); - if (! $isJson) { - return new RouterMatch( - $serverRequest->getMethod(), - $serverRequest->getUri()->getPath(), - $matcher->match($serverRequest), - $serverRequest, - ); - } - return new RouterMatch( $serverRequest->getMethod(), $serverRequest->getUri()->getPath(), $matcher->match($serverRequest), - $this->parseJson($serverRequest), - ); - } - - private function parseFormUrlEncoded(ServerRequestInterface $serverRequest): ServerRequestInterface - { - parse_str((string) $serverRequest->getBody(), $parsedBody); - - return $serverRequest->withParsedBody($parsedBody); - } - - private function parseJson(ServerRequestInterface $serverRequest): ServerRequestInterface - { - $parsedBody = json_decode( - (string) $serverRequest->getBody(), - true, - 512, - JSON_THROW_ON_ERROR, + $serverRequest, ); - - $error = json_last_error(); - if ($error !== JSON_ERROR_NONE) { - throw new InvalidRequestException(json_last_error_msg()); - } - - assert(is_array($parsedBody)); - - return $serverRequest->withParsedBody($parsedBody); } /** @param array $data */