Skip to content

Commit

Permalink
feat: 分页数据内层 data 字段名支持自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Feb 4, 2021
1 parent 130d914 commit 56e9601
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function array()
}
```

- 分页数据
- 分页数据(支持自定义内层 data 字段名称,比如 rows、list)

```php
{
Expand Down Expand Up @@ -200,7 +200,7 @@ public function array()
}
```

- 返回简单分页数据
- 返回简单分页数据(支持自定义内层 data 字段名称,比如 rows、list)

```php
{
Expand Down Expand Up @@ -481,7 +481,7 @@ class ResponseCodeEnum extends BaseResponseCodeEnum

```php
<?php
// resources/lang/zh-CN/enums.php
// resources/lang/zh_CN/enums.php
use App\Repositories\Enums\ResponseCodeEnum;

return [
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"orchestra/testbench": "^6.4",
"phpunit/phpunit": "^9.4",
"laravel/legacy-factories": "^1.0.4",
"jiannei/laravel-enum": "^1.2"
"jiannei/laravel-enum": "^1.2",
"league/fractal": "^0.19.2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
'enum' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class,

'validation_error_code' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::HTTP_UNPROCESSABLE_ENTITY,

'format' => [
'paginated_resource' => [
'data_field' => 'data'
],
],
];
3 changes: 2 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ protected function formatPaginatedResourceResponse($resource, string $message =
];
}

$data = array_merge_recursive(['data' => $this->parseDataFrom($resource)], $paginationInformation);
$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data');
$data = array_merge_recursive([$paginationDataField => $this->parseDataFrom($resource)], $paginationInformation);

return tap(
$this->response($this->formatData($data, $message, $code), $code, $headers, $option),
Expand Down
66 changes: 66 additions & 0 deletions src/Support/Serializers/ArraySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Jiannei\Response\Laravel\Support\Serializers;

use Illuminate\Support\Facades\Config;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Serializer\ArraySerializer as FractalArraySerializer;

class ArraySerializer extends FractalArraySerializer
{
/**
* Serialize a collection.
*
* @param string $resourceKey
* @param array $data
*
* @return array
*/
public function collection($resourceKey, array $data)
{
$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data');

return [$resourceKey ?: $paginationDataField => $data];
}

/**
* Serialize the paginator.
*
* @param PaginatorInterface $paginator
*
* @return array
*/
public function paginator(PaginatorInterface $paginator)
{
$currentPage = (int) $paginator->getCurrentPage();
$lastPage = (int) $paginator->getLastPage();
$isSimplePaginator = property_exists($paginator->getPaginator(), 'hasMore');

$pagination = [
'total' => (int) $paginator->getTotal(),
'count' => (int) $paginator->getCount(),
'per_page' => (int) $paginator->getPerPage(),
'current_page' => $currentPage,
'total_pages' => $lastPage,
'links' => [],
];

if ($currentPage > 1) {
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1);
}

if ($currentPage < $lastPage || ($isSimplePaginator && $paginator->getPaginator()->hasMore)) {
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1);
}

if (empty($pagination['links'])) {
$pagination['links'] = (object) [];
}

if ($isSimplePaginator) {
unset($pagination['total'], $pagination['total_pages']);
}

return ['pagination' => $pagination];
}
}
File renamed without changes.

0 comments on commit 56e9601

Please sign in to comment.