Skip to content

Commit 88b7993

Browse files
committed
pref: format config
1 parent 5e19441 commit 88b7993

File tree

4 files changed

+30
-75
lines changed

4 files changed

+30
-75
lines changed

config/response.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,25 @@
4242
\Illuminate\Auth\AuthenticationException::class => [
4343

4444
],
45-
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class =>[
45+
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
4646
'message' => '',
4747
],
4848
\Illuminate\Database\Eloquent\ModelNotFoundException::class => [
4949
'message' => '',
5050
],
5151
],
5252

53-
// Set the structure of the response data
53+
// Any key that returns data exists supports custom aliases and display.
5454
'format' => [
55-
\Jiannei\Response\Laravel\Support\Format::class,
56-
[
55+
'class' => \Jiannei\Response\Laravel\Support\Format::class,
56+
'config' => [
57+
// key => config
5758
'status' => ['alias' => 'status', 'show' => true],
5859
'code' => ['alias' => 'code', 'show' => true],
5960
'message' => ['alias' => 'message', 'show' => true],
6061
'error' => ['alias' => 'error', 'show' => true],
61-
'data' => [
62-
'alias' => 'data',
63-
'show' => true,
64-
65-
'fields' => [
66-
// When data is nested with data, such as returning paged data, you can also set an alias for the inner data
67-
'data' => ['alias' => 'data', 'show' => true], // data/rows/list
68-
69-
'meta' => [
70-
'alias' => 'meta',
71-
'show' => true,
72-
73-
'fields' => [
74-
'pagination' => [
75-
'alias' => 'pagination',
76-
'show' => true,
77-
78-
'fields' => [
79-
'total' => ['alias' => 'total', 'show' => true],
80-
'count' => ['alias' => 'count', 'show' => true],
81-
'per_page' => ['alias' => 'per_page', 'show' => true],
82-
'current_page' => ['alias' => 'current_page', 'show' => true],
83-
'total_pages' => ['alias' => 'total_pages', 'show' => true],
84-
'links' => [
85-
'alias' => 'links',
86-
'show' => true,
87-
88-
'fields' => [
89-
'previous' => ['alias' => 'previous', 'show' => true],
90-
'next' => ['alias' => 'next', 'show' => true],
91-
],
92-
],
93-
],
94-
],
95-
],
96-
],
97-
],
98-
],
62+
'data' => ['alias' => 'data', 'show' => true,],
63+
'data.data' => ['alias' => 'data.data', 'show' => true,],// rows/items/list
9964
],
10065
],
10166
];

src/Providers/LaravelServiceProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ public function register()
2222

2323
public function boot()
2424
{
25-
$formatter = $this->app['config']->get('response.format.0', \Jiannei\Response\Laravel\Support\Format::class);
26-
$config = $this->app['config']->get('response.format.1', []);
25+
$formatter = $this->app['config']->get('response.format.class', \Jiannei\Response\Laravel\Support\Format::class);
2726

2827
if (is_string($formatter) && class_exists($formatter)) {
29-
$this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter, $config) {
30-
return new $formatter($config);
28+
$this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter) {
29+
return new $formatter;
3130
});
3231
}
3332
}

src/Support/Format.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class Format implements \Jiannei\Response\Laravel\Contracts\Format
2424
{
2525
use Macroable;
2626

27-
protected $config;
28-
29-
public function __construct($config = [])
30-
{
31-
$this->config = $config;
32-
}
33-
3427
/**
3528
* Return a new JSON response from the application.
3629
*
@@ -62,7 +55,7 @@ public function data(?array $data, ?string $message, int $code, $errors = null):
6255
'message' => $this->formatMessage($code, $message),
6356
'data' => $data ?: (object) $data,
6457
'error' => $errors ?: (object) [],
65-
], $this->config);
58+
]);
6659
}
6760

6861
/**
@@ -135,7 +128,7 @@ public function jsonResource(JsonResource $resource, string $message = '', int $
135128
*/
136129
protected function formatMessage(int $code, ?string $message): ?string
137130
{
138-
if (! $message && class_exists($enumClass = Config::get('response.enum'))) {
131+
if (!$message && class_exists($enumClass = Config::get('response.enum'))) {
139132
$message = $enumClass::fromValue($code)->description;
140133
}
141134

@@ -205,36 +198,32 @@ protected function formatPaginatedData(array $paginated): array
205198
/**
206199
* Format response data fields.
207200
*
208-
* @param array $responseData
209-
* @param array $dataFieldsConfig
201+
* @param array $data
210202
* @return array
211203
*/
212-
protected function formatDataFields(array $responseData, array $dataFieldsConfig = []): array
204+
protected function formatDataFields(array $data): array
213205
{
214-
if (empty($dataFieldsConfig)) {
215-
return $responseData;
216-
}
206+
$formatConfig = \config('response.format.config', []);
217207

218-
foreach ($responseData as $field => $value) {
219-
$fieldConfig = Arr::get($dataFieldsConfig, $field);
220-
if (is_null($fieldConfig)) {
208+
foreach ($formatConfig as $key => $config) {
209+
if (!Arr::has($data, $key)) {
221210
continue;
222211
}
223212

224-
if ($value && is_array($value) && in_array($field, ['data', 'meta', 'pagination', 'links'])) {
225-
$value = $this->formatDataFields($value, Arr::get($dataFieldsConfig, "{$field}.fields", []));
226-
}
213+
$show = $config['show'] ?? true;
214+
$alias = $config['alias'] ?? '';
227215

228-
$alias = $fieldConfig['alias'] ?? $field;
229-
$show = $fieldConfig['show'] ?? true;
230-
$map = $fieldConfig['map'] ?? null;
231-
unset($responseData[$field]);
216+
if ($alias && $alias !== $key) {
217+
Arr::set($data, $alias, Arr::get($data, $key));
218+
$data = Arr::except($data, $key);
219+
$key = $alias;
220+
}
232221

233-
if ($show) {
234-
$responseData[$alias] = $map[$value] ?? $value;
222+
if (!$show) {
223+
$data = Arr::except($data, $key);
235224
}
236225
}
237226

238-
return $responseData;
227+
return $data;
239228
}
240229
}

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ protected function defineEnvironment($app)
4444

4545
$app['config']->set('response.enum', \Jiannei\Response\Laravel\Tests\Repositories\Enums\ResponseCodeEnum::class);
4646
if ($this instanceof FormatTest) {
47-
$app['config']->set('response.format', [\Jiannei\Response\Laravel\Tests\Support\Format::class]);
47+
$app['config']->set('response.format', [
48+
'class' => \Jiannei\Response\Laravel\Tests\Support\Format::class
49+
]);
4850
}
4951
}
5052
}

0 commit comments

Comments
 (0)