diff --git a/src/Contract/ResponseFormat.php b/src/Contract/ResponseFormat.php index ff34fad..2311d50 100644 --- a/src/Contract/ResponseFormat.php +++ b/src/Contract/ResponseFormat.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Jiannei\Response\Laravel\Contract; use Illuminate\Contracts\Pagination\Paginator; @@ -23,20 +32,17 @@ public function response(): JsonResponse; */ public function get(): ?array; - /** - * * Format data structures. * * @param mixed|null $data * @param string $message * @param int|\BackedEnum $code - * @param $error + * @param $error * @return $this */ public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static; - /** * Format paginator data. * @@ -60,4 +66,4 @@ public function resourceCollection(ResourceCollection $collection): array; * @return array */ public function jsonResource(JsonResource $resource): array; -} \ No newline at end of file +} diff --git a/src/Support/Format.php b/src/Support/Format.php index 1d65e72..2beae21 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -33,7 +33,6 @@ class Format implements ResponseFormat protected ?array $data = null; protected int $statusCode = 200; - public function __construct(protected array $config = []) { } @@ -154,7 +153,7 @@ protected function formatMessage(int $code, string $message = ''): ?string $localizationKey = join('.', [Config::get('response.locale', 'enums'), $code]); return match (true) { - !$message && Lang::has($localizationKey) => Lang::get($localizationKey), + ! $message && Lang::has($localizationKey) => Lang::get($localizationKey), default => $message }; } @@ -277,7 +276,7 @@ protected function formatDataFields(array $data): array { return tap($data, function (&$item) { foreach ($this->config as $key => $config) { - if (!Arr::has($item, $key)) { + if (! Arr::has($item, $key)) { continue; } @@ -290,7 +289,7 @@ protected function formatDataFields(array $data): array $key = $alias; } - if (!$show) { + if (! $show) { $item = Arr::except($item, $key); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0730ce3..f1440e9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -60,7 +60,7 @@ protected function defineEnvironment($app) 'error' => ['alias' => 'error', 'show' => false], 'data' => ['alias' => 'data', 'show' => true], 'data.data' => ['alias' => 'data.data', 'show' => true], // rows/items/list - ] + ], ]); } } diff --git a/tests/Unit/CustomFormatTest.php b/tests/Unit/CustomFormatTest.php index c2b68aa..9090636 100644 --- a/tests/Unit/CustomFormatTest.php +++ b/tests/Unit/CustomFormatTest.php @@ -30,7 +30,7 @@ * 'error' => ['alias' => 'error', 'show' => false], * 'data' => ['alias' => 'data', 'show' => true], * 'data.data' => ['alias' => 'data.data', 'show' => true], // rows/items/list - * ] + * ]. */ test('hide some field', function () { // 隐藏 status,message 字段名称修改成 msg @@ -38,7 +38,7 @@ expect($data)->toMatchArray([ 'code' => 200, - 'data' => (object)[], - 'msg' => '操作成功' + 'data' => (object) [], + 'msg' => '操作成功', ]); });