-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSimplePager.php
More file actions
129 lines (106 loc) · 3.48 KB
/
Copy pathSimplePager.php
File metadata and controls
129 lines (106 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Datagrid;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\AdminBundle\Util\TraversableToCollection;
/**
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
* @author Sjoerd Peters <sjoerd.peters@gmail.com>
*
* @phpstan-template T of ProxyQueryInterface
* @phpstan-extends Pager<T>
*/
final class SimplePager extends Pager
{
/**
* @var iterable<object>|null
*/
protected $results;
/**
* thresholdCount is null prior to its initialization in `getCurrentPageResults()`.
*/
private ?int $thresholdCount = null;
/**
* The threshold parameter can be used to determine how far ahead the pager
* should fetch results.
*
* If set to 1 which is the minimal value the pager will generate a link to the next page
* If set to 2 the pager will generate links to the next two pages
* If set to 3 the pager will generate links to the next three pages
* etc.
*/
public function __construct(
int $maxPerPage = 10,
private int $threshold = 1
) {
parent::__construct($maxPerPage);
}
public function countResults(): int
{
return ($this->getPage() - 1) * $this->getMaxPerPage() + ($this->thresholdCount ?? 0);
}
public function getCurrentPageResults(): iterable
{
if (null !== $this->results) {
return $this->results;
}
$query = $this->getQuery();
if (null === $query) {
throw new \LogicException('Uninitialized query.');
}
$results = TraversableToCollection::transform($query->execute());
$this->thresholdCount = $results->count();
if ($this->thresholdCount > $this->getMaxPerPage()) {
$results = new ArrayCollection($results->slice(0, $this->getMaxPerPage()));
}
$this->results = $results;
return $this->results;
}
/**
* @throws \LogicException the query is uninitialized
*/
public function init(): void
{
$query = $this->getQuery();
if (null === $query) {
throw new \LogicException('Uninitialized query.');
}
if (0 === $this->getPage() || 0 === $this->getMaxPerPage()) {
$this->setLastPage(0);
$query->setFirstResult(0);
$query->setMaxResults(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$query->setFirstResult($offset);
$maxOffset = $this->getThreshold() > 0
? $this->getMaxPerPage() * $this->threshold + 1 : $this->getMaxPerPage() + 1;
$query->setMaxResults($maxOffset);
$this->results = $this->getCurrentPageResults();
$t = (int) ceil(($this->thresholdCount ?? 0) / $this->getMaxPerPage()) + $this->getPage() - 1;
$this->setLastPage(max(1, $t));
}
}
/**
* Set how many pages to look forward to create links to next pages.
*/
public function setThreshold(int $threshold): void
{
$this->threshold = $threshold;
}
public function getThreshold(): int
{
return $this->threshold;
}
public function isDeterministic(): bool
{
return false;
}
}