Criteria.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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
  40. */
  41. private static $expressionBuilder;
  42. /**
  43. * @var \Doctrine\Common\Collections\Expr\Expression
  44. */
  45. private $expression;
  46. /**
  47. * @var array|null
  48. */
  49. private $orderings;
  50. /**
  51. * @var int
  52. */
  53. private $firstResult;
  54. /**
  55. * @var int
  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. * Return 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 new criteria
  81. *
  82. * @param Expression $expression
  83. * @param array $orderings
  84. * @param int $firstResult
  85. * @param int $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. * Set the where expression to evaluate when this criteria is searched for.
  96. *
  97. * @param Expression
  98. * @return Criteria
  99. */
  100. public function where(Expression $expression)
  101. {
  102. $this->expression = $expression;
  103. return $this;
  104. }
  105. /**
  106. * Append the where expression to evaluate when this criteria is searched for
  107. * using an AND with previous expression.
  108. *
  109. * @param Expression
  110. * @return Criteria
  111. */
  112. public function andWhere(Expression $expression)
  113. {
  114. if ($this->expression === null) {
  115. return $this->where($expression);
  116. }
  117. $this->expression = new CompositeExpression(CompositeExpression::TYPE_AND, array(
  118. $this->expression, $expression
  119. ));
  120. return $this;
  121. }
  122. /**
  123. * Append the where expression to evaluate when this criteria is searched for
  124. * using an OR with previous expression.
  125. *
  126. * @param Expression
  127. * @return Criteria
  128. */
  129. public function orWhere(Expression $expression)
  130. {
  131. if ($this->expression === null) {
  132. return $this->where($expression);
  133. }
  134. $this->expression = new CompositeExpression(CompositeExpression::TYPE_OR, array(
  135. $this->expression, $expression
  136. ));
  137. return $this;
  138. }
  139. /**
  140. * Get the expression attached to this criteria.
  141. *
  142. * @return Expression|null
  143. */
  144. public function getWhereExpression()
  145. {
  146. return $this->expression;
  147. }
  148. /**
  149. * Get current orderings of this Criteria
  150. *
  151. * @return array
  152. */
  153. public function getOrderings()
  154. {
  155. return $this->orderings;
  156. }
  157. /**
  158. * Set the ordering of the result of this criteria.
  159. *
  160. * Keys are field and values are the order, being either ASC or DESC.
  161. *
  162. * @see Criteria::ASC
  163. * @see Criteria::DESC
  164. *
  165. * @param array
  166. * @return Criteria
  167. */
  168. public function orderBy(array $orderings)
  169. {
  170. $this->orderings = $orderings;
  171. return $this;
  172. }
  173. /**
  174. * Get current first result option of the critera.
  175. *
  176. * @return firstResult.
  177. */
  178. public function getFirstResult()
  179. {
  180. return $this->firstResult;
  181. }
  182. /**
  183. * Set number of first result that this criteria should return.
  184. *
  185. * @param firstResult the value to set.
  186. * @return Criteria
  187. */
  188. public function setFirstResult($firstResult)
  189. {
  190. $this->firstResult = $firstResult;
  191. return $this;
  192. }
  193. /**
  194. * Get maxResults.
  195. *
  196. * @return maxResults.
  197. */
  198. public function getMaxResults()
  199. {
  200. return $this->maxResults;
  201. }
  202. /**
  203. * Set maxResults.
  204. *
  205. * @param maxResults the value to set.
  206. * @return Criteria
  207. */
  208. public function setMaxResults($maxResults)
  209. {
  210. $this->maxResults = $maxResults;
  211. return $this;
  212. }
  213. }