43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
final class ApiResponse
|
|
{
|
|
public static function success(mixed $data = null, string $message = 'ok', int $code = 0): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'code' => $code,
|
|
'message' => $message,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public static function error(string $message, int $code = 1, int $status = 400, mixed $data = null): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'code' => $code,
|
|
'message' => $message,
|
|
'data' => $data,
|
|
], $status);
|
|
}
|
|
|
|
public static function page(LengthAwarePaginator $paginator, string $message = 'ok'): JsonResponse
|
|
{
|
|
return self::success([
|
|
'items' => $paginator->items(),
|
|
'meta' => [
|
|
'current_page' => $paginator->currentPage(),
|
|
'per_page' => $paginator->perPage(),
|
|
'total' => $paginator->total(),
|
|
'last_page' => $paginator->lastPage(),
|
|
],
|
|
], $message);
|
|
}
|
|
}
|