3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Collections;
22 use Doctrine\Common\Collections\Expr\Expression;
23 use Doctrine\Common\Collections\Expr\CompositeExpression;
26 * Criteria for filtering Selectable collections.
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
44 * @var \Doctrine\Common\Collections\ExpressionBuilder|null
46 private static $expressionBuilder;
49 * @var \Doctrine\Common\Collections\Expr\Expression|null
56 private $orderings = [];
69 * Creates an instance of the class.
73 public static function create()
79 * Returns the expression builder.
81 * @return \Doctrine\Common\Collections\ExpressionBuilder
83 public static function expr()
85 if (self::$expressionBuilder === null) {
86 self::$expressionBuilder = new ExpressionBuilder();
89 return self::$expressionBuilder;
93 * Construct a new Criteria.
95 * @param Expression $expression
96 * @param string[]|null $orderings
97 * @param int|null $firstResult
98 * @param int|null $maxResults
100 public function __construct(Expression $expression = null, array $orderings = null, $firstResult = null, $maxResults = null)
102 $this->expression = $expression;
104 $this->setFirstResult($firstResult);
105 $this->setMaxResults($maxResults);
107 if (null !== $orderings) {
108 $this->orderBy($orderings);
113 * Sets the where expression to evaluate when this Criteria is searched for.
115 * @param Expression $expression
119 public function where(Expression $expression)
121 $this->expression = $expression;
127 * Appends the where expression to evaluate when this Criteria is searched for
128 * using an AND with previous expression.
130 * @param Expression $expression
134 public function andWhere(Expression $expression)
136 if ($this->expression === null) {
137 return $this->where($expression);
140 $this->expression = new CompositeExpression(
141 CompositeExpression::TYPE_AND,
142 [$this->expression, $expression]
149 * Appends the where expression to evaluate when this Criteria is searched for
150 * using an OR with previous expression.
152 * @param Expression $expression
156 public function orWhere(Expression $expression)
158 if ($this->expression === null) {
159 return $this->where($expression);
162 $this->expression = new CompositeExpression(
163 CompositeExpression::TYPE_OR,
164 [$this->expression, $expression]
171 * Gets the expression attached to this Criteria.
173 * @return Expression|null
175 public function getWhereExpression()
177 return $this->expression;
181 * Gets the current orderings of this Criteria.
185 public function getOrderings()
187 return $this->orderings;
191 * Sets the ordering of the result of this Criteria.
193 * Keys are field and values are the order, being either ASC or DESC.
196 * @see Criteria::DESC
198 * @param string[] $orderings
202 public function orderBy(array $orderings)
204 $this->orderings = array_map(
205 function (string $ordering) : string {
206 return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
215 * Gets the current first result option of this Criteria.
219 public function getFirstResult()
221 return $this->firstResult;
225 * Set the number of first result that this Criteria should return.
227 * @param int|null $firstResult The value to set.
231 public function setFirstResult($firstResult)
233 $this->firstResult = null === $firstResult ? null : (int) $firstResult;
243 public function getMaxResults()
245 return $this->maxResults;
251 * @param int|null $maxResults The value to set.
255 public function setMaxResults($maxResults)
257 $this->maxResults = null === $maxResults ? null : (int) $maxResults;