Criteria.php 5.8 KB

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