Criteria.php 5.5 KB

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