InCondition   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 17
dl 0
loc 69
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 __construct() 0 5 1
A getColumn() 0 3 1
A validateColumn() 0 7 5
A validateValues() 0 8 5
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\Expression\ExpressionInterface;
10
use Yiisoft\Db\QueryBuilder\Condition\Interface\InConditionInterface;
11
use Yiisoft\Db\Query\QueryInterface;
12
13
/**
14
 * Condition that represents `IN` operator.
15
 */
16
final class InCondition implements InConditionInterface
17
{
18
    public function __construct(
19
        private array|string|Iterator|ExpressionInterface $column,
20
        private string $operator,
21
        private int|iterable|Iterator|QueryInterface $values
22
    ) {
23
    }
24
25
    public function getColumn(): array|string|ExpressionInterface|Iterator
26
    {
27
        return $this->column;
28
    }
29
30
    public function getOperator(): string
31
    {
32
        return $this->operator;
33
    }
34
35
    public function getValues(): int|iterable|Iterator|QueryInterface
36
    {
37
        return $this->values;
38
    }
39
40
    /**
41
     * Creates a condition based on the given operator and operands.
42
     *
43
     * @throws InvalidArgumentException If the number of operands isn't 2.
44
     */
45
    public static function fromArrayDefinition(string $operator, array $operands): self
46
    {
47
        if (!isset($operands[0], $operands[1])) {
48
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
49
        }
50
51
        return new self(
52
            self::validateColumn($operator, $operands[0]),
53
            $operator,
54
            self::validateValues($operator, $operands[1]),
55
        );
56
    }
57
58
    /**
59
     * Validates the given column to be `string`, `array` or `ExpressionInterface`.
60
     *
61
     * @throws InvalidArgumentException If the column isn't a `string`, `array` or `ExpressionInterface`.
62
     */
63
    private static function validateColumn(string $operator, mixed $column): array|string|Iterator|ExpressionInterface
64
    {
65
        if (is_string($column) || is_array($column) || $column instanceof Iterator || $column instanceof ExpressionInterface) {
66
            return $column;
67
        }
68
69
        throw new InvalidArgumentException("Operator '$operator' requires column to be string, array or Iterator.");
70
    }
71
72
    /**
73
     * Validates the given values to be `array`, `Iterator`, `int` or `QueryInterface`.
74
     *
75
     * @throws InvalidArgumentException If the values aren't an `array`, `Iterator`, `int` or `QueryInterface`.
76
     */
77
    private static function validateValues(string $operator, mixed $values): int|iterable|Iterator|QueryInterface
78
    {
79
        if (is_array($values) || $values instanceof Iterator || is_int($values) || $values instanceof QueryInterface) {
80
            return $values;
81
        }
82
83
        throw new InvalidArgumentException(
84
            "Operator '$operator' requires values to be array, Iterator, int or QueryInterface."
85
        );
86
    }
87
}
88