Skip to content

Commit fe79ae1

Browse files
committed
PaginatedResult
1 parent e5667e8 commit fe79ae1

6 files changed

Lines changed: 51 additions & 29 deletions

File tree

src/Common/Dto/CursorPaginationResult.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@
77
class CursorPaginationResult
88
{
99
public function __construct(
10-
public readonly array $items,
11-
public readonly int $limit,
12-
public readonly int $total,
10+
private readonly array $items,
11+
private readonly int $limit,
12+
private readonly int $total,
1313
) {
1414
}
15+
16+
public function getItems(): array
17+
{
18+
return $this->items;
19+
}
20+
21+
public function getLimit(): int
22+
{
23+
return $this->limit;
24+
}
25+
26+
public function getTotal(): int
27+
{
28+
return $this->total;
29+
}
1530
}

src/Common/Serializer/CursorPaginationNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class CursorPaginationNormalizer implements NormalizerInterface
1515
*/
1616
public function normalize($object, string $format = null, array $context = []): array
1717
{
18-
$items = $object->items;
19-
$limit = $object->limit;
20-
$total = $object->total;
18+
$items = $object->getItems();
19+
$limit = $object->getLimit();
20+
$total = $object->getTotal();
2121
$hasNext = !empty($items) && isset($items[array_key_last($items)]['id']);
2222

2323
return [

src/Common/Service/Provider/PaginatedDataProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ public function getPaginatedList(
3737
throw new RuntimeException('Repository not found');
3838
}
3939

40-
$items = $repository->getFilteredAfterId(
40+
$result = $repository->getFilteredAfterId(
4141
lastId: $pagination->afterId,
4242
limit: $pagination->limit,
4343
filter: $filter,
4444
);
45-
$total = $repository->count();
4645

4746
$normalizedItems = array_map(
4847
fn($item) => $normalizer->normalize($item, 'json'),
49-
$items
48+
$result->getItems()
5049
);
5150

5251
return $this->paginationNormalizer->normalize(
53-
new CursorPaginationResult($normalizedItems, $pagination->limit, $total)
52+
new CursorPaginationResult(
53+
$normalizedItems,
54+
$result->getLimit(),
55+
$result->getTotal(),
56+
)
5457
);
5558
}
5659
}

tests/Helpers/DummyPaginatableRepository.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66

77
use Doctrine\ORM\EntityRepository;
88
use PhpList\Core\Domain\Common\Model\Filter\FilterRequestInterface;
9+
use PhpList\Core\Domain\Common\Model\PaginatedResult;
910
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
1011

1112
class DummyPaginatableRepository extends EntityRepository implements PaginatableRepositoryInterface
1213
{
13-
public function getFilteredAfterId(int $lastId, int $limit, ?FilterRequestInterface $filter = null): array
14+
public function getFilteredAfterId(int $lastId, int $limit, ?FilterRequestInterface $filter = null): PaginatedResult
1415
{
15-
return [
16-
(object)['id' => 1, 'name' => 'Item 1'],
17-
(object)['id' => 2, 'name' => 'Item 2'],
18-
];
19-
}
20-
21-
public function count(array $criteria = []): int
22-
{
23-
return 10;
16+
return new PaginatedResult(
17+
[
18+
(object)['id' => 1, 'name' => 'Item 1'],
19+
(object)['id' => 2, 'name' => 'Item 2'],
20+
],
21+
2,
22+
10,
23+
2,
24+
);
2425
}
2526
}

tests/Integration/Subscription/Controller/SubscriberListControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function testGetListMembersWithCurrentSessionKeyForExistingListWithSubscr
313313
],
314314
],
315315
'pagination' => [
316-
'total' => 3,
316+
'total' => 2,
317317
'limit' => 25,
318318
'has_more' => false,
319319
'next_cursor' => 2,

tests/Unit/Common/Service/Provider/PaginatedDataProviderTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\RestBundle\Tests\Unit\Common\Service\Provider;
66

77
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Common\Model\PaginatedResult;
89
use PhpList\RestBundle\Common\Dto\CursorPaginationResult;
910
use PhpList\RestBundle\Common\Request\PaginationCursorRequest;
1011
use PhpList\RestBundle\Common\Serializer\CursorPaginationNormalizer;
@@ -37,14 +38,16 @@ public function testGetPaginatedListSuccess(): void
3738
$repository->expects($this->once())
3839
->method('getFilteredAfterId')
3940
->with(0, 2)
40-
->willReturn([
41-
(object)['id' => 1, 'name' => 'Item 1'],
42-
(object)['id' => 2, 'name' => 'Item 2'],
43-
]);
44-
45-
$repository->expects($this->once())
46-
->method('count')
47-
->willReturn(10);
41+
->willReturn(
42+
new PaginatedResult([
43+
(object)['id' => 1, 'name' => 'Item 1'],
44+
(object)['id' => 2, 'name' => 'Item 2'],
45+
],
46+
2,
47+
10,
48+
2,
49+
)
50+
);
4851

4952
$entityManager->method('getRepository')
5053
->willReturn($repository);

0 commit comments

Comments
 (0)