From 7bdfaf0c2afc9d25c6f1ffa9f4f989391c1f8fad Mon Sep 17 00:00:00 2001 From: jiannei Date: Wed, 11 Oct 2023 16:28:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20Fractal=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84=20Format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Support/Facades/Format.php | 8 ++--- src/Support/Format.php | 40 +++++++++++++++--------- src/Support/Traits/JsonResponseTrait.php | 38 ++++++++++------------ tests/Support/Format.php | 2 +- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/Support/Facades/Format.php b/src/Support/Facades/Format.php index 1490a67..9436284 100644 --- a/src/Support/Facades/Format.php +++ b/src/Support/Facades/Format.php @@ -10,11 +10,11 @@ use Illuminate\Support\Facades\Facade as IlluminateFacade; /** - * @method static JsonResponse response($data = [], int $status = 200, array $headers = [], int $options = 0) + * @method static JsonResponse response($data = null, string $message = '', int $code = 200, $errors = null, array $headers = [], int $option = 0, string $from = 'success') * @method static array data($data, ?string $message, int $code, $errors = null) - * @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource) - * @method static array resourceCollection(ResourceCollection $collection) - * @method static array jsonResource(JsonResource $resource) + * @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource, $transformer = null, $resourceName = null) + * @method static array resourceCollection(ResourceCollection $collection, $transformer = null, $resourceName = null) + * @method static array jsonResource(JsonResource $resource, $transformer = null, $resourceName = null) * * @see \Jiannei\Response\Laravel\Support\Format */ diff --git a/src/Support/Format.php b/src/Support/Format.php index 1fdc0d6..b3b1f18 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -37,14 +37,17 @@ class Format * Return a new JSON response from the application. * * @param mixed $data - * @param int $status + * @param string $message + * @param int $code + * @param null $errors * @param array $headers - * @param int $options + * @param int $option + * @param string $from * @return JsonResponse */ - public function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse + public function response($data = null, string $message = '', int $code = 200, $errors = null, array $headers = [], int $option = 0, string $from = 'success'): JsonResponse { - return new JsonResponse($data, $this->formatStatusCode($status), $headers, $options); + return new JsonResponse($this->data($data, $message, $code, $errors), $this->formatStatusCode($code, $from), $headers, $option); } /** @@ -79,13 +82,15 @@ public function data($data, ?string $message, int $code, $errors = null): array * Format paginator data. * * @param AbstractPaginator|AbstractCursorPaginator $resource + * @param null $transformer + * @param null $resourceName * @return array */ - public function paginator(AbstractPaginator|AbstractCursorPaginator $resource): array + public function paginator(AbstractPaginator|AbstractCursorPaginator $resource, $transformer = null, $resourceName = null): array { - $fractal = fractal()->collection($resource, function ($item) { + $fractal = fractal()->collection($resource, $transformer ?: function ($item) { return $item->toArray(); - })->serializeWith(DataArraySerializer::class); + }, $resourceName)->serializeWith(DataArraySerializer::class); return tap($fractal, $this->formatCollection($resource))->toArray(); } @@ -94,13 +99,15 @@ public function paginator(AbstractPaginator|AbstractCursorPaginator $resource): * Format collection resource data. * * @param ResourceCollection $collection + * @param null $transformer + * @param null $resourceName * @return array */ - public function resourceCollection(ResourceCollection $collection): array + public function resourceCollection(ResourceCollection $collection, $transformer = null, $resourceName = null): array { - $fractal = fractal()->collection($collection->resource, function (JsonResource $resource) { + $fractal = fractal()->collection($collection->resource, $transformer ?: function (JsonResource $resource) { return array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional); - })->serializeWith(DataArraySerializer::class); + }, $resourceName)->serializeWith(DataArraySerializer::class); return tap($fractal, $this->formatCollection($collection->resource))->toArray(); } @@ -109,13 +116,15 @@ public function resourceCollection(ResourceCollection $collection): array * Format JsonResource Data. * * @param JsonResource $resource + * @param null $transformer + * @param null $resourceName * @return array */ - public function jsonResource(JsonResource $resource): array + public function jsonResource(JsonResource $resource, $transformer = null, $resourceName = null): array { $data = array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional); - return fractal()->item($data, fn() => $data)->serializeWith(ArraySerializer::class)->toArray(); + return fractal()->item($data, $transformer ?: fn() => $data, $resourceName)->serializeWith(ArraySerializer::class)->toArray(); } /** @@ -123,7 +132,7 @@ public function jsonResource(JsonResource $resource): array * * @param int $code * @param string|null $message - * @return string + * @return string|null */ protected function formatMessage(int $code, ?string $message): ?string { @@ -158,11 +167,12 @@ protected function formatStatus(int $code): string * Http status code. * * @param $code + * @param string $from * @return int */ - protected function formatStatusCode($code): int + protected function formatStatusCode($code, string $from = 'success'): int { - return (int) substr($code, 0, 3); + return (int) substr($from === 'fail' ? (Config::get('response.error_code') ?: $code) : $code, 0, 3); } protected function formatCollection($collection): \Closure diff --git a/src/Support/Traits/JsonResponseTrait.php b/src/Support/Traits/JsonResponseTrait.php index b375952..ab0efb2 100644 --- a/src/Support/Traits/JsonResponseTrait.php +++ b/src/Support/Traits/JsonResponseTrait.php @@ -32,7 +32,7 @@ trait JsonResponseTrait * @param string $location * @return JsonResponse */ - public function accepted($data = [], string $message = '', string $location = '') + public function accepted($data = [], string $message = '', string $location = ''): JsonResponse { $response = $this->success($data, $message, 202); if ($location) { @@ -50,7 +50,7 @@ public function accepted($data = [], string $message = '', string $location = '' * @param string $location * @return JsonResponse */ - public function created($data = [], string $message = '', string $location = '') + public function created($data = [], string $message = '', string $location = ''): JsonResponse { $response = $this->success($data, $message, 201); if ($location) { @@ -66,7 +66,7 @@ public function created($data = [], string $message = '', string $location = '') * @param string $message * @return JsonResponse */ - public function noContent(string $message = '') + public function noContent(string $message = ''): JsonResponse { return $this->success([], $message, 204); } @@ -104,7 +104,7 @@ public function localize(int $code = 200, array $headers = [], int $option = 0) * * @param string|null $message */ - public function errorBadRequest(string $message = '') + public function errorBadRequest(string $message = ''): void { $this->fail($message, 400); } @@ -114,7 +114,7 @@ public function errorBadRequest(string $message = '') * * @param string $message */ - public function errorUnauthorized(string $message = '') + public function errorUnauthorized(string $message = ''): void { $this->fail($message, 401); } @@ -124,7 +124,7 @@ public function errorUnauthorized(string $message = '') * * @param string $message */ - public function errorForbidden(string $message = '') + public function errorForbidden(string $message = ''): void { $this->fail($message, 403); } @@ -134,7 +134,7 @@ public function errorForbidden(string $message = '') * * @param string $message */ - public function errorNotFound(string $message = '') + public function errorNotFound(string $message = ''): void { $this->fail($message, 404); } @@ -144,7 +144,7 @@ public function errorNotFound(string $message = '') * * @param string $message */ - public function errorMethodNotAllowed(string $message = '') + public function errorMethodNotAllowed(string $message = ''): void { $this->fail($message, 405); } @@ -154,7 +154,7 @@ public function errorMethodNotAllowed(string $message = '') * * @param string $message */ - public function errorInternal(string $message = '') + public function errorInternal(string $message = ''): void { $this->fail($message); } @@ -164,21 +164,15 @@ public function errorInternal(string $message = '') * * @param string $message * @param int $code - * @param array|null $errors - * @param array $header - * @param int $options + * @param null $errors + * @param array $headers + * @param int $option * @return JsonResponse * - * @throws HttpResponseException */ - public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0) + public function fail(string $message = '', int $code = 500, $errors = null, array $headers = [], int $option = 0): JsonResponse { - $response = Format::response( - Format::data(null, $message, $code, $errors), - Config::get('response.error_code') ?: $code, - $header, - $options - ); + $response = Format::response(null, $message, $code, $errors, $headers, $option, 'fail'); if (is_null($errors)) { $response->throwResponse(); @@ -190,7 +184,7 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra /** * Return a success response. * - * @param JsonResource|array|mixed $data + * @param mixed $data * @param string $message * @param int $code * @param array $headers @@ -199,6 +193,6 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra */ public function success($data = [], string $message = '', int $code = 200, array $headers = [], int $option = 0) { - return Format::response(Format::data($data,$message,$code), $code, $headers, $option); + return Format::response(...func_get_args()); } } diff --git a/tests/Support/Format.php b/tests/Support/Format.php index 4422ee3..eb14763 100644 --- a/tests/Support/Format.php +++ b/tests/Support/Format.php @@ -13,7 +13,7 @@ class Format extends \Jiannei\Response\Laravel\Support\Format { - public function data($data, ?string $message, int $code, $errors = null): array + public function data(mixed $data, ?string $message, int $code, $errors = null): array { return [ 'status' => $this->formatStatus($code),