BetweenColumnsCondition.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\conditions;
  8. use yii\base\InvalidArgumentException;
  9. use yii\db\ExpressionInterface;
  10. use yii\db\Query;
  11. /**
  12. * Class BetweenColumnCondition represents a `BETWEEN` condition where
  13. * values is between two columns. For example:
  14. *
  15. * ```php
  16. * new BetweenColumnsCondition(42, 'BETWEEN', 'min_value', 'max_value')
  17. * // Will be build to:
  18. * // 42 BETWEEN min_value AND max_value
  19. * ```
  20. *
  21. * And a more complex example:
  22. *
  23. * ```php
  24. * new BetweenColumnsCondition(
  25. * new Expression('NOW()'),
  26. * 'NOT BETWEEN',
  27. * (new Query)->select('time')->from('log')->orderBy('id ASC')->limit(1),
  28. * 'update_time'
  29. * );
  30. *
  31. * // Will be built to:
  32. * // NOW() NOT BETWEEN (SELECT time FROM log ORDER BY id ASC LIMIT 1) AND update_time
  33. * ```
  34. *
  35. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  36. * @since 2.0.14
  37. */
  38. class BetweenColumnsCondition implements ConditionInterface
  39. {
  40. /**
  41. * @var string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
  42. */
  43. private $operator;
  44. /**
  45. * @var mixed the value to compare against
  46. */
  47. private $value;
  48. /**
  49. * @var string|ExpressionInterface|Query the column name or expression that is a beginning of the interval
  50. */
  51. private $intervalStartColumn;
  52. /**
  53. * @var string|ExpressionInterface|Query the column name or expression that is an end of the interval
  54. */
  55. private $intervalEndColumn;
  56. /**
  57. * Creates a condition with the `BETWEEN` operator.
  58. *
  59. * @param mixed the value to compare against
  60. * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
  61. * @param string|ExpressionInterface $intervalStartColumn the column name or expression that is a beginning of the interval
  62. * @param string|ExpressionInterface $intervalEndColumn the column name or expression that is an end of the interval
  63. */
  64. public function __construct($value, $operator, $intervalStartColumn, $intervalEndColumn)
  65. {
  66. $this->value = $value;
  67. $this->operator = $operator;
  68. $this->intervalStartColumn = $intervalStartColumn;
  69. $this->intervalEndColumn = $intervalEndColumn;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getOperator()
  75. {
  76. return $this->operator;
  77. }
  78. /**
  79. * @return mixed
  80. */
  81. public function getValue()
  82. {
  83. return $this->value;
  84. }
  85. /**
  86. * @return string|ExpressionInterface|Query
  87. */
  88. public function getIntervalStartColumn()
  89. {
  90. return $this->intervalStartColumn;
  91. }
  92. /**
  93. * @return string|ExpressionInterface|Query
  94. */
  95. public function getIntervalEndColumn()
  96. {
  97. return $this->intervalEndColumn;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. * @throws InvalidArgumentException if wrong number of operands have been given.
  102. */
  103. public static function fromArrayDefinition($operator, $operands)
  104. {
  105. if (!isset($operands[0], $operands[1], $operands[2])) {
  106. throw new InvalidArgumentException("Operator '$operator' requires three operands.");
  107. }
  108. return new static($operands[0], $operator, $operands[1], $operands[2]);
  109. }
  110. }