From 69f6374842a99588a2813b3a155e6da9f6ba8d21 Mon Sep 17 00:00:00 2001 From: Aditya Darma Date: Thu, 25 Apr 2024 16:00:13 +0800 Subject: [PATCH] feat: add function json from resource --- src/Services/BaseService.php | 87 +++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/src/Services/BaseService.php b/src/Services/BaseService.php index c9f6ce5..c71c459 100644 --- a/src/Services/BaseService.php +++ b/src/Services/BaseService.php @@ -131,6 +131,17 @@ public function toJson(): JsonResponse ]), $this->code); } + /** + * Convert resource to json response + * + * @param $resource + * @return static + */ + public function toJsonFromResource($resource) + { + return $this->resource($resource)->toJson(); + } + /** * Convert to json data with resource * @@ -139,43 +150,45 @@ public function toJson(): JsonResponse */ public function resource($resource): static { - // Simple Paginate - if ($this->data instanceof Paginator) { - $this->meta = [ - 'per_page' => $this->data->perPage(), - 'current_page' => $this->data->currentPage(), - ]; - $this->data = $resource::collection($this->data->items()); - } - // Paginate - elseif ($this->data instanceof LengthAwarePaginator) { - $this->meta = [ - 'total' => $this->data->total(), - 'count' => $this->data->count(), - 'per_page' => $this->data->perPage(), - 'current_page' => $this->data->currentPage(), - 'total_pages' => $this->data->lastPage() - ]; - $this->data = $resource::collection($this->data->items()); - } - // Cursor Paginate - elseif ($this->data instanceof CursorPaginator) { - $this->meta = [ - 'per_page' => $this->data->perPage(), - ]; - $this->link = [ - 'prev_page_url' => $this->data->previousPageUrl(), - 'next_page_url' => $this->data->nextPageUrl(), - ]; - $this->data = $resource::collection($this->data->items()); - } - // Data Collection - elseif($this->data instanceof Collection) { - $this->data = $resource::collection($this->data); - } - // Data Model - elseif ($this->data instanceof Model) { - $this->data = new $resource($this->data); + if ($this->data) { + // Simple Paginate + if ($this->data instanceof Paginator) { + $this->meta = [ + 'per_page' => $this->data->perPage(), + 'current_page' => $this->data->currentPage(), + ]; + $this->data = $resource::collection($this->data->items()); + } + // Paginate + elseif ($this->data instanceof LengthAwarePaginator) { + $this->meta = [ + 'total' => $this->data->total(), + 'count' => $this->data->count(), + 'per_page' => $this->data->perPage(), + 'current_page' => $this->data->currentPage(), + 'total_pages' => $this->data->lastPage() + ]; + $this->data = $resource::collection($this->data->items()); + } + // Cursor Paginate + elseif ($this->data instanceof CursorPaginator) { + $this->meta = [ + 'per_page' => $this->data->perPage(), + ]; + $this->link = [ + 'prev_page_url' => $this->data->previousPageUrl(), + 'next_page_url' => $this->data->nextPageUrl(), + ]; + $this->data = $resource::collection($this->data->items()); + } + // Data Collection + elseif($this->data instanceof Collection) { + $this->data = $resource::collection($this->data); + } + // Data Model + elseif ($this->data instanceof Model) { + $this->data = new $resource($this->data); + } } return $this;