Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / 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
45      */
46     private static $expressionBuilder;
47
48     /**
49      * @var \Doctrine\Common\Collections\Expr\Expression
50      */
51     private $expression;
52
53     /**
54      * @var array|null
55      */
56     private $orderings;
57
58     /**
59      * @var int
60      */
61     private $firstResult;
62
63     /**
64      * @var int
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      * Return 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         return self::$expressionBuilder;
89     }
90
91     /**
92      * Construct new criteria
93      *
94      * @param Expression $expression
95      * @param array $orderings
96      * @param int $firstResult
97      * @param int $maxResults
98      */
99     public function __construct(Expression $expression = null, array $orderings = null, $firstResult = null, $maxResults = null)
100     {
101         $this->expression  = $expression;
102         $this->orderings   = $orderings;
103         $this->firstResult = $firstResult;
104         $this->maxResults  = $maxResults;
105     }
106
107     /**
108      * Set the where expression to evaluate when this criteria is searched for.
109      *
110      * @param Expression
111      * @return Criteria
112      */
113     public function where(Expression $expression)
114     {
115         $this->expression = $expression;
116         return $this;
117     }
118
119     /**
120      * Append the where expression to evaluate when this criteria is searched for
121      * using an AND with previous expression.
122      *
123      * @param Expression
124      * @return Criteria
125      */
126     public function andWhere(Expression $expression)
127     {
128         if ($this->expression === null) {
129             return $this->where($expression);
130         }
131
132         $this->expression = new CompositeExpression(CompositeExpression::TYPE_AND, array(
133             $this->expression, $expression
134         ));
135
136         return $this;
137     }
138
139     /**
140      * Append the where expression to evaluate when this criteria is searched for
141      * using an OR with previous expression.
142      *
143      * @param Expression
144      * @return Criteria
145      */
146     public function orWhere(Expression $expression)
147     {
148         if ($this->expression === null) {
149             return $this->where($expression);
150         }
151
152         $this->expression = new CompositeExpression(CompositeExpression::TYPE_OR, array(
153             $this->expression, $expression
154         ));
155
156         return $this;
157     }
158
159     /**
160      * Get the expression attached to this criteria.
161      *
162      * @return Expression|null
163      */
164     public function getWhereExpression()
165     {
166         return $this->expression;
167     }
168
169     /**
170      * Get current orderings of this Criteria
171      *
172      * @return array
173      */
174     public function getOrderings()
175     {
176         return $this->orderings;
177     }
178
179     /**
180      * Set the ordering of the result of this criteria.
181      *
182      * Keys are field and values are the order, being either ASC or DESC.
183      *
184      * @see Criteria::ASC
185      * @see Criteria::DESC
186      *
187      * @param array
188      * @return Criteria
189      */
190     public function orderBy(array $orderings)
191     {
192         $this->orderings = $orderings;
193         return $this;
194     }
195
196     /**
197      * Get current first result option of the critera.
198      *
199      * @return firstResult.
200      */
201     public function getFirstResult()
202     {
203         return $this->firstResult;
204     }
205
206     /**
207      * Set number of first result that this criteria should return.
208      *
209      * @param firstResult the value to set.
210      * @return Criteria
211      */
212     public function setFirstResult($firstResult)
213     {
214         $this->firstResult = $firstResult;
215         return $this;
216     }
217
218     /**
219      * Get maxResults.
220      *
221      * @return maxResults.
222      */
223     public function getMaxResults()
224     {
225         return $this->maxResults;
226     }
227
228     /**
229      * Set maxResults.
230      *
231      * @param maxResults the value to set.
232      * @return Criteria
233      */
234     public function setMaxResults($maxResults)
235     {
236         $this->maxResults = $maxResults;
237         return $this;
238     }
239 }
240