Skip to content

Commit

Permalink
feat: 支持自定义字段返回
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Dec 29, 2022
1 parent a5ebda9 commit f60d5b6
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 16 deletions.
65 changes: 65 additions & 0 deletions src/Contracts/Format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Jiannei\Response\Laravel\Contracts;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;

interface Format
{
/**
* Format return data structure.
*
* @param array|null $data
* @param string|null $message
* @param int $code
* @param null $errors
* @return array
*/
public function data(?array $data, ?string $message, int $code, $errors = null): array;

/**
* Http status code.
*
* @param $code
* @return int
*/
public function statusCode($code): int;

/**
* Format paginator data.
*
* @param AbstractPaginator $resource
* @param string $message
* @param int $code
* @param array $headers
* @param int $option
* @return array
*/
public function paginator(AbstractPaginator $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0):array;

/**
* Format collection resource data.
*
* @param ResourceCollection $resource
* @param string $message
* @param int $code
* @param array $headers
* @param int $option
* @return array
*/
public function resourceCollection(ResourceCollection $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array;

/**
* Format JsonResource Data.
*
* @param JsonResource $resource
* @param string $message
* @param int $code
* @param array $headers
* @param int $option
* @return array
*/
public function jsonResource(JsonResource $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array;
}
9 changes: 9 additions & 0 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function register()
$this->setupConfig();
}

public function boot()
{
$format = $this->app['config']['response']['format'];

if (is_string($format) && class_exists($format)) {
$this->app->bind(\Jiannei\Response\Laravel\Support\Format::class,$format);
}
}

protected function setupConfig()
{
$path = dirname(__DIR__, 2).'/config/response.php';
Expand Down
6 changes: 3 additions & 3 deletions src/Support/Facades/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
/**
* @method static int statusCode(int $code)
* @method static array data($data, $message, $code, $errors = null)
* @method static mixed resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static mixed jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
* @method static mixed paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
* @method static array resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static array jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
* @method static array paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
*
* @see \Jiannei\Response\Laravel\Support\Format
*/
Expand Down
27 changes: 14 additions & 13 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@
namespace Jiannei\Response\Laravel\Support;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Traits\Macroable;

class Format
class Format implements \Jiannei\Response\Laravel\Contracts\Format
{
use Macroable;

/**
* Format return data structure.
*
* @param JsonResource|array|null $data
* @param $message
* @param $code
* @param array|null $data
* @param string|null $message
* @param int $code
* @param null $errors
* @return array
*/
public function data($data, $message, $code, $errors = null): array
public function data(?array $data, ?string $message, int $code, $errors = null): array
{
if (! $message && class_exists($enumClass = Config::get('response.enum'))) {
$message = $enumClass::fromValue($code)->description;
Expand Down Expand Up @@ -64,9 +65,9 @@ public function statusCode($code): int
* @param int $code
* @param array $headers
* @param int $option
* @return mixed
* @return array
*/
public function paginator($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
public function paginator(AbstractPaginator $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array
{
$paginated = $resource->toArray();

Expand All @@ -80,14 +81,14 @@ public function paginator($resource, string $message = '', $code = 200, array $h
/**
* Format collection resource data.
*
* @param JsonResource $resource
* @param ResourceCollection $resource
* @param string $message
* @param int $code
* @param array $headers
* @param int $option
* @return mixed
* @return array
*/
public function resourceCollection($resource, string $message = '', int $code = 200, array $headers = [], int $option = 0)
public function resourceCollection(ResourceCollection $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array
{
$data = array_merge_recursive(['data' => $resource->resolve(request())], $resource->with(request()), $resource->additional);
if ($resource->resource instanceof AbstractPaginator) {
Expand All @@ -108,9 +109,9 @@ public function resourceCollection($resource, string $message = '', int $code =
* @param int $code
* @param array $headers
* @param int $option
* @return mixed
* @return array
*/
public function jsonResource($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
public function jsonResource(JsonResource $resource, string $message = '', int $code = 200, array $headers = [], int $option = 0): array
{
$resourceData = array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional);

Expand Down Expand Up @@ -143,7 +144,7 @@ protected function formatStatus(int $code): string
* @param array $paginated
* @return array
*/
protected function formatPaginatedData(array $paginated)
protected function formatPaginatedData(array $paginated): array
{
return [
'meta' => [
Expand Down
18 changes: 18 additions & 0 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Jiannei\Response\Laravel\Tests;

use Jiannei\Response\Laravel\Support\Facades\Response;

class FormatTest extends TestCase
{
public function testAddExtraField()
{
$response = Response::success();

$this->assertEquals(200, $response->status());

$this->assertArrayHasKey('extra',$response->getData(true));
$this->assertArrayHasKey('time',$response->getData(true)['extra']);
}
}
26 changes: 26 additions & 0 deletions tests/Support/Format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Jiannei\Response\Laravel\Tests\Support;

use Illuminate\Support\Facades\Config;

class Format extends \Jiannei\Response\Laravel\Support\Format
{
public function data(?array $data, ?string $message, int $code, $errors = null): array
{
if (! $message && class_exists($enumClass = Config::get('response.enum'))) {
$message = $enumClass::fromValue($code)->description;
}

return $this->formatDataFields([
'status' => $this->formatStatus($code),
'code' => $code,
'message' => $message,
'data' => $data ?: (object) $data,
'error' => $errors ?: (object) [],
'extra' => [
'time' => time()
],
], Config::get('response.format.fields', []));
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ protected function defineEnvironment($app)
]);

$app['config']->set('response.enum', \Jiannei\Response\Laravel\Tests\Repositories\Enums\ResponseCodeEnum::class);
$app['config']->set('response.format',\Jiannei\Response\Laravel\Tests\Support\Format::class);
}
}

0 comments on commit f60d5b6

Please sign in to comment.