1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Pagination; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Ynlo\GraphQLBundle\Model\ConnectionInterface; |
15
|
|
|
use Ynlo\GraphQLBundle\Model\NodeConnection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DoctrineOffsetCursorPaginator |
19
|
|
|
*/ |
20
|
|
|
class DoctrineOffsetCursorPaginator implements DoctrineCursorPaginatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var NodeConnection |
24
|
|
|
*/ |
25
|
|
|
protected $connection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
10 |
|
public function paginate(QueryBuilder $query, PaginationRequest $pagination, ConnectionInterface $connection) |
31
|
|
|
{ |
32
|
10 |
|
$count = $this->getQueryTotal($query); |
33
|
10 |
|
$this->connection = $connection; |
|
|
|
|
34
|
10 |
|
$this->connection->setTotalCount($count); |
35
|
|
|
|
36
|
10 |
|
$this->applyCursor($query, $count, $pagination); |
37
|
|
|
|
38
|
10 |
|
$limit = $pagination->getFirst() ?? $pagination->getLast(); |
39
|
10 |
|
$query->setMaxResults($limit); |
40
|
|
|
|
41
|
10 |
|
$results = $query->getQuery()->execute(); |
42
|
|
|
|
43
|
10 |
|
$offset = $query->getFirstResult(); |
44
|
|
|
|
45
|
10 |
|
$cursorOffset = $offset - 1; |
46
|
10 |
|
foreach ($results as $result) { |
47
|
8 |
|
$cursorOffset ++; |
48
|
|
|
|
49
|
8 |
|
if (!$this->connection->getPageInfo()->getStartCursor()) { |
|
|
|
|
50
|
8 |
|
$this->connection->getPageInfo()->setStartCursor($this->encodeCursor($offset)); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
$cursor = $this->encodeCursor($cursorOffset); |
54
|
8 |
|
$this->connection->addEdge($this->connection->createEdge($result, $cursor)); |
55
|
8 |
|
$this->connection->getPageInfo()->setEndCursor($cursor); |
56
|
|
|
} |
57
|
10 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param QueryBuilder $qb |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
10 |
|
protected function getQueryTotal(QueryBuilder $qb) |
65
|
|
|
{ |
66
|
10 |
|
$countQuery = clone $qb; |
67
|
|
|
|
68
|
10 |
|
if (count($qb->getParameters()) > 0) { |
69
|
3 |
|
$countQuery->setParameters($qb->getParameters()); |
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
if ($countQuery->getDQLPart('orderBy')) { |
73
|
7 |
|
$countQuery->resetDQLPart('orderBy'); |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
$countQuery->setMaxResults(null); |
77
|
10 |
|
$countQuery->setFirstResult(0); |
78
|
|
|
|
79
|
10 |
|
$queryAlias = $qb->getAllAliases()[0]; |
80
|
10 |
|
$countQuery->select(sprintf('count(DISTINCT %s.%s) as total', $queryAlias, 'id')); |
81
|
|
|
|
82
|
10 |
|
return $countQuery->getQuery()->getSingleScalarResult(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param QueryBuilder $qb |
87
|
|
|
* @param int $count |
88
|
|
|
* @param PaginationRequest $pagination |
89
|
|
|
*/ |
90
|
10 |
|
protected function applyCursor(QueryBuilder $qb, $count, PaginationRequest $pagination) |
91
|
|
|
{ |
92
|
10 |
|
$limit = $pagination->getFirst() ?? $pagination->getLast(); |
93
|
10 |
|
$offset = 0; |
94
|
10 |
|
if (null !== $pagination->getBefore()) { |
95
|
2 |
|
$offset = $this->decodeCursor($pagination->getBefore()) - $limit; |
96
|
|
|
|
97
|
|
|
//when the offset is less than 0, |
98
|
|
|
//the limit of records will be modified to start in 0 |
99
|
2 |
|
if ($offset < 0) { |
100
|
|
|
if ($pagination->getFirst()) { |
|
|
|
|
101
|
|
|
$pagination->setFirst($pagination->getFirst() - abs($offset)); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$pagination->setLast($pagination->getLast() - abs($offset)); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
$offset = 0; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//first records before any cursor always start in 0 |
109
|
2 |
|
if ($pagination->getFirst()) { |
|
|
|
|
110
|
2 |
|
$offset = 0; |
111
|
|
|
} |
112
|
8 |
|
} elseif (null !== $pagination->getAfter()) { |
113
|
|
|
//last records after any cursor always start in ($count - $limit) |
114
|
2 |
|
if ($pagination->getLast()) { |
|
|
|
|
115
|
1 |
|
$offset = $count - $pagination->getLast(); |
116
|
1 |
|
if ($offset < $pagination->getAfter()) { |
117
|
1 |
|
$offset = $pagination->getAfter() + 1; |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
1 |
|
$offset = $this->decodeCursor($pagination->getAfter()) + 1; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
10 |
|
if ($pagination->getLast() && !$pagination->getBefore() && !$pagination->getAfter()) { |
|
|
|
|
125
|
|
|
$offset = $count - $pagination->getLast(); |
126
|
|
|
if ($offset < 0) { |
127
|
|
|
$offset = 0; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
10 |
|
if (0 === $offset) { |
132
|
7 |
|
$this->connection->getPageInfo()->setHasPreviousPage(false); |
133
|
|
|
} else { |
134
|
3 |
|
$this->connection->getPageInfo()->setHasPreviousPage(true); |
135
|
|
|
} |
136
|
|
|
|
137
|
10 |
|
if ($offset + $limit >= $count) { |
138
|
4 |
|
$this->connection->getPageInfo()->setHasNextPage(false); |
139
|
|
|
} else { |
140
|
7 |
|
$this->connection->getPageInfo()->setHasNextPage(true); |
141
|
|
|
} |
142
|
|
|
|
143
|
10 |
|
$qb->setFirstResult($offset); |
144
|
10 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $cursor |
148
|
|
|
* |
149
|
|
|
* @return int |
150
|
|
|
*/ |
151
|
3 |
|
protected function decodeCursor($cursor) |
152
|
|
|
{ |
153
|
3 |
|
list(, $offset) = explode(':', base64_decode($cursor)); |
|
|
|
|
154
|
|
|
|
155
|
3 |
|
return $offset; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $offset |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
8 |
|
protected function encodeCursor($offset) |
164
|
|
|
{ |
165
|
8 |
|
return base64_encode(sprintf('cursor:%s', $offset)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.