Criteria.php 4.8 KB

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