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