Skip to content

Commit

Permalink
pref: format config
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 10, 2023
1 parent 5e19441 commit 88b7993
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 75 deletions.
49 changes: 7 additions & 42 deletions config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,25 @@
\Illuminate\Auth\AuthenticationException::class => [

],
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class =>[
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
'message' => '',
],
\Illuminate\Database\Eloquent\ModelNotFoundException::class => [
'message' => '',
],
],

// 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
],
],
];
7 changes: 3 additions & 4 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
Expand Down
45 changes: 17 additions & 28 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
]);
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}
}
}

0 comments on commit 88b7993

Please sign in to comment.