This commit is contained in:
xl
2023-11-13 22:37:49 +08:00
parent b15d02be12
commit cde7fe3f2d
+18 -7
View File
@@ -3,10 +3,12 @@ declare(strict_types=1);
namespace Kiri\Router\Constrict; namespace Kiri\Router\Constrict;
use Kiri\Core\Xml;
use Kiri\Router\Base\AuthorizationInterface; use Kiri\Router\Base\AuthorizationInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Swoole\Http\Request;
class ConstrictRequest extends Message implements RequestInterface, ServerRequestInterface class ConstrictRequest extends Message implements RequestInterface, ServerRequestInterface
{ {
@@ -383,7 +385,7 @@ class ConstrictRequest extends Message implements RequestInterface, ServerReques
{ {
// TODO: Implement getParsedBody() method. // TODO: Implement getParsedBody() method.
if ($this->parsedBody instanceof \Closure) { if ($this->parsedBody instanceof \Closure) {
$this->parsedBody = call_user_func($this->parsedBody); $this->parsedBody = call_user_func($this->parsedBody, $this->getBody()->getContents());
} }
return $this->parsedBody; return $this->parsedBody;
} }
@@ -410,7 +412,7 @@ class ConstrictRequest extends Message implements RequestInterface, ServerReques
* immutability of the message, and MUST return an instance that has the * immutability of the message, and MUST return an instance that has the
* updated body parameters. * updated body parameters.
* *
* @param null|array|object $data The deserialized body data. This will * @param null|array|object|Request $data The deserialized body data. This will
* typically be in an array or object. * typically be in an array or object.
* @return static * @return static
* @throws \InvalidArgumentException if an unsupported argument type is * @throws \InvalidArgumentException if an unsupported argument type is
@@ -418,8 +420,17 @@ class ConstrictRequest extends Message implements RequestInterface, ServerReques
*/ */
public function withParsedBody($data): static public function withParsedBody($data): static
{ {
// TODO: Implement withParsedBody() method. $contentType = $data->header['content-type'] ?? 'application/json';
$this->parsedBody = $data; $post = $data->post;
$this->parsedBody = static function (string $content) use ($contentType, $post) {
if (\str_contains($contentType, 'json')) {
return \json_decode($content, true);
} else if (\str_contains($contentType, 'xml')) {
return Xml::toArray($content);
} else {
return $post ?? [];
}
};
return $this; return $this;
} }
@@ -509,8 +520,8 @@ class ConstrictRequest extends Message implements RequestInterface, ServerReques
public function offset(): int public function offset(): int
{ {
$params = $this->getQueryParams(); $params = $this->getQueryParams();
$page = (int)($params['page'] ?? 1); $page = (int)($params['page'] ?? 1);
$size = $this->size(); $size = $this->size();
if ($page < 1) { if ($page < 1) {
$page = 1; $page = 1;
} }
@@ -526,7 +537,7 @@ class ConstrictRequest extends Message implements RequestInterface, ServerReques
public function size(): int public function size(): int
{ {
$params = $this->getQueryParams(); $params = $this->getQueryParams();
$size = (int)($params['size'] ?? 1); $size = (int)($params['size'] ?? 1);
if ($size < 1) { if ($size < 1) {
$size = 1; $size = 1;
} }