Criteria.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 string[]
  48. */
  49. private $orderings = array();
  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 string[]|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->setFirstResult($firstResult);
  91. $this->setMaxResults($maxResults);
  92. if (null !== $orderings) {
  93. $this->orderBy($orderings);
  94. }
  95. }
  96. /**
  97. * Sets the where expression to evaluate when this Criteria is searched for.
  98. *
  99. * @param Expression $expression
  100. *
  101. * @return Criteria
  102. */
  103. public function where(Expression $expression)
  104. {
  105. $this->expression = $expression;
  106. return $this;
  107. }
  108. /**
  109. * Appends the where expression to evaluate when this Criteria is searched for
  110. * using an AND with previous expression.
  111. *
  112. * @param Expression $expression
  113. *
  114. * @return Criteria
  115. */
  116. public function andWhere(Expression $expression)
  117. {
  118. if ($this->expression === null) {
  119. return $this->where($expression);
  120. }
  121. $this->expression = new CompositeExpression(CompositeExpression::TYPE_AND, array(
  122. $this->expression, $expression
  123. ));
  124. return $this;
  125. }
  126. /**
  127. * Appends the where expression to evaluate when this Criteria is searched for
  128. * using an OR with previous expression.
  129. *
  130. * @param Expression $expression
  131. *
  132. * @return Criteria
  133. */
  134. public function orWhere(Expression $expression)
  135. {
  136. if ($this->expression === null) {
  137. return $this->where($expression);
  138. }
  139. $this->expression = new CompositeExpression(CompositeExpression::TYPE_OR, array(
  140. $this->expression, $expression
  141. ));
  142. return $this;
  143. }
  144. /**
  145. * Gets the expression attached to this Criteria.
  146. *
  147. * @return Expression|null
  148. */
  149. public function getWhereExpression()
  150. {
  151. return $this->expression;
  152. }
  153. /**
  154. * Gets the current orderings of this Criteria.
  155. *
  156. * @return string[]
  157. */
  158. public function getOrderings()
  159. {
  160. return $this->orderings;
  161. }
  162. /**
  163. * Sets the ordering of the result of this Criteria.
  164. *
  165. * Keys are field and values are the order, being either ASC or DESC.
  166. *
  167. * @see Criteria::ASC
  168. * @see Criteria::DESC
  169. *
  170. * @param string[] $orderings
  171. *
  172. * @return Criteria
  173. */
  174. public function orderBy(array $orderings)
  175. {
  176. $this->orderings = array_map(
  177. function ($ordering) {
  178. return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC;
  179. },
  180. $orderings
  181. );
  182. return $this;
  183. }
  184. /**
  185. * Gets the current first result option of this Criteria.
  186. *
  187. * @return int|null
  188. */
  189. public function getFirstResult()
  190. {
  191. return $this->firstResult;
  192. }
  193. /**
  194. * Set the number of first result that this Criteria should return.
  195. *
  196. * @param int|null $firstResult The value to set.
  197. *
  198. * @return Criteria
  199. */
  200. public function setFirstResult($firstResult)
  201. {
  202. $this->firstResult = null === $firstResult ? null : (int) $firstResult;
  203. return $this;
  204. }
  205. /**
  206. * Gets maxResults.
  207. *
  208. * @return int|null
  209. */
  210. public function getMaxResults()
  211. {
  212. return $this->maxResults;
  213. }
  214. /**
  215. * Sets maxResults.
  216. *
  217. * @param int|null $maxResults The value to set.
  218. *
  219. * @return Criteria
  220. */
  221. public function setMaxResults($maxResults)
  222. {
  223. $this->maxResults = null === $maxResults ? null : (int) $maxResults;
  224. return $this;
  225. }
  226. }