forge/app/Traits/ApiResponses.php
2025-01-30 20:49:56 -05:00

47 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Traits;
use Illuminate\Http\JsonResponse;
trait ApiResponses
{
/**
* Return a success JSON response.
*
* @param array<string, string> $data
*/
protected function success(string $message, ?array $data = []): JsonResponse
{
return $this->baseResponse(message: $message, data: $data, code: 200);
}
/**
* The base response.
*
* @param array<string, string> $data
*/
private function baseResponse(?string $message = '', ?array $data = [], ?int $code = 200): JsonResponse
{
$response = [];
$response['message'] = $message;
if ($data) {
$response['data'] = $data;
}
$response['status'] = $code;
return response()->json($response, $code);
}
/**
* Return an error JSON response.
*/
protected function error(string $message, int $code): JsonResponse
{
return $this->baseResponse(message: $message, code: $code);
}
}