Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change type data Collection to Arrayable #13

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
{
Expand Down Expand Up @@ -135,10 +135,10 @@ public static function validateOrFail(
}

/**
* Add data to response and transform according to its type.
*
* @param array|Collection|JsonResource|ResourceCollection $data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should keep the collection just because its more popular rather than Arrayable

@param array|Arrayable|Collection|JsonResource|ResourceCollection

*/
* 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)) {
Expand All @@ -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 {
Expand Down