From d10582f359ff690ac5889ab3c8d036acac32ee50 Mon Sep 17 00:00:00 2001 From: jiannei Date: Wed, 11 Oct 2023 15:29:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20Fractal=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84=20Format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Support/Facades/Format.php | 1 - src/Support/Format.php | 9 +++++++++ src/Support/Traits/JsonResponseTrait.php | 8 -------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Support/Facades/Format.php b/src/Support/Facades/Format.php index 26031a9..1490a67 100644 --- a/src/Support/Facades/Format.php +++ b/src/Support/Facades/Format.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Facade as IlluminateFacade; /** - * * @method static JsonResponse response($data = [], int $status = 200, array $headers = [], int $options = 0) * @method static array data($data, ?string $message, int $code, $errors = null) * @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource) diff --git a/src/Support/Format.php b/src/Support/Format.php index 4656c9c..1fdc0d6 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -12,6 +12,7 @@ namespace Jiannei\Response\Laravel\Support; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -57,6 +58,14 @@ public function response($data = [], int $status = 200, array $headers = [], int */ public function data($data, ?string $message, int $code, $errors = null): array { + $data = match (true) { + $data instanceof ResourceCollection => $this->resourceCollection($data), + $data instanceof JsonResource => $this->jsonResource($data), + $data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => $this->paginator($data), + $data instanceof Arrayable || (is_object($data) && method_exists($data, 'toArray')) => $data->toArray(), + default => Arr::wrap($data) + }; + return $this->formatDataFields([ 'status' => $this->formatStatus($code), 'code' => $code, diff --git a/src/Support/Traits/JsonResponseTrait.php b/src/Support/Traits/JsonResponseTrait.php index 631ada3..b375952 100644 --- a/src/Support/Traits/JsonResponseTrait.php +++ b/src/Support/Traits/JsonResponseTrait.php @@ -199,14 +199,6 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra */ public function success($data = [], string $message = '', int $code = 200, array $headers = [], int $option = 0) { - $data = match (true) { - $data instanceof ResourceCollection => Format::resourceCollection($data), - $data instanceof JsonResource => Format::jsonResource($data), - $data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => Format::paginator($data), - $data instanceof Arrayable || (is_object($data) && method_exists($data, 'toArray')) => $data->toArray(), - default => Arr::wrap($data) - }; - return Format::response(Format::data($data,$message,$code), $code, $headers, $option); } }