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 = array();
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(CompositeExpression::TYPE_AND, array(
141 $this->expression, $expression
148 * Appends the where expression to evaluate when this Criteria is searched for
149 * using an OR with previous expression.
151 * @param Expression $expression
155 public function orWhere(Expression $expression)
157 if ($this->expression === null) {
158 return $this->where($expression);
161 $this->expression = new CompositeExpression(CompositeExpression::TYPE_OR, array(
162 $this->expression, $expression
169 * Gets the expression attached to this Criteria.
171 * @return Expression|null
173 public function getWhereExpression()
175 return $this->expression;
179 * Gets the current orderings of this Criteria.
183 public function getOrderings()
185 return $this->orderings;
189 * Sets the ordering of the result of this Criteria.
191 * Keys are field and values are the order, being either ASC or DESC.
194 * @see Criteria::DESC
196 * @param string[] $orderings
200 public function orderBy(array $orderings)
202 $this->orderings = array_map(
203 function ($ordering) {
204 return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
213 * Gets the current first result option of this Criteria.
217 public function getFirstResult()
219 return $this->firstResult;
223 * Set the number of first result that this Criteria should return.
225 * @param int|null $firstResult The value to set.
229 public function setFirstResult($firstResult)
231 $this->firstResult = null === $firstResult ? null : (int) $firstResult;
241 public function getMaxResults()
243 return $this->maxResults;
249 * @param int|null $maxResults The value to set.
253 public function setMaxResults($maxResults)
255 $this->maxResults = null === $maxResults ? null : (int) $maxResults;