Skip to content

Commit 89cc7c0

Browse files
committed
Merge branch 'master' into 3.2-merge
2 parents 66c477e + 2d642bd commit 89cc7c0

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/Response/PaginatedResponse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ protected function paginationInformation(): array
4141
{
4242
$paginated = $this->resource->resource->toArray();
4343

44-
return [
44+
$default = [
4545
'links' => $this->paginationLinks($paginated),
4646
'meta' => $this->meta($paginated),
4747
];
48+
49+
if ($this->resource instanceof PaginationInformationInterface) {
50+
return $this->resource->paginationInformation($paginated, $default);
51+
}
52+
53+
return $default;
4854
}
4955

5056
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Resource\Response;
14+
15+
interface PaginationInformationInterface
16+
{
17+
public function paginationInformation(array $paginated, array $default);
18+
}

tests/ResourceTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use HyperfTest\Resource\Stubs\Models\Subscription;
2424
use HyperfTest\Resource\Stubs\Resources\AuthorResourceWithOptionalRelationship;
2525
use HyperfTest\Resource\Stubs\Resources\ObjectResource;
26+
use HyperfTest\Resource\Stubs\Resources\PaginationCollection;
2627
use HyperfTest\Resource\Stubs\Resources\PostCollectionResource;
2728
use HyperfTest\Resource\Stubs\Resources\PostResource;
2829
use HyperfTest\Resource\Stubs\Resources\PostResourceWithExtraData;
@@ -733,6 +734,27 @@ public function testItWontKeysIfAnyOfThemAreStrings()
733734
], ['data' => [0 => 10, 1 => 20, 'total' => 30]]);
734735
}
735736

737+
public function testResourceCollectionPagination()
738+
{
739+
$this->http(function () {
740+
$paginator = new LengthAwarePaginator(
741+
collect([new Post(['id' => 5, 'title' => 'Test Title'])]),
742+
10,
743+
15,
744+
1
745+
);
746+
747+
return new PaginationCollection($paginator);
748+
})->assertJson([
749+
'data' => [
750+
[
751+
'id' => 5,
752+
'title' => 'Test Title',
753+
],
754+
],
755+
]);
756+
}
757+
736758
private function assertJsonResourceResponse($data, $expectedJson)
737759
{
738760
$this->http(function () use ($data) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Resource\Stubs\Resources;
14+
15+
use Hyperf\Resource\Json\ResourceCollection;
16+
use Hyperf\Resource\Response\PaginationInformationInterface;
17+
18+
class PaginationCollection extends ResourceCollection implements PaginationInformationInterface
19+
{
20+
public function paginationInformation(array $paginated, array $default): array
21+
{
22+
return [];
23+
}
24+
}

0 commit comments

Comments
 (0)