Security update to Drupal 8.4.6
[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 = [];
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(
141             CompositeExpression::TYPE_AND,
142             [$this->expression, $expression]
143         );
144
145         return $this;
146     }
147
148     /**
149      * Appends the where expression to evaluate when this Criteria is searched for
150      * using an OR with previous expression.
151      *
152      * @param Expression $expression
153      *
154      * @return Criteria
155      */
156     public function orWhere(Expression $expression)
157     {
158         if ($this->expression === null) {
159             return $this->where($expression);
160         }
161
162         $this->expression = new CompositeExpression(
163             CompositeExpression::TYPE_OR,
164             [$this->expression, $expression]
165         );
166
167         return $this;
168     }
169
170     /**
171      * Gets the expression attached to this Criteria.
172      *
173      * @return Expression|null
174      */
175     public function getWhereExpression()
176     {
177         return $this->expression;
178     }
179
180     /**
181      * Gets the current orderings of this Criteria.
182      *
183      * @return string[]
184      */
185     public function getOrderings()
186     {
187         return $this->orderings;
188     }
189
190     /**
191      * Sets the ordering of the result of this Criteria.
192      *
193      * Keys are field and values are the order, being either ASC or DESC.
194      *
195      * @see Criteria::ASC
196      * @see Criteria::DESC
197      *
198      * @param string[] $orderings
199      *
200      * @return Criteria
201      */
202     public function orderBy(array $orderings)
203     {
204         $this->orderings = array_map(
205             function (string $ordering) : string {
206                 return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
207             },
208             $orderings
209         );
210
211         return $this;
212     }
213
214     /**
215      * Gets the current first result option of this Criteria.
216      *
217      * @return int|null
218      */
219     public function getFirstResult()
220     {
221         return $this->firstResult;
222     }
223
224     /**
225      * Set the number of first result that this Criteria should return.
226      *
227      * @param int|null $firstResult The value to set.
228      *
229      * @return Criteria
230      */
231     public function setFirstResult($firstResult)
232     {
233         $this->firstResult = null === $firstResult ? null : (int) $firstResult;
234
235         return $this;
236     }
237
238     /**
239      * Gets maxResults.
240      *
241      * @return int|null
242      */
243     public function getMaxResults()
244     {
245         return $this->maxResults;
246     }
247
248     /**
249      * Sets maxResults.
250      *
251      * @param int|null $maxResults The value to set.
252      *
253      * @return Criteria
254      */
255     public function setMaxResults($maxResults)
256     {
257         $this->maxResults = null === $maxResults ? null : (int) $maxResults;
258
259         return $this;
260     }
261 }