Criteria.php 4.8 KB

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