Skip to content

Implement LimitedAdapter (closes #244). #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Pagerfanta/Adapter/LimitedAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pagerfanta\Adapter;

/**
* Adapter that allows limit maximum number of results. It can be useful for preventing
* slow down queries with large offsets or get rid of errors like "result window too large".
*
* @author Konstantin Myakshin <[email protected]>
*/
class LimitedAdapter implements AdapterInterface
{
private $decorated;

private $maxNbResults;

public function __construct(AdapterInterface $decorated, int $maxNbResults)
{
$this->decorated = $decorated;
$this->maxNbResults = $maxNbResults;
}

public function getNbResults()
{
return min($this->decorated->getNbResults(), $this->maxNbResults);
}

public function getSlice($offset, $length)
{
return $this->decorated->getSlice($offset, $length);
}
}
50 changes: 50 additions & 0 deletions tests/Pagerfanta/Tests/Adapter/LimitedAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Pagerfanta\Tests\Adapter;

use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Adapter\LimitedAdapter;
use PHPUnit\Framework\TestCase;

class LimitedAdapterTest extends TestCase
{
/**
* @dataProvider nbResultsProvider
*/
public function testNbResults(int $nbResultsFromAdapter, int $maxNbResults, int $result)
{
$decorated = $this->createMock(AdapterInterface::class);

$decorated
->expects($this->once())
->method('getNbResults')
->will($this->returnValue($nbResultsFromAdapter)));

$adapter = new LimitedAdapter($decorated, $maxNbResults);
$this->assertSame($result, $adapter->getNbResults());
}

public function testGetSlice()
{
$decorated = $this->createMock(AdapterInterface::class);
$offset = 0;
$limit = 5;
$result = [0, 1, 2, 3, 4];

$decorated
->expects($this->once())
->method('getSlice')
->with($offset, $limit)
->will($this->returnValue($result));

$adapter = new LimitedAdapter($decorated, 100);
$this->assertSame($result, $adapter->getSlice($offset, $limit));
}

public function nbResultsProvider()
{
yield 'Adapter returns more results than allowed.' => [100, 90, 90];

yield 'Adapter returns less results than allowed.' => [100, 1000, 100];
}
}