From f60d5b6b1009ef615fcc0e273e3c6e665078527f Mon Sep 17 00:00:00 2001 From: jiannei Date: Thu, 29 Dec 2022 15:50:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=AD=97=E6=AE=B5=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Contracts/Format.php | 65 ++++++++++++++++++++++++ src/Providers/LaravelServiceProvider.php | 9 ++++ src/Support/Facades/Format.php | 6 +-- src/Support/Format.php | 27 +++++----- tests/FormatTest.php | 18 +++++++ tests/Support/Format.php | 26 ++++++++++ tests/TestCase.php | 1 + 7 files changed, 136 insertions(+), 16 deletions(-) create mode 100644 src/Contracts/Format.php create mode 100644 tests/FormatTest.php create mode 100644 tests/Support/Format.php diff --git a/src/Contracts/Format.php b/src/Contracts/Format.php new file mode 100644 index 0000000..19015f6 --- /dev/null +++ b/src/Contracts/Format.php @@ -0,0 +1,65 @@ +setupConfig(); } + public function boot() + { + $format = $this->app['config']['response']['format']; + + if (is_string($format) && class_exists($format)) { + $this->app->bind(\Jiannei\Response\Laravel\Support\Format::class,$format); + } + } + protected function setupConfig() { $path = dirname(__DIR__, 2).'/config/response.php'; diff --git a/src/Support/Facades/Format.php b/src/Support/Facades/Format.php index ec5bf8c..e153b7d 100644 --- a/src/Support/Facades/Format.php +++ b/src/Support/Facades/Format.php @@ -16,9 +16,9 @@ /** * @method static int statusCode(int $code) * @method static array data($data, $message, $code, $errors = null) - * @method static mixed resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0) - * @method static mixed jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0) - * @method static mixed paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0) + * @method static array resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0) + * @method static array jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0) + * @method static array paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0) * * @see \Jiannei\Response\Laravel\Support\Format */ diff --git a/src/Support/Format.php b/src/Support/Format.php index 9782330..a10a041 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -12,25 +12,26 @@ namespace Jiannei\Response\Laravel\Support; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Pagination\AbstractPaginator; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Config; use Illuminate\Support\Traits\Macroable; -class Format +class Format implements \Jiannei\Response\Laravel\Contracts\Format { use Macroable; /** * Format return data structure. * - * @param JsonResource|array|null $data - * @param $message - * @param $code + * @param array|null $data + * @param string|null $message + * @param int $code * @param null $errors * @return array */ - public function data($data, $message, $code, $errors = null): array + public function data(?array $data, ?string $message, int $code, $errors = null): array { if (! $message && class_exists($enumClass = Config::get('response.enum'))) { $message = $enumClass::fromValue($code)->description; @@ -64,9 +65,9 @@ public function statusCode($code): int * @param int $code * @param array $headers * @param int $option - * @return mixed + * @return array */ - public function paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0) + public function paginator(AbstractPaginator $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array { $paginated = $resource->toArray(); @@ -80,14 +81,14 @@ public function paginator($resource, string $message = '', $code = 200, array $h /** * Format collection resource data. * - * @param JsonResource $resource + * @param ResourceCollection $resource * @param string $message * @param int $code * @param array $headers * @param int $option - * @return mixed + * @return array */ - public function resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0) + public function resourceCollection(ResourceCollection $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array { $data = array_merge_recursive(['data' => $resource->resolve(request())], $resource->with(request()), $resource->additional); if ($resource->resource instanceof AbstractPaginator) { @@ -108,9 +109,9 @@ public function resourceCollection($resource, string $message = '', int $code = * @param int $code * @param array $headers * @param int $option - * @return mixed + * @return array */ - public function jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0) + public function jsonResource(JsonResource $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array { $resourceData = array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional); @@ -143,7 +144,7 @@ protected function formatStatus(int $code): string * @param array $paginated * @return array */ - protected function formatPaginatedData(array $paginated) + protected function formatPaginatedData(array $paginated): array { return [ 'meta' => [ diff --git a/tests/FormatTest.php b/tests/FormatTest.php new file mode 100644 index 0000000..e07eef1 --- /dev/null +++ b/tests/FormatTest.php @@ -0,0 +1,18 @@ +assertEquals(200, $response->status()); + + $this->assertArrayHasKey('extra',$response->getData(true)); + $this->assertArrayHasKey('time',$response->getData(true)['extra']); + } +} \ No newline at end of file diff --git a/tests/Support/Format.php b/tests/Support/Format.php new file mode 100644 index 0000000..a91488c --- /dev/null +++ b/tests/Support/Format.php @@ -0,0 +1,26 @@ +description; + } + + return $this->formatDataFields([ + 'status' => $this->formatStatus($code), + 'code' => $code, + 'message' => $message, + 'data' => $data ?: (object) $data, + 'error' => $errors ?: (object) [], + 'extra' => [ + 'time' => time() + ], + ], Config::get('response.format.fields', [])); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index cc41e62..def7d77 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -43,5 +43,6 @@ protected function defineEnvironment($app) ]); $app['config']->set('response.enum', \Jiannei\Response\Laravel\Tests\Repositories\Enums\ResponseCodeEnum::class); + $app['config']->set('response.format',\Jiannei\Response\Laravel\Tests\Support\Format::class); } }