• File: TokenResponse.php
  • Full Path: /var/www/html/back/app/Responses/TokenResponse.php
  • File size: 1.6 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

declare(strict_types=1);

namespace App\Responses;

use App\BaseClasses\BaseResponse;
use Illuminate\Http\{JsonResponse};

class TokenResponse extends BaseResponse
{
    private int $status;
    private string $message;
    private string $token;
    private int $userId;

    public function __construct(
        int $status,
        string $message,
        string $token,
        int $userId
    ) {
        $this->status = $status;
        $this->message = $message;
        $this->token = $token;
        $this->userId = $userId;
    }

    public function toResponse($request): JsonResponse
    {
        return new JsonResponse(
            data: [
                'status' => $this->status,
                'data' => [
                    'message' => __(key: $this->message),
                    'token' => __(key: $this->token),
                    'userId' => __(key: $this->userId),
                ],
                'metadata' => [
                    'request_id' => str()->uuid()->toString(),
                    'timestamp' => now()->toIso8601String()
                ],
            ],
            status: $this->status
        );
    }

    public static function example(): array
    {
        return [
            'data' => [
                'message' => 'Token generated successfully',
                'token' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpvaG4iOiIxMjM0NTQ4ODM3ODA2IiwidmFsdWUifQ...',
                'userId' => 123,
            ],
            'metadata' => [
                'request_id' => str()->uuid()->toString(),
                'timestamp' => now()->toIso8601String()
            ]
        ];
    }
}