printer-manager-admin/app/Http/Responses/ApiResponse.php
2023-11-01 22:27:44 +01:00

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),
);
}
}