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); } }