Mahdee Rajon  subception

File "MessageResponse.php"

Full Path: /var/www/html/back/app/Responses/MessageResponse.php
File size: 1.27 KB
MIME-type: text/x-php
Charset: utf-8

<?php

declare(strict_types=1);

namespace App\Responses;

use App\BaseClasses\BaseResponse;
use Carbon\Carbon;
use Illuminate\Http\{JsonResponse};
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;

class MessageResponse extends BaseResponse
{
    public function __construct(
        private string $message,
        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' => [
                    'message' => __(key: $this->message)
                ],
                'metadata' => [
                    'request_id' => $requestId,
                    'timestamp' => $timestamp
                ],
            ],
            status: $this->status
        );
    }

    public static function example(): array
    {
        return [
           'status' => 200,
            'data' => [
               'message' => 'ok'
            ],
            'metadata' => [
                'request_id' => Str::uuid()->toString(),
                'timestamp' => Carbon::now()->toString()
            ],
        ];
    }


}