Passed
Pull Request — master (#376)
by Alexander
05:11 queued 02:37
created

BetweenCondition   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumn() 0 3 1
A __construct() 0 6 1
A getIntervalStart() 0 3 1
A getOperator() 0 3 1
A getIntervalEnd() 0 3 1
A validateColumn() 0 9 3
A fromArrayDefinition() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder\Conditions;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\QueryBuilder\Conditions\Interface\BetweenConditionInterface;
9
use Yiisoft\Db\Expression\Expression;
10
11
/**
12
 * Class BetweenCondition represents a `BETWEEN` condition.
13
 */
14
final class BetweenCondition implements BetweenConditionInterface
15
{
16
    public function __construct(
17
        private string|Expression $column,
18
        private string $operator,
19
        private mixed $intervalStart,
20
        private mixed $intervalEnd
21
    ) {
22
    }
23
24
    public function getColumn(): string|Expression
25
    {
26
        return $this->column;
27
    }
28
29
    public function getIntervalEnd(): mixed
30
    {
31
        return $this->intervalEnd;
32
    }
33
34
    public function getIntervalStart(): mixed
35
    {
36
        return $this->intervalStart;
37
    }
38
39
    public function getOperator(): string
40
    {
41
        return $this->operator;
42
    }
43
44
    /**
45
     * @throws InvalidArgumentException
46
     */
47
    public static function fromArrayDefinition(string $operator, array $operands): self
48
    {
49
        if (!isset($operands[0], $operands[1], $operands[2])) {
50
            throw new InvalidArgumentException("Operator '$operator' requires three operands.");
51
        }
52
53
        return new self(self::validateColumn($operator, $operands[0]), $operator, $operands[1], $operands[2]);
54
    }
55
56
    private static function validateColumn(string $operator, mixed $operand): string|Expression
57
    {
58
        if (!is_string($operand) && !($operand instanceof Expression)) {
59
            throw new InvalidArgumentException(
60
                "Operator '$operator' requires column to be string or ExpressionInterface."
61
            );
62
        }
63
64
        return $operand;
65
    }
66
}
67