ExpressionBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @since 2.3
  28. */
  29. class ExpressionBuilder
  30. {
  31. /**
  32. * @param mixed $x
  33. *
  34. * @return CompositeExpression
  35. */
  36. public function andX($x = null)
  37. {
  38. return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
  39. }
  40. /**
  41. * @param mixed $x
  42. *
  43. * @return CompositeExpression
  44. */
  45. public function orX($x = null)
  46. {
  47. return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
  48. }
  49. /**
  50. * @param string $field
  51. * @param mixed $value
  52. *
  53. * @return Comparison
  54. */
  55. public function eq($field, $value)
  56. {
  57. return new Comparison($field, Comparison::EQ, new Value($value));
  58. }
  59. /**
  60. * @param string $field
  61. * @param mixed $value
  62. *
  63. * @return Comparison
  64. */
  65. public function gt($field, $value)
  66. {
  67. return new Comparison($field, Comparison::GT, new Value($value));
  68. }
  69. /**
  70. * @param string $field
  71. * @param mixed $value
  72. *
  73. * @return Comparison
  74. */
  75. public function lt($field, $value)
  76. {
  77. return new Comparison($field, Comparison::LT, new Value($value));
  78. }
  79. /**
  80. * @param string $field
  81. * @param mixed $value
  82. *
  83. * @return Comparison
  84. */
  85. public function gte($field, $value)
  86. {
  87. return new Comparison($field, Comparison::GTE, new Value($value));
  88. }
  89. /**
  90. * @param string $field
  91. * @param mixed $value
  92. *
  93. * @return Comparison
  94. */
  95. public function lte($field, $value)
  96. {
  97. return new Comparison($field, Comparison::LTE, new Value($value));
  98. }
  99. /**
  100. * @param string $field
  101. * @param mixed $value
  102. *
  103. * @return Comparison
  104. */
  105. public function neq($field, $value)
  106. {
  107. return new Comparison($field, Comparison::NEQ, new Value($value));
  108. }
  109. /**
  110. * @param string $field
  111. *
  112. * @return Comparison
  113. */
  114. public function isNull($field)
  115. {
  116. return new Comparison($field, Comparison::EQ, new Value(null));
  117. }
  118. /**
  119. * @param string $field
  120. * @param mixed $values
  121. *
  122. * @return Comparison
  123. */
  124. public function in($field, array $values)
  125. {
  126. return new Comparison($field, Comparison::IN, new Value($values));
  127. }
  128. /**
  129. * @param string $field
  130. * @param mixed $values
  131. *
  132. * @return Comparison
  133. */
  134. public function notIn($field, array $values)
  135. {
  136. return new Comparison($field, Comparison::NIN, new Value($values));
  137. }
  138. /**
  139. * @param string $field
  140. * @param mixed $value
  141. *
  142. * @return Comparison
  143. */
  144. public function contains($field, $value)
  145. {
  146. return new Comparison($field, Comparison::CONTAINS, new Value($value));
  147. }
  148. }