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

InCondition   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 17
dl 0
loc 58
rs 10
c 0
b 0
f 0

7 Methods

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