Comparison.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Expr;
  20. /**
  21. * Comparison of a field with a value by the given operator.
  22. *
  23. * @author Benjamin Eberlei <kontakt@beberlei.de>
  24. * @since 2.3
  25. */
  26. class Comparison implements Expression
  27. {
  28. const EQ = '=';
  29. const NEQ = '<>';
  30. const LT = '<';
  31. const LTE = '<=';
  32. const GT = '>';
  33. const GTE = '>=';
  34. const IS = '='; // no difference with EQ
  35. const IN = 'IN';
  36. const NIN = 'NIN';
  37. const CONTAINS = 'CONTAINS';
  38. /**
  39. * @var string
  40. */
  41. private $field;
  42. /**
  43. * @var string
  44. */
  45. private $op;
  46. /**
  47. * @var Value
  48. */
  49. private $value;
  50. /**
  51. * @param string $field
  52. * @param string $operator
  53. * @param mixed $value
  54. */
  55. public function __construct($field, $operator, $value)
  56. {
  57. if ( ! ($value instanceof Value)) {
  58. $value = new Value($value);
  59. }
  60. $this->field = $field;
  61. $this->op = $operator;
  62. $this->value = $value;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getField()
  68. {
  69. return $this->field;
  70. }
  71. /**
  72. * @return Value
  73. */
  74. public function getValue()
  75. {
  76. return $this->value;
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function getOperator()
  82. {
  83. return $this->op;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function visit(ExpressionVisitor $visitor)
  89. {
  90. return $visitor->walkComparison($this);
  91. }
  92. }