update project
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Api;
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
|
||||
final class ApiExceptionRenderer
|
||||
{
|
||||
public function render(Throwable $e): JsonResponse
|
||||
{
|
||||
if ($e instanceof ValidationException) {
|
||||
return ApiResponse::error('Validation failed.', 422, $e->errors());
|
||||
}
|
||||
|
||||
if ($e instanceof AuthenticationException) {
|
||||
return ApiResponse::error('Unauthenticated.', 401);
|
||||
}
|
||||
|
||||
if ($e instanceof AuthorizationException) {
|
||||
return ApiResponse::error('Forbidden.', 403);
|
||||
}
|
||||
|
||||
if ($e instanceof ModelNotFoundException || $e instanceof NotFoundHttpException || str_contains($e::class, 'NotFound')) {
|
||||
return ApiResponse::error('Resource not found.', 404);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Conflict') || str_contains($e::class, 'Duplicate')) {
|
||||
return ApiResponse::error('Conflict.', 409);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Unauthorized')) {
|
||||
return ApiResponse::error('Forbidden.', 403);
|
||||
}
|
||||
|
||||
if (str_contains($e::class, 'Exception')) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
return ApiResponse::error('Server error.', 500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Api;
|
||||
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class ApiResponse
|
||||
{
|
||||
public static function success(mixed $data = null, ?string $message = null, array $meta = [], int $status = 200): JsonResponse
|
||||
{
|
||||
if ($data instanceof JsonResource) {
|
||||
$data = $data->resolve(request());
|
||||
}
|
||||
|
||||
if ($data instanceof LengthAwarePaginator) {
|
||||
$meta = array_merge($meta, [
|
||||
'pagination' => [
|
||||
'current_page' => $data->currentPage(),
|
||||
'per_page' => $data->perPage(),
|
||||
'total' => $data->total(),
|
||||
'last_page' => $data->lastPage(),
|
||||
],
|
||||
]);
|
||||
$data = $data->items();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $data,
|
||||
'message' => $message,
|
||||
'meta' => $meta,
|
||||
'errors' => null,
|
||||
], $status);
|
||||
}
|
||||
|
||||
public static function error(string $message, int $status, array $errors = [], array $meta = []): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'data' => null,
|
||||
'message' => $message,
|
||||
'meta' => $meta,
|
||||
'errors' => $errors ?: null,
|
||||
], $status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Api;
|
||||
|
||||
enum ControllerClassification: string
|
||||
{
|
||||
case CANONICAL = 'canonical';
|
||||
case ADAPTER = 'adapter';
|
||||
case DEPRECATED = 'deprecated';
|
||||
case REMOVE = 'remove';
|
||||
case EXTENSION = 'extension';
|
||||
case UNKNOWN = 'unknown';
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Support\Api;
|
||||
|
||||
enum RouteRisk: string
|
||||
{
|
||||
case LOW = 'low';
|
||||
case MEDIUM = 'medium';
|
||||
case HIGH = 'high';
|
||||
case CRITICAL = 'critical';
|
||||
}
|
||||
Reference in New Issue
Block a user