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