summaryrefslogtreecommitdiff
path: root/vendor/doctrine/collections/src/Criteria.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/collections/src/Criteria.php')
-rw-r--r--vendor/doctrine/collections/src/Criteria.php285
1 files changed, 285 insertions, 0 deletions
diff --git a/vendor/doctrine/collections/src/Criteria.php b/vendor/doctrine/collections/src/Criteria.php
new file mode 100644
index 0000000..4c8a0a7
--- /dev/null
+++ b/vendor/doctrine/collections/src/Criteria.php
@@ -0,0 +1,285 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Common\Collections;
6
7use Doctrine\Common\Collections\Expr\CompositeExpression;
8use Doctrine\Common\Collections\Expr\Expression;
9use Doctrine\Deprecations\Deprecation;
10
11use function array_map;
12use function func_num_args;
13use function strtoupper;
14
15/**
16 * Criteria for filtering Selectable collections.
17 *
18 * @psalm-consistent-constructor
19 */
20class Criteria
21{
22 /** @deprecated use Order::Ascending instead */
23 final public const ASC = 'ASC';
24
25 /** @deprecated use Order::Descending instead */
26 final public const DESC = 'DESC';
27
28 private static ExpressionBuilder|null $expressionBuilder = null;
29
30 /** @var array<string, Order> */
31 private array $orderings = [];
32
33 private int|null $firstResult = null;
34 private int|null $maxResults = null;
35
36 /**
37 * Creates an instance of the class.
38 *
39 * @return static
40 */
41 public static function create()
42 {
43 return new static();
44 }
45
46 /**
47 * Returns the expression builder.
48 *
49 * @return ExpressionBuilder
50 */
51 public static function expr()
52 {
53 if (self::$expressionBuilder === null) {
54 self::$expressionBuilder = new ExpressionBuilder();
55 }
56
57 return self::$expressionBuilder;
58 }
59
60 /**
61 * Construct a new Criteria.
62 *
63 * @param array<string, string|Order>|null $orderings
64 */
65 public function __construct(
66 private Expression|null $expression = null,
67 array|null $orderings = null,
68 int|null $firstResult = null,
69 int|null $maxResults = null,
70 ) {
71 $this->expression = $expression;
72
73 if ($firstResult === null && func_num_args() > 2) {
74 Deprecation::trigger(
75 'doctrine/collections',
76 'https://github.com/doctrine/collections/pull/311',
77 'Passing null as $firstResult to the constructor of %s is deprecated. Pass 0 instead or omit the argument.',
78 self::class,
79 );
80 }
81
82 $this->setFirstResult($firstResult);
83 $this->setMaxResults($maxResults);
84
85 if ($orderings === null) {
86 return;
87 }
88
89 $this->orderBy($orderings);
90 }
91
92 /**
93 * Sets the where expression to evaluate when this Criteria is searched for.
94 *
95 * @return $this
96 */
97 public function where(Expression $expression)
98 {
99 $this->expression = $expression;
100
101 return $this;
102 }
103
104 /**
105 * Appends the where expression to evaluate when this Criteria is searched for
106 * using an AND with previous expression.
107 *
108 * @return $this
109 */
110 public function andWhere(Expression $expression)
111 {
112 if ($this->expression === null) {
113 return $this->where($expression);
114 }
115
116 $this->expression = new CompositeExpression(
117 CompositeExpression::TYPE_AND,
118 [$this->expression, $expression],
119 );
120
121 return $this;
122 }
123
124 /**
125 * Appends the where expression to evaluate when this Criteria is searched for
126 * using an OR with previous expression.
127 *
128 * @return $this
129 */
130 public function orWhere(Expression $expression)
131 {
132 if ($this->expression === null) {
133 return $this->where($expression);
134 }
135
136 $this->expression = new CompositeExpression(
137 CompositeExpression::TYPE_OR,
138 [$this->expression, $expression],
139 );
140
141 return $this;
142 }
143
144 /**
145 * Gets the expression attached to this Criteria.
146 *
147 * @return Expression|null
148 */
149 public function getWhereExpression()
150 {
151 return $this->expression;
152 }
153
154 /**
155 * Gets the current orderings of this Criteria.
156 *
157 * @deprecated use orderings() instead
158 *
159 * @return array<string, string>
160 */
161 public function getOrderings()
162 {
163 Deprecation::trigger(
164 'doctrine/collections',
165 'https://github.com/doctrine/collections/pull/389',
166 'Calling %s() is deprecated. Use %s::orderings() instead.',
167 __METHOD__,
168 self::class,
169 );
170
171 return array_map(
172 static fn (Order $ordering): string => $ordering->value,
173 $this->orderings,
174 );
175 }
176
177 /**
178 * Gets the current orderings of this Criteria.
179 *
180 * @return array<string, Order>
181 */
182 public function orderings(): array
183 {
184 return $this->orderings;
185 }
186
187 /**
188 * Sets the ordering of the result of this Criteria.
189 *
190 * Keys are field and values are the order, being a valid Order enum case.
191 *
192 * @see Order::Ascending
193 * @see Order::Descending
194 *
195 * @param array<string, string|Order> $orderings
196 *
197 * @return $this
198 */
199 public function orderBy(array $orderings)
200 {
201 $method = __METHOD__;
202 $this->orderings = array_map(
203 static function (string|Order $ordering) use ($method): Order {
204 if ($ordering instanceof Order) {
205 return $ordering;
206 }
207
208 static $triggered = false;
209
210 if (! $triggered) {
211 Deprecation::trigger(
212 'doctrine/collections',
213 'https://github.com/doctrine/collections/pull/389',
214 'Passing non-Order enum values to %s() is deprecated. Pass Order enum values instead.',
215 $method,
216 );
217 }
218
219 $triggered = true;
220
221 return strtoupper($ordering) === Order::Ascending->value ? Order::Ascending : Order::Descending;
222 },
223 $orderings,
224 );
225
226 return $this;
227 }
228
229 /**
230 * Gets the current first result option of this Criteria.
231 *
232 * @return int|null
233 */
234 public function getFirstResult()
235 {
236 return $this->firstResult;
237 }
238
239 /**
240 * Set the number of first result that this Criteria should return.
241 *
242 * @param int|null $firstResult The value to set.
243 *
244 * @return $this
245 */
246 public function setFirstResult(int|null $firstResult)
247 {
248 if ($firstResult === null) {
249 Deprecation::triggerIfCalledFromOutside(
250 'doctrine/collections',
251 'https://github.com/doctrine/collections/pull/311',
252 'Passing null to %s() is deprecated, pass 0 instead.',
253 __METHOD__,
254 );
255 }
256
257 $this->firstResult = $firstResult;
258
259 return $this;
260 }
261
262 /**
263 * Gets maxResults.
264 *
265 * @return int|null
266 */
267 public function getMaxResults()
268 {
269 return $this->maxResults;
270 }
271
272 /**
273 * Sets maxResults.
274 *
275 * @param int|null $maxResults The value to set.
276 *
277 * @return $this
278 */
279 public function setMaxResults(int|null $maxResults)
280 {
281 $this->maxResults = $maxResults;
282
283 return $this;
284 }
285}