31 lines
806 B
PHP
31 lines
806 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Responses;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class ApiResponse
|
|
{
|
|
private static array $headers = [
|
|
'Content-Type' => 'application/vnd.api+json',
|
|
];
|
|
|
|
public static function handle(null|array|JsonResource $data = null, null|int $status = null, array $headers = []): JsonResponse
|
|
{
|
|
$response = ['message' => 'ok'];
|
|
|
|
if (is_array($data) || $data instanceof JsonResource) {
|
|
$response = ['data' => $data];
|
|
}
|
|
|
|
return new JsonResponse(
|
|
data: $response,
|
|
status: $status ?? Response::HTTP_OK,
|
|
headers: array_merge(static::$headers, $headers),
|
|
);
|
|
}
|
|
}
|