From 916dbdc21c9a2dc4b43a73cda5738ba791f28e2f Mon Sep 17 00:00:00 2001 From: Akhmad Salafudin Date: Tue, 13 Dec 2022 16:17:52 +0700 Subject: [PATCH] refactor: change type data Collection to Arrayable --- src/ApiResponse.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ApiResponse.php b/src/ApiResponse.php index 43f6a5b..baa17e4 100644 --- a/src/ApiResponse.php +++ b/src/ApiResponse.php @@ -2,13 +2,13 @@ namespace KodePandai\ApiResponse; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Responsable; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Response; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Fluent; use InvalidArgumentException; @@ -62,12 +62,12 @@ public function addHeaders(array $headers): self public function toResponse($request): JsonResponse { return (new JsonResponse([ - 'success' => $this->isSuccess, - 'title' => $this->title, - 'message' => $this->message, - 'data' => $this->data, - 'errors' => $this->errors, - ])) + 'success' => $this->isSuccess, + 'title' => $this->title, + 'message' => $this->message, + 'data' => $this->data, + 'errors' => $this->errors, + ])) ->setStatusCode($this->statusCode) ->withHeaders($this->headers); } @@ -83,7 +83,7 @@ public static function create(): self /** * Return a success api response. * - * @param array|Collection|JsonResource|ResourceCollection $data + * @param array|Arrayable|JsonResource|ResourceCollection $data */ public static function success($data = []): self { @@ -135,10 +135,10 @@ public static function validateOrFail( } /** - * Add data to response and transform according to its type. - * - * @param array|Collection|JsonResource|ResourceCollection $data - */ + * Add data to response and transform according to its type. + * + * @param array|Arrayable|JsonResource|ResourceCollection $data + */ public function data($data): self { if (is_array($data)) { @@ -150,7 +150,7 @@ public function data($data): self } elseif ($data instanceof JsonResource) { $this->attributes['data'] = json_decode($data->toJson(), true); //. - } elseif ($data instanceof Collection) { + } elseif ($data instanceof Arrayable) { $this->attributes['data'] = $data->toArray(); //. } else {