ExpressionBuilder.php 4.9 KB

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