1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
6
|
|
|
use Zenstruck\Porpaginas\Callback\CallbackPage; |
7
|
|
|
use Zenstruck\Porpaginas\Page; |
8
|
|
|
use Zenstruck\Porpaginas\Result; |
9
|
|
|
use Zenstruck\Porpaginas\ResultPaginator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class DBALQueryBuilderResult implements Result |
15
|
|
|
{ |
16
|
|
|
use ResultPaginator; |
17
|
|
|
|
18
|
|
|
private QueryBuilder $qb; |
19
|
|
|
private $countModifier; |
20
|
|
|
private ?int $count = null; |
21
|
|
|
|
22
|
45 |
|
public function __construct(QueryBuilder $qb, ?callable $countModifier = null) |
23
|
|
|
{ |
24
|
45 |
|
$this->qb = $qb; |
25
|
|
|
$this->countModifier = $countModifier ?: static function(QueryBuilder $qb) { |
26
|
36 |
|
return $qb->select('COUNT(*)'); |
27
|
45 |
|
}; |
28
|
45 |
|
} |
29
|
|
|
|
30
|
7 |
|
public function take(int $offset, int $limit): Page |
31
|
|
|
{ |
32
|
7 |
|
$qb = clone $this->qb; |
33
|
|
|
$results = static function($offset, $limit) use ($qb) { |
34
|
|
|
return $qb |
|
|
|
|
35
|
7 |
|
->setFirstResult($offset) |
36
|
7 |
|
->setMaxResults($limit) |
37
|
7 |
|
->execute() |
38
|
7 |
|
->fetchAll() |
39
|
|
|
; |
40
|
7 |
|
}; |
41
|
|
|
|
42
|
7 |
|
return new CallbackPage($results, [$this, 'count'], $offset, $limit); |
43
|
|
|
} |
44
|
|
|
|
45
|
36 |
|
public function count(): int |
46
|
|
|
{ |
47
|
36 |
|
if (null !== $this->count) { |
48
|
1 |
|
return $this->count; |
49
|
|
|
} |
50
|
|
|
|
51
|
36 |
|
$qb = clone $this->qb; |
52
|
|
|
|
53
|
36 |
|
\call_user_func($this->countModifier, $qb); |
54
|
|
|
|
55
|
36 |
|
return $this->count = $qb->execute()->fetchColumn(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
34 |
|
public function getIterator(): \Traversable |
59
|
|
|
{ |
60
|
34 |
|
$stmt = $this->qb->execute(); |
61
|
|
|
|
62
|
34 |
|
while ($data = $stmt->fetch()) { |
|
|
|
|
63
|
33 |
|
yield $data; |
64
|
|
|
} |
65
|
34 |
|
} |
66
|
|
|
} |
67
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.