From 88b79937f7464f595417f02aa0063f682e4dfcfd Mon Sep 17 00:00:00 2001 From: jiannei Date: Wed, 11 Oct 2023 02:22:58 +0800 Subject: [PATCH] pref: format config --- config/response.php | 49 ++++-------------------- src/Providers/LaravelServiceProvider.php | 7 ++-- src/Support/Format.php | 45 ++++++++-------------- tests/TestCase.php | 4 +- 4 files changed, 30 insertions(+), 75 deletions(-) diff --git a/config/response.php b/config/response.php index d2254f1..d255651 100644 --- a/config/response.php +++ b/config/response.php @@ -42,7 +42,7 @@ \Illuminate\Auth\AuthenticationException::class => [ ], - \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class =>[ + \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [ 'message' => '', ], \Illuminate\Database\Eloquent\ModelNotFoundException::class => [ @@ -50,52 +50,17 @@ ], ], - // Set the structure of the response data + // Any key that returns data exists supports custom aliases and display. 'format' => [ - \Jiannei\Response\Laravel\Support\Format::class, - [ + 'class' => \Jiannei\Response\Laravel\Support\Format::class, + 'config' => [ + // key => config 'status' => ['alias' => 'status', 'show' => true], 'code' => ['alias' => 'code', 'show' => true], 'message' => ['alias' => 'message', 'show' => true], 'error' => ['alias' => 'error', 'show' => true], - 'data' => [ - 'alias' => 'data', - 'show' => true, - - 'fields' => [ - // When data is nested with data, such as returning paged data, you can also set an alias for the inner data - 'data' => ['alias' => 'data', 'show' => true], // data/rows/list - - 'meta' => [ - 'alias' => 'meta', - 'show' => true, - - 'fields' => [ - 'pagination' => [ - 'alias' => 'pagination', - 'show' => true, - - 'fields' => [ - 'total' => ['alias' => 'total', 'show' => true], - 'count' => ['alias' => 'count', 'show' => true], - 'per_page' => ['alias' => 'per_page', 'show' => true], - 'current_page' => ['alias' => 'current_page', 'show' => true], - 'total_pages' => ['alias' => 'total_pages', 'show' => true], - 'links' => [ - 'alias' => 'links', - 'show' => true, - - 'fields' => [ - 'previous' => ['alias' => 'previous', 'show' => true], - 'next' => ['alias' => 'next', 'show' => true], - ], - ], - ], - ], - ], - ], - ], - ], + 'data' => ['alias' => 'data', 'show' => true,], + 'data.data' => ['alias' => 'data.data', 'show' => true,],// rows/items/list ], ], ]; diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index 82062f7..dc142b2 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -22,12 +22,11 @@ public function register() public function boot() { - $formatter = $this->app['config']->get('response.format.0', \Jiannei\Response\Laravel\Support\Format::class); - $config = $this->app['config']->get('response.format.1', []); + $formatter = $this->app['config']->get('response.format.class', \Jiannei\Response\Laravel\Support\Format::class); if (is_string($formatter) && class_exists($formatter)) { - $this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter, $config) { - return new $formatter($config); + $this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter) { + return new $formatter; }); } } diff --git a/src/Support/Format.php b/src/Support/Format.php index 99a8d93..c03f2b0 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -24,13 +24,6 @@ class Format implements \Jiannei\Response\Laravel\Contracts\Format { use Macroable; - protected $config; - - public function __construct($config = []) - { - $this->config = $config; - } - /** * Return a new JSON response from the application. * @@ -62,7 +55,7 @@ public function data(?array $data, ?string $message, int $code, $errors = null): 'message' => $this->formatMessage($code, $message), 'data' => $data ?: (object) $data, 'error' => $errors ?: (object) [], - ], $this->config); + ]); } /** @@ -135,7 +128,7 @@ public function jsonResource(JsonResource $resource, string $message = '', int $ */ protected function formatMessage(int $code, ?string $message): ?string { - if (! $message && class_exists($enumClass = Config::get('response.enum'))) { + if (!$message && class_exists($enumClass = Config::get('response.enum'))) { $message = $enumClass::fromValue($code)->description; } @@ -205,36 +198,32 @@ protected function formatPaginatedData(array $paginated): array /** * Format response data fields. * - * @param array $responseData - * @param array $dataFieldsConfig + * @param array $data * @return array */ - protected function formatDataFields(array $responseData, array $dataFieldsConfig = []): array + protected function formatDataFields(array $data): array { - if (empty($dataFieldsConfig)) { - return $responseData; - } + $formatConfig = \config('response.format.config', []); - foreach ($responseData as $field => $value) { - $fieldConfig = Arr::get($dataFieldsConfig, $field); - if (is_null($fieldConfig)) { + foreach ($formatConfig as $key => $config) { + if (!Arr::has($data, $key)) { continue; } - if ($value && is_array($value) && in_array($field, ['data', 'meta', 'pagination', 'links'])) { - $value = $this->formatDataFields($value, Arr::get($dataFieldsConfig, "{$field}.fields", [])); - } + $show = $config['show'] ?? true; + $alias = $config['alias'] ?? ''; - $alias = $fieldConfig['alias'] ?? $field; - $show = $fieldConfig['show'] ?? true; - $map = $fieldConfig['map'] ?? null; - unset($responseData[$field]); + if ($alias && $alias !== $key) { + Arr::set($data, $alias, Arr::get($data, $key)); + $data = Arr::except($data, $key); + $key = $alias; + } - if ($show) { - $responseData[$alias] = $map[$value] ?? $value; + if (!$show) { + $data = Arr::except($data, $key); } } - return $responseData; + return $data; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index ec31daa..1c43831 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -44,7 +44,9 @@ protected function defineEnvironment($app) $app['config']->set('response.enum', \Jiannei\Response\Laravel\Tests\Repositories\Enums\ResponseCodeEnum::class); if ($this instanceof FormatTest) { - $app['config']->set('response.format', [\Jiannei\Response\Laravel\Tests\Support\Format::class]); + $app['config']->set('response.format', [ + 'class' => \Jiannei\Response\Laravel\Tests\Support\Format::class + ]); } } }