/
Criteria.php
222 lines (188 loc) · 4.8 KB
1
2
3
4
<?php
namespace Doctrine\Common\Collections;
5
use Doctrine\Common\Collections\Expr\CompositeExpression;
6
7
8
use Doctrine\Common\Collections\Expr\Expression;
use function array_map;
use function strtoupper;
9
10
11
12
13
14
/**
* Criteria for filtering Selectable collections.
*/
class Criteria
{
15
public const ASC = 'ASC';
16
17
public const DESC = 'DESC';
18
19
/** @var ExpressionBuilder|null */
20
21
private static $expressionBuilder;
22
/** @var Expression|null */
23
24
private $expression;
25
/** @var string[] */
26
private $orderings = [];
27
28
/** @var int|null */
29
30
private $firstResult;
31
/** @var int|null */
32
33
private $maxResults;
34
35
36
37
38
39
40
41
42
43
/**
* Creates an instance of the class.
*
* @return Criteria
*/
public static function create()
{
return new static();
}
44
/**
45
* Returns the expression builder.
46
*
47
* @return ExpressionBuilder
48
49
50
51
52
53
*/
public static function expr()
{
if (self::$expressionBuilder === null) {
self::$expressionBuilder = new ExpressionBuilder();
}
54
55
56
57
return self::$expressionBuilder;
}
58
/**
59
* Construct a new Criteria.
60
*
61
62
63
* @param string[]|null $orderings
* @param int|null $firstResult
* @param int|null $maxResults
64
*/
65
public function __construct(?Expression $expression = null, ?array $orderings = null, $firstResult = null, $maxResults = null)
66
{
67
$this->expression = $expression;
68
69
70
$this->setFirstResult($firstResult);
$this->setMaxResults($maxResults);
71
72
73
if ($orderings === null) {
return;
74
}
75
76
$this->orderBy($orderings);
77
78
79
}
/**
80
* Sets the where expression to evaluate when this Criteria is searched for.
81
82
83
84
85
86
*
* @return Criteria
*/
public function where(Expression $expression)
{
$this->expression = $expression;
87
88
89
90
return $this;
}
91
/**
92
* Appends the where expression to evaluate when this Criteria is searched for
93
94
95
96
97
98
99
100
101
102
* using an AND with previous expression.
*
* @return Criteria
*/
public function andWhere(Expression $expression)
{
if ($this->expression === null) {
return $this->where($expression);
}
103
104
105
106
$this->expression = new CompositeExpression(
CompositeExpression::TYPE_AND,
[$this->expression, $expression]
);
107
108
109
110
111
return $this;
}
/**
112
* Appends the where expression to evaluate when this Criteria is searched for
113
114
115
116
117
118
119
120
121
122
* using an OR with previous expression.
*
* @return Criteria
*/
public function orWhere(Expression $expression)
{
if ($this->expression === null) {
return $this->where($expression);
}
123
124
125
126
$this->expression = new CompositeExpression(
CompositeExpression::TYPE_OR,
[$this->expression, $expression]
);
127
128
129
130
return $this;
}
131
/**
132
* Gets the expression attached to this Criteria.
133
134
135
136
137
138
139
140
*
* @return Expression|null
*/
public function getWhereExpression()
{
return $this->expression;
}
141
/**
142
* Gets the current orderings of this Criteria.
143
*
144
* @return string[]
145
*/
146
147
148
149
150
151
public function getOrderings()
{
return $this->orderings;
}
/**
152
* Sets the ordering of the result of this Criteria.
153
154
155
156
157
158
*
* Keys are field and values are the order, being either ASC or DESC.
*
* @see Criteria::ASC
* @see Criteria::DESC
*
159
* @param string[] $orderings
160
*
161
162
163
164
* @return Criteria
*/
public function orderBy(array $orderings)
{
165
$this->orderings = array_map(
166
static function (string $ordering) : string {
167
return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
168
169
170
},
$orderings
);
171
172
173
174
175
return $this;
}
/**
176
* Gets the current first result option of this Criteria.
177
*
178
* @return int|null
179
180
181
182
183
184
185
*/
public function getFirstResult()
{
return $this->firstResult;
}
/**
186
187
188
* Set the number of first result that this Criteria should return.
*
* @param int|null $firstResult The value to set.
189
*
190
* @return Criteria
191
192
193
*/
public function setFirstResult($firstResult)
{
194
$this->firstResult = $firstResult === null ? null : (int) $firstResult;
195
196
return $this;
197
198
199
}
/**
200
* Gets maxResults.
201
*
202
* @return int|null
203
204
205
206
207
208
209
*/
public function getMaxResults()
{
return $this->maxResults;
}
/**
210
211
212
* Sets maxResults.
*
* @param int|null $maxResults The value to set.
213
*
214
* @return Criteria
215
216
217
*/
public function setMaxResults($maxResults)
{
218
$this->maxResults = $maxResults === null ? null : (int) $maxResults;
219
220
return $this;
221
222
}
}