Skip to content

Commit

Permalink
pref: 自定义 format 支持传参
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Dec 30, 2022
1 parent 5926d0c commit 02e975a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
}
],
"require": {
"php": "^7.2.5|^8.0"
"php": "^7.2.5|^8.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "^7.0",
Expand Down
48 changes: 47 additions & 1 deletion config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,51 @@
],

// Set the structure of the response data
'format' => \Jiannei\Response\Laravel\Support\Format::class,
'format' => [
\Jiannei\Response\Laravel\Support\Format::class,
[
'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' => [
'alia' => '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],
],
],
],
],
],
],
],
],
],
],
];
7 changes: 5 additions & 2 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public function register()

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

if (is_string($formatter) && class_exists($formatter)) {
$this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, $formatter);
$this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter,$config) {
return new $formatter($config);
});
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

class LumenServiceProvider extends LaravelServiceProvider
{
public function boot()
{
$this->app->configure('response');
}

protected function setupConfig()
{
$path = dirname(__DIR__, 2).'/config/response.php';
Expand Down
9 changes: 8 additions & 1 deletion src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Format implements \Jiannei\Response\Laravel\Contracts\Format
{
use Macroable;

protected $config;

public function __construct($config = [])
{
$this->config = $config;
}

/**
* Format return data structure.
*
Expand All @@ -39,7 +46,7 @@ public function data(?array $data, ?string $message, int $code, $errors = null):
'message' => $this->formatMessage($code, $message),
'data' => $data ?: (object) $data,
'error' => $errors ?: (object) [],
], Config::get('response.format.fields', []));
], $this->config);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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', [\Jiannei\Response\Laravel\Tests\Support\Format::class]);
}
}
}

0 comments on commit 02e975a

Please sign in to comment.