From 36c03cec7934c784a0bd57eda2abac44d1cefb93 Mon Sep 17 00:00:00 2001 From: abdulmohsen Date: Mon, 22 Apr 2024 18:09:39 +0300 Subject: [PATCH] Added new method to DataUtil to load data from Request. --- src/Libs/DataUtil.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Libs/DataUtil.php b/src/Libs/DataUtil.php index 042aa811..a12f3e32 100644 --- a/src/Libs/DataUtil.php +++ b/src/Libs/DataUtil.php @@ -6,6 +6,7 @@ namespace App\Libs; use Closure; use JsonSerializable; +use Psr\Http\Message\ServerRequestInterface as iRequest; use Stringable; final readonly class DataUtil implements JsonSerializable, Stringable @@ -22,6 +23,17 @@ final readonly class DataUtil implements JsonSerializable, Stringable return new self($data); } + public static function fromRequest(iRequest $request, bool $includeQueryParams = false): self + { + $params = $includeQueryParams ? $request->getQueryParams() : []; + + if (null !== ($data = $request->getParsedBody())) { + $params = array_replace_recursive($params, is_object($data) ? (array)$data : $data); + } + + return new self($params); + } + public function get(string $key, mixed $default = null): mixed { return ag($this->data, $key, $default);