Completed
Pull Request — master (#261)
by Konstantin
10:23
created

LimitedAdapter::getSlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pagerfanta\Adapter;
13
14
/**
15
 * Adapter that allows limit maximum number of results. It can be useful for preventing
16
 * slow down queries with large offsets or get rid of errors like "result window too large".
17
 *
18
 * @author Konstantin Myakshin <[email protected]>
19
 */
20
class LimitedAdapter implements AdapterInterface
21
{
22
    private $decorated;
23
24
    private $maxNbResults;
25
26
    public function __construct(AdapterInterface $decorated, int $maxNbResults)
27
    {
28
        $this->decorated = $decorated;
29
        $this->maxNbResults = $maxNbResults;
30
    }
31
32
    public function getNbResults()
33
    {
34
        return min($this->decorated->getNbResults(), $this->maxNbResults);
35
    }
36
37
    public function getSlice($offset, $length)
38
    {
39
        return $this->decorated->getSlice($offset, $length);
40
    }
41
}
42