Passed
Pull Request — master (#343)
by Sergei
02:17
created

SimpleCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArrayDefinition() 0 7 2
A __construct() 0 5 1
A getOperator() 0 3 1
A getColumn() 0 3 1
A getValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder\Conditions;
6
7
use Iterator;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Expression\Expression;
10
use Yiisoft\Db\Expression\ExpressionInterface;
11
use Yiisoft\Db\QueryBuilder\Conditions\Interface\SimpleConditionInterface;
12
use Yiisoft\Db\Query\QueryInterface;
13
14
use function count;
15
16
/**
17
 * Class SimpleCondition represents a simple condition like `"column" operator value`.
18
 */
19
final class SimpleCondition implements SimpleConditionInterface
20
{
21
    public function __construct(
22
        private string|Expression|QueryInterface $column,
23
        private string $operator,
24
        private array|int|string|Iterator|ExpressionInterface|null $value
25
    ) {
26
    }
27
28
    public function getColumn(): string|Expression|QueryInterface
29
    {
30
        return $this->column;
31
    }
32
33
    public function getOperator(): string
34
    {
35
        return $this->operator;
36
    }
37
38
    public function getValue(): array|int|string|Iterator|ExpressionInterface|null
39
    {
40
        return $this->value;
41
    }
42
43
    /**
44
     * @throws InvalidArgumentException
45
     *
46
     * @psalm-suppress MixedArgument
47
     */
48
    public static function fromArrayDefinition(string $operator, array $operands): self
49
    {
50
        if (count($operands) !== 2) {
51
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
52
        }
53
54
        return new self($operands[0], $operator, $operands[1]);
55
    }
56
}
57