|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) 2021-2024 guanguans<ityaozm@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @see https://github.com/guanguans/laravel-api-response |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Guanguans\LaravelApiResponse; |
| 15 | + |
| 16 | +use Guanguans\LaravelApiResponse\Concerns\ConcreteHttpStatusMethods; |
| 17 | +use Guanguans\LaravelApiResponse\Concerns\HasExceptionMap; |
| 18 | +use Guanguans\LaravelApiResponse\Concerns\HasPipes; |
| 19 | +use Illuminate\Contracts\Debug\ExceptionHandler; |
| 20 | +use Illuminate\Http\JsonResponse; |
| 21 | +use Illuminate\Pipeline\Pipeline; |
| 22 | +use Illuminate\Support\Collection; |
| 23 | +use Illuminate\Support\Traits\Conditionable; |
| 24 | +use Illuminate\Support\Traits\Dumpable; |
| 25 | +use Illuminate\Support\Traits\Macroable; |
| 26 | +use Illuminate\Support\Traits\Tappable; |
| 27 | +use Illuminate\Validation\ValidationException; |
| 28 | +use Symfony\Component\HttpFoundation\Response; |
| 29 | +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
| 30 | + |
| 31 | +/** |
| 32 | + * @see https://github.com/dingo/api |
| 33 | + * @see https://github.com/f9webltd/laravel-api-response-helpers |
| 34 | + * @see https://github.com/flugg/laravel-responder |
| 35 | + * @see https://github.com/jiannei/laravel-response |
| 36 | + * @see https://github.com/MarcinOrlowski/laravel-api-response-builder |
| 37 | + * |
| 38 | + * @method array convertExceptionToArray(\Throwable $throwable) |
| 39 | + */ |
| 40 | +class ApiResponse implements Contracts\ApiResponse |
| 41 | +{ |
| 42 | + // use Dumpable; |
| 43 | + use ConcreteHttpStatusMethods; |
| 44 | + use Conditionable; |
| 45 | + use HasExceptionMap; |
| 46 | + use HasPipes; |
| 47 | + use Macroable; |
| 48 | + use Tappable; |
| 49 | + |
| 50 | + public function __construct(?Collection $pipes = null, ?Collection $exceptionMap = null) |
| 51 | + { |
| 52 | + $this->pipes = collect($pipes); |
| 53 | + $this->exceptionMap = collect($exceptionMap); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param mixed $data |
| 58 | + */ |
| 59 | + public function success($data = null, string $message = '', int $code = Response::HTTP_OK): JsonResponse |
| 60 | + { |
| 61 | + return $this->json(true, $code, $message, $data); |
| 62 | + } |
| 63 | + |
| 64 | + public function error(string $message = '', int $code = Response::HTTP_BAD_REQUEST, ?array $error = null): JsonResponse |
| 65 | + { |
| 66 | + return $this->json(false, $code, $message, null, $error); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @see \Illuminate\Foundation\Exceptions\Handler::render() |
| 71 | + * @see \Illuminate\Foundation\Exceptions\Handler::prepareException() |
| 72 | + * @see \Illuminate\Foundation\Exceptions\Handler::convertExceptionToArray() |
| 73 | + * @see \Illuminate\Database\QueryException |
| 74 | + */ |
| 75 | + public function throw(\Throwable $throwable): JsonResponse |
| 76 | + { |
| 77 | + $newThrowable = $this->mapException($throwable); |
| 78 | + $newThrowable instanceof \Throwable and $throwable = $newThrowable; |
| 79 | + |
| 80 | + /** @noinspection PhpCastIsUnnecessaryInspection */ |
| 81 | + $code = (int) $throwable->getCode() ?: Response::HTTP_INTERNAL_SERVER_ERROR; |
| 82 | + $message = app()->hasDebugModeEnabled() ? $throwable->getMessage() : ''; |
| 83 | + $error = (fn (): array => $this->convertExceptionToArray($throwable))->call(app(ExceptionHandler::class)); |
| 84 | + $headers = []; |
| 85 | + |
| 86 | + if ($throwable instanceof HttpExceptionInterface) { |
| 87 | + $message = $throwable->getMessage(); |
| 88 | + $code = $throwable->getStatusCode(); |
| 89 | + $headers = $throwable->getHeaders(); |
| 90 | + } |
| 91 | + |
| 92 | + if ($throwable instanceof ValidationException) { |
| 93 | + $message = $throwable->getMessage(); |
| 94 | + $code = $throwable->status; |
| 95 | + $error = $throwable->errors(); |
| 96 | + } |
| 97 | + |
| 98 | + if (\is_array($newThrowable) && $newThrowable) { |
| 99 | + $message = $newThrowable['message'] ?? null ?: $message; |
| 100 | + $code = $newThrowable['code'] ?? null ?: $code; |
| 101 | + $error = $newThrowable['error'] ?? null ?: $error; |
| 102 | + $headers = $newThrowable['headers'] ?? null ?: $headers; |
| 103 | + } |
| 104 | + |
| 105 | + return $this->error($message, $code, $error)->withHeaders($headers); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param int<100, 599>|int<10000, 59999> $code |
| 110 | + * @param mixed $data |
| 111 | + * @param null|array<string, mixed> $error |
| 112 | + */ |
| 113 | + public function json(bool $status, int $code, string $message = '', $data = null, ?array $error = null): JsonResponse |
| 114 | + { |
| 115 | + return (new Pipeline(app())) |
| 116 | + ->send(['status' => $status, 'code' => $code, 'message' => $message, 'data' => $data, 'error' => $error]) |
| 117 | + ->through($this->pipes()) |
| 118 | + ->then(static fn (array $data): JsonResponse => new JsonResponse( |
| 119 | + $data, |
| 120 | + 200, |
| 121 | + [], |
| 122 | + \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_LINE_TERMINATORS |
| 123 | + | \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT |
| 124 | + )); |
| 125 | + } |
| 126 | +} |
0 commit comments