/var/www/html/back/app/Services/ResponseService.php
<?php
declare(strict_types=1);
namespace App\Services;
use App\Responses\ResponseDto;
use Illuminate\Http\JsonResponse;
class ResponseService
{
public static function json($result = null, $status = 200): JsonResponse
{
try {
return response()->json(self::wrap($result), $status);
} catch (\Throwable $exception) {
dd($exception);
}
}
public static function array($result = null, bool $paginatable = false): array
{
try {
return self::wrap($result, $paginatable)->toArray();
} catch (\Throwable $exception) {
dd($exception);
}
}
public static function wrap(array $result = null, ?bool $paginatable = false): ResponseDto
{
return ResponseDto::from([...$result, 'paginatable' => $paginatable]);
}
}