Skip to content

Latest commit

 

History

History
222 lines (188 loc) · 4.8 KB

Criteria.php

File metadata and controls

222 lines (188 loc) · 4.8 KB
 
1
2
3
4
<?php
namespace Doctrine\Common\Collections;
Jul 31, 2012
Jul 31, 2012
5
use Doctrine\Common\Collections\Expr\CompositeExpression;
Jan 29, 2018
Jan 29, 2018
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
{
Jan 29, 2018
Jan 29, 2018
15
public const ASC = 'ASC';
Jan 29, 2018
Jan 29, 2018
17
public const DESC = 'DESC';
Mar 5, 2018
Mar 5, 2018
19
/** @var ExpressionBuilder|null */
Aug 1, 2012
Aug 1, 2012
20
21
private static $expressionBuilder;
Mar 5, 2018
Mar 5, 2018
22
/** @var Expression|null */
23
24
private $expression;
Mar 5, 2018
Mar 5, 2018
25
/** @var string[] */
May 20, 2017
May 20, 2017
26
private $orderings = [];
Mar 5, 2018
Mar 5, 2018
28
/** @var int|null */
29
30
private $firstResult;
Mar 5, 2018
Mar 5, 2018
31
/** @var int|null */
32
33
private $maxResults;
Jul 31, 2012
Jul 31, 2012
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();
}
Aug 1, 2012
Aug 1, 2012
44
/**
Jan 20, 2013
Jan 20, 2013
45
* Returns the expression builder.
Sep 25, 2018
Sep 25, 2018
46
*
Mar 5, 2018
Mar 5, 2018
47
* @return ExpressionBuilder
Aug 1, 2012
Aug 1, 2012
48
49
50
51
52
53
*/
public static function expr()
{
if (self::$expressionBuilder === null) {
self::$expressionBuilder = new ExpressionBuilder();
}
May 7, 2014
May 7, 2014
54
Aug 1, 2012
Aug 1, 2012
55
56
57
return self::$expressionBuilder;
}
Jan 20, 2013
Jan 20, 2013
59
* Construct a new Criteria.
May 7, 2014
May 7, 2014
61
62
63
* @param string[]|null $orderings
* @param int|null $firstResult
* @param int|null $maxResults
Jan 29, 2018
Jan 29, 2018
65
public function __construct(?Expression $expression = null, ?array $orderings = null, $firstResult = null, $maxResults = null)
May 7, 2014
May 7, 2014
67
$this->expression = $expression;
May 7, 2014
May 7, 2014
68
69
70
$this->setFirstResult($firstResult);
$this->setMaxResults($maxResults);
May 7, 2014
May 7, 2014
71
Mar 5, 2018
Mar 5, 2018
72
73
if ($orderings === null) {
return;
May 7, 2014
May 7, 2014
74
}
Mar 5, 2018
Mar 5, 2018
75
76
$this->orderBy($orderings);
Jan 20, 2013
Jan 20, 2013
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;
May 7, 2014
May 7, 2014
87
Jul 31, 2012
Jul 31, 2012
91
/**
Jan 20, 2013
Jan 20, 2013
92
* Appends the where expression to evaluate when this Criteria is searched for
Jul 31, 2012
Jul 31, 2012
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);
}
May 20, 2017
May 20, 2017
103
104
105
106
$this->expression = new CompositeExpression(
CompositeExpression::TYPE_AND,
[$this->expression, $expression]
);
Jul 31, 2012
Jul 31, 2012
107
108
109
110
111
return $this;
}
/**
Jan 20, 2013
Jan 20, 2013
112
* Appends the where expression to evaluate when this Criteria is searched for
Jul 31, 2012
Jul 31, 2012
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);
}
May 20, 2017
May 20, 2017
123
124
125
126
$this->expression = new CompositeExpression(
CompositeExpression::TYPE_OR,
[$this->expression, $expression]
);
Jul 31, 2012
Jul 31, 2012
127
128
129
130
return $this;
}
Jan 20, 2013
Jan 20, 2013
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;
}
Jun 18, 2012
Jun 18, 2012
141
/**
Jan 20, 2013
Jan 20, 2013
142
* Gets the current orderings of this Criteria.
Jun 18, 2012
Jun 18, 2012
143
*
May 7, 2014
May 7, 2014
144
* @return string[]
Jun 18, 2012
Jun 18, 2012
145
*/
146
147
148
149
150
151
public function getOrderings()
{
return $this->orderings;
}
/**
Jan 20, 2013
Jan 20, 2013
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
*
May 7, 2014
May 7, 2014
159
* @param string[] $orderings
Jan 20, 2013
Jan 20, 2013
160
*
161
162
163
164
* @return Criteria
*/
public function orderBy(array $orderings)
{
May 7, 2014
May 7, 2014
165
$this->orderings = array_map(
Sep 25, 2018
Sep 25, 2018
166
static function (string $ordering) : string {
May 7, 2014
May 7, 2014
167
return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
May 7, 2014
May 7, 2014
168
169
170
},
$orderings
);
May 7, 2014
May 7, 2014
171
172
173
174
175
return $this;
}
/**
Jan 20, 2013
Jan 20, 2013
176
* Gets the current first result option of this Criteria.
Jan 20, 2013
Jan 20, 2013
178
* @return int|null
179
180
181
182
183
184
185
*/
public function getFirstResult()
{
return $this->firstResult;
}
/**
Jan 20, 2013
Jan 20, 2013
186
187
188
* Set the number of first result that this Criteria should return.
*
* @param int|null $firstResult The value to set.
Jun 18, 2012
Jun 18, 2012
190
* @return Criteria
191
192
193
*/
public function setFirstResult($firstResult)
{
Jan 29, 2018
Jan 29, 2018
194
$this->firstResult = $firstResult === null ? null : (int) $firstResult;
May 7, 2014
May 7, 2014
195
Jun 18, 2012
Jun 18, 2012
196
return $this;
Jan 20, 2013
Jan 20, 2013
200
* Gets maxResults.
Jan 20, 2013
Jan 20, 2013
202
* @return int|null
203
204
205
206
207
208
209
*/
public function getMaxResults()
{
return $this->maxResults;
}
/**
Jan 20, 2013
Jan 20, 2013
210
211
212
* Sets maxResults.
*
* @param int|null $maxResults The value to set.
Jun 18, 2012
Jun 18, 2012
214
* @return Criteria
215
216
217
*/
public function setMaxResults($maxResults)
{
Jan 29, 2018
Jan 29, 2018
218
$this->maxResults = $maxResults === null ? null : (int) $maxResults;
May 7, 2014
May 7, 2014
219
Jun 18, 2012
Jun 18, 2012
220
return $this;