Skip to content

Commit 1bd1751

Browse files
authored
Merge pull request #1601 from norkunas/override-max-items
Allow to set maximum items per page at operation/resource level
2 parents 4ea214d + 506fdd5 commit 1bd1751

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
9393
}
9494

9595
if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) {
96+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true);
97+
9698
$itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage);
97-
$itemsPerPage = (null !== $this->maximumItemPerPage && $itemsPerPage >= $this->maximumItemPerPage ? $this->maximumItemPerPage : $itemsPerPage);
99+
$itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage);
98100
}
99101

100102
if (0 > $itemsPerPage) {

tests/Bridge/Doctrine/Orm/Extension/PaginationExtensionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,41 @@ public function testApplyToCollectionPaginationDisabled()
300300
$extension->applyToCollection($queryBuilder, new QueryNameGenerator(), 'Foo', 'op');
301301
}
302302

303+
public function testApplyToCollectionWithMaximumItemsPerPage()
304+
{
305+
$requestStack = new RequestStack();
306+
$requestStack->push(new Request(['pagination' => true, 'itemsPerPage' => 80, 'page' => 1]));
307+
308+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
309+
$attributes = [
310+
'pagination_enabled' => true,
311+
'pagination_client_enabled' => true,
312+
'maximum_items_per_page' => 80,
313+
];
314+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata(null, null, null, [], [], $attributes))->shouldBeCalled();
315+
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
316+
317+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
318+
$queryBuilderProphecy->setFirstResult(0)->willReturn($queryBuilderProphecy)->shouldBeCalled();
319+
$queryBuilderProphecy->setMaxResults(80)->shouldBeCalled();
320+
$queryBuilder = $queryBuilderProphecy->reveal();
321+
322+
$extension = new PaginationExtension(
323+
$this->prophesize(ManagerRegistry::class)->reveal(),
324+
$requestStack,
325+
$resourceMetadataFactory,
326+
true,
327+
true,
328+
true,
329+
30,
330+
'page',
331+
'pagination',
332+
'itemsPerPage',
333+
50
334+
);
335+
$extension->applyToCollection($queryBuilder, new QueryNameGenerator(), 'Foo', 'op');
336+
}
337+
303338
public function testSupportsResult()
304339
{
305340
$requestStack = new RequestStack();

0 commit comments

Comments
 (0)