ExpressionBuilder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use Doctrine\Common\Collections\Expr\Comparison;
  4. use Doctrine\Common\Collections\Expr\CompositeExpression;
  5. use Doctrine\Common\Collections\Expr\Value;
  6. use function func_get_args;
  7. /**
  8. * Builder for Expressions in the {@link Selectable} interface.
  9. *
  10. * Important Notice for interoperable code: You have to use scalar
  11. * values only for comparisons, otherwise the behavior of the comparison
  12. * may be different between implementations (Array vs ORM vs ODM).
  13. */
  14. class ExpressionBuilder
  15. {
  16. /**
  17. * @param mixed ...$x
  18. *
  19. * @return CompositeExpression
  20. */
  21. public function andX($x = null)
  22. {
  23. return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
  24. }
  25. /**
  26. * @param mixed ...$x
  27. *
  28. * @return CompositeExpression
  29. */
  30. public function orX($x = null)
  31. {
  32. return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
  33. }
  34. /**
  35. * @param string $field
  36. * @param mixed $value
  37. *
  38. * @return Comparison
  39. */
  40. public function eq($field, $value)
  41. {
  42. return new Comparison($field, Comparison::EQ, new Value($value));
  43. }
  44. /**
  45. * @param string $field
  46. * @param mixed $value
  47. *
  48. * @return Comparison
  49. */
  50. public function gt($field, $value)
  51. {
  52. return new Comparison($field, Comparison::GT, new Value($value));
  53. }
  54. /**
  55. * @param string $field
  56. * @param mixed $value
  57. *
  58. * @return Comparison
  59. */
  60. public function lt($field, $value)
  61. {
  62. return new Comparison($field, Comparison::LT, new Value($value));
  63. }
  64. /**
  65. * @param string $field
  66. * @param mixed $value
  67. *
  68. * @return Comparison
  69. */
  70. public function gte($field, $value)
  71. {
  72. return new Comparison($field, Comparison::GTE, new Value($value));
  73. }
  74. /**
  75. * @param string $field
  76. * @param mixed $value
  77. *
  78. * @return Comparison
  79. */
  80. public function lte($field, $value)
  81. {
  82. return new Comparison($field, Comparison::LTE, new Value($value));
  83. }
  84. /**
  85. * @param string $field
  86. * @param mixed $value
  87. *
  88. * @return Comparison
  89. */
  90. public function neq($field, $value)
  91. {
  92. return new Comparison($field, Comparison::NEQ, new Value($value));
  93. }
  94. /**
  95. * @param string $field
  96. *
  97. * @return Comparison
  98. */
  99. public function isNull($field)
  100. {
  101. return new Comparison($field, Comparison::EQ, new Value(null));
  102. }
  103. /**
  104. * @param string $field
  105. * @param array $values
  106. *
  107. * @return Comparison
  108. */
  109. public function in($field, array $values)
  110. {
  111. return new Comparison($field, Comparison::IN, new Value($values));
  112. }
  113. /**
  114. * @param string $field
  115. * @param array $values
  116. *
  117. * @return Comparison
  118. */
  119. public function notIn($field, array $values)
  120. {
  121. return new Comparison($field, Comparison::NIN, new Value($values));
  122. }
  123. /**
  124. * @param string $field
  125. * @param mixed $value
  126. *
  127. * @return Comparison
  128. */
  129. public function contains($field, $value)
  130. {
  131. return new Comparison($field, Comparison::CONTAINS, new Value($value));
  132. }
  133. /**
  134. * @param string $field
  135. * @param mixed $value
  136. *
  137. * @return Comparison
  138. */
  139. public function memberOf($field, $value)
  140. {
  141. return new Comparison($field, Comparison::MEMBER_OF, new Value($value));
  142. }
  143. /**
  144. * @param string $field
  145. * @param mixed $value
  146. *
  147. * @return Comparison
  148. */
  149. public function startsWith($field, $value)
  150. {
  151. return new Comparison($field, Comparison::STARTS_WITH, new Value($value));
  152. }
  153. /**
  154. * @param string $field
  155. * @param mixed $value
  156. *
  157. * @return Comparison
  158. */
  159. public function endsWith($field, $value)
  160. {
  161. return new Comparison($field, Comparison::ENDS_WITH, new Value($value));
  162. }
  163. }