This commit is contained in:
2021-09-10 18:04:57 +08:00
parent 5b33d09da0
commit cde1704866
2 changed files with 14 additions and 15 deletions
+12 -12
View File
@@ -10,26 +10,26 @@ class Parse
/**
* @param $content
* @param $contentType
* @return mixed
* @throws \Exception
*/
public static function data($content, $contentType): mixed
public static function data($content): mixed
{
if (empty($content)) {
return null;
}
if (str_starts_with($content, '<') || str_contains($contentType, 'xml')) {
return Xml::toArray($content);
if (is_bool($content) || is_numeric($content)) {
return $content;
}
if (str_contains($contentType, 'x-www-form-urlencoded')) {
parse_str($content, $array);
return $array;
}
if (str_contains($contentType, 'serialize')) {
return unserialize($content);
}
return json_decode($content, true);
$start = substr($content, 0, 1);
return match ($start) {
'<' => Xml::toArray($content),
'[', '{' => json_decode($content, true),
default => call_user_func(function () use ($content) {
parse_str($content, $array);
return $array;
})
};
}
}
+2 -3
View File
@@ -67,7 +67,6 @@ class ServerRequest extends Request implements ServerRequestInterface
*/
public static function createServerRequest(\Swoole\Http\Request $request): static|ServerRequestInterface
{
$contentType = $request->header['content-type'];
return (new static())->parseRequestHeaders($request)
->withServerParams($request->server)
->withServerTarget($request)
@@ -77,9 +76,9 @@ class ServerRequest extends Request implements ServerRequestInterface
->withQueryParams($request->get ?? [])
->withUploadedFiles($request->files ?? [])
->withMethod($request->getMethod())
->withParsedBody(function (StreamInterface $stream, ?array $posts) use ($contentType) {
->withParsedBody(function (StreamInterface $stream, ?array $posts) {
try {
$content = Parse::data($stream->getContents(), $contentType);
$content = Parse::data($stream->getContents());
if (!empty($content)) {
return $content;
}