-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6306c64
commit e38877f
Showing
3 changed files
with
75 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyPackage\Router; | ||
|
||
use Laminas\Diactoros\ServerRequestFactory as LaminasServerRequestFactory; | ||
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 ServerRequestFactory | ||
{ | ||
public static function fromGlobals(): ServerRequestInterface | ||
{ | ||
$serverRequest = LaminasServerRequestFactory::fromGlobals(); | ||
|
||
$isFormUrlEncoded = in_array( | ||
'application/x-www-form-urlencoded', | ||
$serverRequest->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters