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