Skip to content

Commit

Permalink
Update ApiResponse.php updated formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianzofcin committed Jan 17, 2023
1 parent 0193a8f commit 908275f
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public static function data(mixed $data): static
* @param $resource
* @return static
*/
public static function collection(mixed $data, $resource = null): static
public static function collection(mixed $pagination, $resource = null): static
{
if ($resource) static::$data = ($resource::collection($data))->toResponse(app('request'))->getData();
else static::$data = $data;
if ($resource) static::$data = (array)($resource::collection($pagination))->toResponse(app('request'))->getData();
else static::$data = $pagination->toArray();

return new static;
}
Expand All @@ -81,11 +81,27 @@ public static function message(string $message): static
*/
public static function response(int $statusCode = 200): \Illuminate\Http\JsonResponse
{
$response = collect( self::$data);
$response = $response->merge([
'code' => self::$code,
'message' => self::$message ?? __('api.' . self::$code),
]);
if (gettype(self::$data) === 'array') {
if (key_exists('data', self::$data)) {
$response = collect(self::$data);
$response = $response->merge([
'code' => self::$code,
'message' => self::$message ?? __('api.' . self::$code),
]);
} else {
$response = [
'data' => self::$data,
'code' => self::$code,
'message' => self::$message ?? __('api.' . self::$code),
];
}
} else {
$response = [
'data' => self::$data,
'code' => self::$code,
'message' => self::$message ?? __('api.' . self::$code),
];
}

return response()->json($response)->setStatusCode($statusCode);
}
Expand Down

0 comments on commit 908275f

Please sign in to comment.