Passed
Push — master ( f4eeb8...0d22ee )
by Sergei
11:47 queued 01:39
created

BetweenColumnsCondition::getIntervalStartColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Query\Conditions;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\ExpressionInterface;
9
use Yiisoft\Db\Query\Query;
10
11
/**
12
 * Class BetweenColumnCondition represents a `BETWEEN` condition where values is between two columns.
13
 *
14
 * For example:.
15
 *
16
 * ```php
17
 * new BetweenColumnsCondition(42, 'BETWEEN', 'min_value', 'max_value')
18
 * // Will be build to:
19
 * // 42 BETWEEN min_value AND max_value
20
 * ```
21
 *
22
 * And a more complex example:
23
 *
24
 * ```php
25
 * new BetweenColumnsCondition(
26
 *    new Expression('NOW()'),
27
 *    'NOT BETWEEN',
28
 *    (new Query)->select('time')->from('log')->orderBy('id ASC')->limit(1),
29
 *    'update_time'
30
 * );
31
 *
32
 * // Will be built to:
33
 * // NOW() NOT BETWEEN (SELECT time FROM log ORDER BY id ASC LIMIT 1) AND update_time
34
 * ```
35
 */
36
class BetweenColumnsCondition implements ConditionInterface
37
{
38
    private string $operator = '';
39
    private $value;
40
    private $intervalStartColumn;
41
    private $intervalEndColumn;
42
43
    public function __construct($value, string $operator, $intervalStartColumn, $intervalEndColumn)
44
    {
45
        $this->value = $value;
46
        $this->operator = $operator;
47
        $this->intervalStartColumn = $intervalStartColumn;
48
        $this->intervalEndColumn = $intervalEndColumn;
49
    }
50
51
    /**
52
     * @return string the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`).
53
     */
54 25
    public function getOperator(): string
55
    {
56 25
        return $this->operator;
57
    }
58
59
    /**
60
     * @return mixed the value to compare against.
61
     */
62 25
    public function getValue()
63
    {
64 25
        return $this->value;
65
    }
66
67
    /**
68
     * @return ExpressionInterface|Query|string the column name or expression that is a beginning of the interval.
69
     */
70 25
    public function getIntervalStartColumn()
71
    {
72 25
        return $this->intervalStartColumn;
73
    }
74
75
    /**
76
     * @return ExpressionInterface|Query|string the column name or expression that is an end of the interval.
77
     */
78 25
    public function getIntervalEndColumn()
79
    {
80 25
        return $this->intervalEndColumn;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @throws InvalidArgumentException if wrong number of operands have been given.
87
     */
88
    public static function fromArrayDefinition(string $operator, array $operands): self
89
    {
90
        if (!isset($operands[0], $operands[1], $operands[2])) {
91
            throw new InvalidArgumentException("Operator '$operator' requires three operands.");
92
        }
93
94
        return new static($operands[0], $operator, $operands[1], $operands[2]);
95
    }
96
}
97