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