Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot authored and jiannei committed Oct 14, 2023
1 parent a18fcd1 commit 63b733a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
16 changes: 11 additions & 5 deletions src/Contract/ResponseFormat.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <longjian.huang@foxmail.com>
*
* 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;
Expand All @@ -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.
*
Expand All @@ -60,4 +66,4 @@ public function resourceCollection(ResourceCollection $collection): array;
* @return array
*/
public function jsonResource(JsonResource $resource): array;
}
}
7 changes: 3 additions & 4 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Format implements ResponseFormat
protected ?array $data = null;
protected int $statusCode = 200;


public function __construct(protected array $config = [])
{
}
Expand Down Expand Up @@ -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
};
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -290,7 +289,7 @@ protected function formatDataFields(array $data): array
$key = $alias;
}

if (!$show) {
if (! $show) {
$item = Arr::except($item, $key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/CustomFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
* '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
$data = Format::data()->get();

expect($data)->toMatchArray([
'code' => 200,
'data' => (object)[],
'msg' => '操作成功'
'data' => (object) [],
'msg' => '操作成功',
]);
});

0 comments on commit 63b733a

Please sign in to comment.