Skip to content

Commit 56e9601

Browse files
author
jiannei
committed
feat: 分页数据内层 data 字段名支持自定义
1 parent 130d914 commit 56e9601

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function array()
152152
}
153153
```
154154

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

157157
```php
158158
{
@@ -200,7 +200,7 @@ public function array()
200200
}
201201
```
202202

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

205205
```php
206206
{
@@ -481,7 +481,7 @@ class ResponseCodeEnum extends BaseResponseCodeEnum
481481

482482
```php
483483
<?php
484-
// resources/lang/zh-CN/enums.php
484+
// resources/lang/zh_CN/enums.php
485485
use App\Repositories\Enums\ResponseCodeEnum;
486486

487487
return [

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"orchestra/testbench": "^6.4",
1616
"phpunit/phpunit": "^9.4",
1717
"laravel/legacy-factories": "^1.0.4",
18-
"jiannei/laravel-enum": "^1.2"
18+
"jiannei/laravel-enum": "^1.2",
19+
"league/fractal": "^0.19.2"
1920
},
2021
"autoload": {
2122
"psr-4": {

config/response.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
'enum' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class,
1414

1515
'validation_error_code' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::HTTP_UNPROCESSABLE_ENTITY,
16+
17+
'format' => [
18+
'paginated_resource' => [
19+
'data_field' => 'data'
20+
],
21+
],
1622
];

src/Response.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ protected function formatPaginatedResourceResponse($resource, string $message =
279279
];
280280
}
281281

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

284285
return tap(
285286
$this->response($this->formatData($data, $message, $code), $code, $headers, $option),
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Jiannei\Response\Laravel\Support\Serializers;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use League\Fractal\Pagination\PaginatorInterface;
7+
use League\Fractal\Serializer\ArraySerializer as FractalArraySerializer;
8+
9+
class ArraySerializer extends FractalArraySerializer
10+
{
11+
/**
12+
* Serialize a collection.
13+
*
14+
* @param string $resourceKey
15+
* @param array $data
16+
*
17+
* @return array
18+
*/
19+
public function collection($resourceKey, array $data)
20+
{
21+
$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data');
22+
23+
return [$resourceKey ?: $paginationDataField => $data];
24+
}
25+
26+
/**
27+
* Serialize the paginator.
28+
*
29+
* @param PaginatorInterface $paginator
30+
*
31+
* @return array
32+
*/
33+
public function paginator(PaginatorInterface $paginator)
34+
{
35+
$currentPage = (int) $paginator->getCurrentPage();
36+
$lastPage = (int) $paginator->getLastPage();
37+
$isSimplePaginator = property_exists($paginator->getPaginator(), 'hasMore');
38+
39+
$pagination = [
40+
'total' => (int) $paginator->getTotal(),
41+
'count' => (int) $paginator->getCount(),
42+
'per_page' => (int) $paginator->getPerPage(),
43+
'current_page' => $currentPage,
44+
'total_pages' => $lastPage,
45+
'links' => [],
46+
];
47+
48+
if ($currentPage > 1) {
49+
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1);
50+
}
51+
52+
if ($currentPage < $lastPage || ($isSimplePaginator && $paginator->getPaginator()->hasMore)) {
53+
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1);
54+
}
55+
56+
if (empty($pagination['links'])) {
57+
$pagination['links'] = (object) [];
58+
}
59+
60+
if ($isSimplePaginator) {
61+
unset($pagination['total'], $pagination['total_pages']);
62+
}
63+
64+
return ['pagination' => $pagination];
65+
}
66+
}
File renamed without changes.

0 commit comments

Comments
 (0)