Passed
Push — master ( c65027...a39d50 )
by Wilmer
18:53 queued 16:02
created

SimpleCondition::getOperator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder\Condition;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\QueryBuilder\Condition\Interface\SimpleConditionInterface;
10
use Yiisoft\Db\Query\QueryInterface;
11
12
/**
13
 * Class SimpleCondition represents a simple condition like `"column" operator value`.
14
 */
15
final class SimpleCondition implements SimpleConditionInterface
16
{
17
    public function __construct(
18
        private string|Expression|QueryInterface $column,
19
        private string $operator,
20
        private mixed $value
21
    ) {
22
    }
23
24
    public function getColumn(): string|Expression|QueryInterface
25
    {
26
        return $this->column;
27
    }
28
29
    public function getOperator(): string
30
    {
31
        return $this->operator;
32
    }
33
34
    public function getValue(): mixed
35
    {
36
        return $this->value;
37
    }
38
39
    /**
40
     * @throws InvalidArgumentException
41
     */
42
    public static function fromArrayDefinition(string $operator, array $operands): self
43
    {
44
        if (!isset($operands[0]) || !array_key_exists(1, $operands)) {
45
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
46
        }
47
48
        return new self(self::validateColumn($operator, $operands[0]), $operator, $operands[1]);
49
    }
50
51
    private static function validateColumn(string $operator, mixed $column): string|Expression|QueryInterface
52
    {
53
        if (
54
            !is_string($column) &&
55
            !($column instanceof Expression) &&
56
            !($column instanceof QueryInterface)
57
        ) {
58
            throw new InvalidArgumentException(
59
                "Operator '$operator' requires column to be string, ExpressionInterface or QueryInterface."
60
            );
61
        }
62
63
        return $column;
64
    }
65
}
66