BetweenColumnsCondition.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://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. * @phpcs:disable Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore
  38. */
  39. class BetweenColumnsCondition implements ConditionInterface
  40. {
  41. /**
  42. * @var string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
  43. */
  44. private $operator;
  45. /**
  46. * @var mixed the value to compare against
  47. */
  48. private $value;
  49. /**
  50. * @var string|ExpressionInterface|Query the column name or expression that is a beginning of the interval
  51. */
  52. private $intervalStartColumn;
  53. /**
  54. * @var string|ExpressionInterface|Query the column name or expression that is an end of the interval
  55. */
  56. private $intervalEndColumn;
  57. /**
  58. * Creates a condition with the `BETWEEN` operator.
  59. *
  60. * @param mixed the value to compare against
  61. * @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`)
  62. * @param string|ExpressionInterface $intervalStartColumn the column name or expression that is a beginning of the interval
  63. * @param string|ExpressionInterface $intervalEndColumn the column name or expression that is an end of the interval
  64. */
  65. public function __construct($value, $operator, $intervalStartColumn, $intervalEndColumn)
  66. {
  67. $this->value = $value;
  68. $this->operator = $operator;
  69. $this->intervalStartColumn = $intervalStartColumn;
  70. $this->intervalEndColumn = $intervalEndColumn;
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function getOperator()
  76. {
  77. return $this->operator;
  78. }
  79. /**
  80. * @return mixed
  81. */
  82. public function getValue()
  83. {
  84. return $this->value;
  85. }
  86. /**
  87. * @return string|ExpressionInterface|Query
  88. */
  89. public function getIntervalStartColumn()
  90. {
  91. return $this->intervalStartColumn;
  92. }
  93. /**
  94. * @return string|ExpressionInterface|Query
  95. */
  96. public function getIntervalEndColumn()
  97. {
  98. return $this->intervalEndColumn;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. * @throws InvalidArgumentException if wrong number of operands have been given.
  103. */
  104. public static function fromArrayDefinition($operator, $operands)
  105. {
  106. if (!isset($operands[0], $operands[1], $operands[2])) {
  107. throw new InvalidArgumentException("Operator '$operator' requires three operands.");
  108. }
  109. return new static($operands[0], $operator, $operands[1], $operands[2]);
  110. }
  111. }