Skip to content

Commit

Permalink
feat: add function json from resource
Browse files Browse the repository at this point in the history
  • Loading branch information
adityadarma committed Apr 25, 2024
1 parent 8f1947f commit 69f6374
Showing 1 changed file with 50 additions and 37 deletions.
87 changes: 50 additions & 37 deletions src/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down

0 comments on commit 69f6374

Please sign in to comment.