39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Responses;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* @OA\Schema(
|
|
* schema="ApiResponse",
|
|
* type="object",
|
|
* @OA\Property(property="code", type="string", example="OK"),
|
|
* @OA\Property(property="message", type="string", example="Success"),
|
|
* @OA\Property(property="data", type="object", nullable=true)
|
|
* )
|
|
*/
|
|
|
|
class ApiResponse
|
|
{
|
|
public static function success($data = null, string $message = 'Success', string $code = 'OK', int $status = Response::HTTP_OK): JsonResponse
|
|
{
|
|
return self::respond($code, $message, $data, $status);
|
|
}
|
|
|
|
public static function error(string $message = 'Error', string $code = 'ERROR', int $status = Response::HTTP_BAD_REQUEST): JsonResponse
|
|
{
|
|
return self::respond($code, $message, null, $status);
|
|
}
|
|
|
|
public static function unauthorized(string $message = 'Unauthorized'): JsonResponse
|
|
{
|
|
return self::error($message, 'UNAUTHORIZED', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
private static function respond(string $code, string $message, $data, int $status): JsonResponse
|
|
{
|
|
return response()->json(['code' => $code,'message' => $message,'data' => $data,], $status);
|
|
}
|
|
} |