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 |
||||
0 ignored issues
–
show
|
|||||
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(); |
|||
0 ignored issues
–
show
The function
Doctrine\DBAL\Driver\Res...tatement::fetchColumn() has been deprecated: Use fetchOne() instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
56 | } |
||||
57 | |||||
58 | 34 | public function getIterator(): \Traversable |
|||
59 | { |
||||
60 | 34 | $stmt = $this->qb->execute(); |
|||
61 | |||||
62 | 34 | while ($data = $stmt->fetch()) { |
|||
0 ignored issues
–
show
The function
Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
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.