From 24f6b7b4795d0f1ca7b05478375f25bf857c3ad4 Mon Sep 17 00:00:00 2001 From: jiannei Date: Mon, 6 Feb 2023 10:08:42 +0800 Subject: [PATCH] pref: response format --- src/Contracts/Format.php | 8 ----- src/Support/Format.php | 39 ++++++++++++++++-------- src/Support/Traits/JsonResponseTrait.php | 24 +++------------ 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/Contracts/Format.php b/src/Contracts/Format.php index b6e0e59..a64cbdc 100644 --- a/src/Contracts/Format.php +++ b/src/Contracts/Format.php @@ -28,14 +28,6 @@ interface Format */ public function data(?array $data, ?string $message, int $code, $errors = null): array; - /** - * Http status code. - * - * @param $code - * @return int - */ - public function statusCode($code): int; - /** * Format paginator data. * diff --git a/src/Support/Format.php b/src/Support/Format.php index f0d84ce..4303a0f 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -11,6 +11,7 @@ namespace Jiannei\Response\Laravel\Support; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Pagination\AbstractPaginator; @@ -29,6 +30,20 @@ public function __construct($config = []) $this->config = $config; } + /** + * Return a new JSON response from the application. + * + * @param mixed $data + * @param int $status + * @param array $headers + * @param int $options + * @return JsonResponse + */ + public function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse + { + return new JsonResponse($data, $this->formatStatusCode($status), $headers, $options); + } + /** * Format return data structure. * @@ -49,17 +64,6 @@ public function data(?array $data, ?string $message, int $code, $errors = null): ], $this->config); } - /** - * Http status code. - * - * @param $code - * @return int - */ - public function statusCode($code): int - { - return (int) substr($code, 0, 3); - } - /** * Format paginator data. * @@ -145,7 +149,7 @@ protected function formatMessage(int $code, ?string $message): ?string */ protected function formatStatus(int $code): string { - $statusCode = $this->statusCode($code); + $statusCode = $this->formatStatusCode($code); if ($statusCode >= 400 && $statusCode <= 499) {// client error $status = 'error'; } elseif ($statusCode >= 500 && $statusCode <= 599) {// service error @@ -157,6 +161,17 @@ protected function formatStatus(int $code): string return $status; } + /** + * Http status code. + * + * @param $code + * @return int + */ + protected function formatStatusCode($code): int + { + return (int) substr($code, 0, 3); + } + /** * Format paginated data. * diff --git a/src/Support/Traits/JsonResponseTrait.php b/src/Support/Traits/JsonResponseTrait.php index 04f431f..00c6c94 100644 --- a/src/Support/Traits/JsonResponseTrait.php +++ b/src/Support/Traits/JsonResponseTrait.php @@ -171,7 +171,7 @@ public function errorInternal(string $message = '') */ public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0) { - $response = $this->response( + $response = $this->formatter->response( $this->formatter->data(null, $message, $code, $errors), Config::get('response.error_code') ?: $code, $header, @@ -199,7 +199,7 @@ public function success($data = [], string $message = '', int $code = 200, array { if ($data instanceof ResourceCollection) { return tap( - $this->response($this->formatter->resourceCollection(...func_get_args()), $code, $headers, $option), + $this->formatter->response($this->formatter->resourceCollection(...func_get_args()), $code, $headers, $option), function ($response) use ($data) { $response->original = $data->resource->map( function ($item) { @@ -214,7 +214,7 @@ function ($item) { if ($data instanceof JsonResource) { return tap( - $this->response($this->formatter->jsonResource(...func_get_args()), $code, $headers, $option), + $this->formatter->response($this->formatter->jsonResource(...func_get_args()), $code, $headers, $option), function ($response) use ($data) { $response->original = $data->resource; @@ -224,27 +224,13 @@ function ($response) use ($data) { } if ($data instanceof AbstractPaginator) { - return $this->response($this->formatter->paginator(...func_get_args()), $code, $headers, $option); + return $this->formatter->response($this->formatter->paginator(...func_get_args()), $code, $headers, $option); } if ($data instanceof Arrayable) { $data = $data->toArray(); } - return $this->response($this->formatter->data(Arr::wrap($data), $message, $code), $code, $headers, $option); - } - - /** - * Return a new JSON response from the application. - * - * @param mixed $data - * @param int $status - * @param array $headers - * @param int $options - * @return JsonResponse - */ - protected function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse - { - return new JsonResponse($data, $this->formatter->statusCode($status), $headers, $options); + return $this->formatter->response($this->formatter->data(Arr::wrap($data), $message, $code), $code, $headers, $option); } }