/var/www/html/back/app/Responses/ResourceResponse.php
<?php
declare(strict_types=1);
namespace App\Responses;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Context;
final readonly class ResourceResponse implements Responsable
{
public function __construct(
private mixed $data,
private int $status
) {
}
public function toResponse($request): JsonResponse
{
$requestId = Context::get(key: 'request_id');
$timestamp = Context::get(key: 'timestamp');
return new JsonResponse(
data: [
'status' => $this->status,
'data' => $this->data,
'metadata' => [
'request_id' => $requestId,
'timestamp' => $timestamp
],
],
status: $this->status
);
}
public static function example(): array
{
return [
'data' => [
'status' => 1,
'data' => [],
'metadata' => [
'request_id' => '',
'timestamp' => ''
],
],
'status' => 1
];
}
}