1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Pagination\Entity\Doctrine; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
use Paysera\Pagination\Entity\OrderingConfiguration; |
8
|
|
|
use Paysera\Pagination\Exception\InvalidOrderByException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
class ConfiguredQuery |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QueryBuilder |
16
|
|
|
*/ |
17
|
|
|
private $queryBuilder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array|OrderingConfiguration[] associative, keys are strings for ordering fields |
21
|
|
|
*/ |
22
|
|
|
private $orderingConfigurations; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $totalCountNeeded; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int|null |
31
|
|
|
*/ |
32
|
|
|
private $maximumOffset; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable|null |
36
|
|
|
*/ |
37
|
|
|
private $itemTransformer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var callable|null |
41
|
|
|
*/ |
42
|
|
|
private $queryModifier; |
43
|
|
|
|
44
|
48 |
|
public function __construct(QueryBuilder $queryBuilder) |
45
|
|
|
{ |
46
|
48 |
|
$this->queryBuilder = $queryBuilder; |
47
|
48 |
|
$this->orderingConfigurations = []; |
48
|
48 |
|
$this->totalCountNeeded = false; |
49
|
48 |
|
} |
50
|
|
|
|
51
|
32 |
|
public function addOrderingConfiguration(string $orderBy, OrderingConfiguration $configuration): self |
52
|
|
|
{ |
53
|
32 |
|
if ($configuration->getAccessorPath() === null && $configuration->getAccessorClosure() === null) { |
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
'Must set either accessorPath or accessorClosure for every OrderingConfiguration' |
56
|
|
|
); |
57
|
|
|
} |
58
|
32 |
|
$this->orderingConfigurations[$orderBy] = $configuration; |
59
|
32 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array|OrderingConfiguration[] $orderingConfigurations array of `orderBy => OrderingConfiguration` pairs |
64
|
|
|
* @return ConfiguredQuery |
65
|
|
|
*/ |
66
|
28 |
|
public function addOrderingConfigurations(array $orderingConfigurations): self |
67
|
|
|
{ |
68
|
28 |
|
foreach ($orderingConfigurations as $orderBy => $configuration) { |
69
|
28 |
|
$this->addOrderingConfiguration($orderBy, $configuration); |
70
|
|
|
} |
71
|
|
|
|
72
|
28 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
31 |
|
public function getOrderingConfigurationFor(string $orderBy): OrderingConfiguration |
76
|
|
|
{ |
77
|
31 |
|
if (!isset($this->orderingConfigurations[$orderBy])) { |
78
|
1 |
|
throw new InvalidOrderByException($orderBy); |
79
|
|
|
} |
80
|
|
|
|
81
|
30 |
|
return $this->orderingConfigurations[$orderBy]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return QueryBuilder |
86
|
|
|
*/ |
87
|
48 |
|
public function getQueryBuilder(): QueryBuilder |
88
|
|
|
{ |
89
|
48 |
|
return $this->queryBuilder; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
36 |
|
public function isTotalCountNeeded(): bool |
96
|
|
|
{ |
97
|
36 |
|
return $this->totalCountNeeded; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param bool $totalCountNeeded |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
23 |
|
public function setTotalCountNeeded(bool $totalCountNeeded): self |
105
|
|
|
{ |
106
|
23 |
|
$this->totalCountNeeded = $totalCountNeeded; |
107
|
23 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $maximumOffset |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
1 |
|
public function setMaximumOffset(int $maximumOffset): self |
115
|
|
|
{ |
116
|
1 |
|
$this->maximumOffset = $maximumOffset; |
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
public function hasMaximumOffset(): bool |
121
|
|
|
{ |
122
|
6 |
|
return $this->maximumOffset !== null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int |
127
|
|
|
* @throws RuntimeException if maximum offset was not set. Check with hasMaximumOffset beforehand |
128
|
|
|
*/ |
129
|
1 |
|
public function getMaximumOffset(): int |
130
|
|
|
{ |
131
|
1 |
|
if ($this->maximumOffset === null) { |
132
|
|
|
throw new RuntimeException('Maximum offset was not set'); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return $this->maximumOffset; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return callable|null |
140
|
|
|
*/ |
141
|
36 |
|
public function getItemTransformer() |
142
|
|
|
{ |
143
|
36 |
|
return $this->itemTransformer; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function setItemTransformer(callable $itemTransformer): self |
147
|
|
|
{ |
148
|
1 |
|
$this->itemTransformer = $itemTransformer; |
149
|
|
|
|
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return callable|null |
155
|
|
|
*/ |
156
|
43 |
|
public function getQueryModifier() |
157
|
|
|
{ |
158
|
43 |
|
return $this->queryModifier; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
public function setQueryModifier(callable $queryModifier): self |
162
|
|
|
{ |
163
|
1 |
|
$this->queryModifier = $queryModifier; |
164
|
|
|
|
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|