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

LikeCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Expression;
10
use Yiisoft\Db\Expression\ExpressionInterface;
11
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
12
13
/**
14
 * Class LikeCondition represents a `LIKE` condition.
15
 */
16
final class LikeCondition implements LikeConditionInterface
17
{
18
    protected array|null $escapingReplacements = [];
19
20
    public function __construct(
21
        private string|Expression $column,
22
        private string $operator,
23
        private array|int|string|Iterator|ExpressionInterface|null $value
24
    ) {
25
    }
26
27
    public function getColumn(): string|Expression
28
    {
29
        return $this->column;
30
    }
31
32
    public function getEscapingReplacements(): ?array
33
    {
34
        return $this->escapingReplacements;
35
    }
36
37
    public function getOperator(): string
38
    {
39
        return $this->operator;
40
    }
41
42
    public function getValue(): array|int|string|Iterator|ExpressionInterface|null
43
    {
44
        return $this->value;
45
    }
46
47
    public function setEscapingReplacements(array|null $escapingReplacements): void
48
    {
49
        $this->escapingReplacements = $escapingReplacements;
50
    }
51
52
    /**
53
     * @throws InvalidArgumentException
54
     */
55
    public static function fromArrayDefinition(string $operator, array $operands): self
56
    {
57
        if (!isset($operands[0], $operands[1])) {
58
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
59
        }
60
61
        $condition = new self(
62
            self::validateColumn($operator, $operands[0]),
63
            $operator,
64
            self::validateValue($operator, $operands[1]),
65
        );
66
67
        if (array_key_exists(2, $operands) && (is_array($operands[2]) || $operands[2] === null)) {
68
            $condition->setEscapingReplacements($operands[2]);
69
        }
70
71
        return $condition;
72
    }
73
74
    private static function validateColumn(string $operator, mixed $column): string|Expression
75
    {
76
        if (!is_string($column) && !$column instanceof Expression) {
77
            throw new InvalidArgumentException("Operator '$operator' requires column to be string or Expression.");
78
        }
79
80
        return $column;
81
    }
82
83
    private static function validateValue(
84
        string $operator,
85
        mixed $value
86
    ): array|int|string|Iterator|ExpressionInterface|null {
87
        if (
88
            !is_string($value) &&
89
            !is_array($value) &&
90
            !is_int($value) &&
91
            !$value instanceof Iterator &&
92
            !$value instanceof ExpressionInterface &&
93
            $value !== null
94
        ) {
95
            throw new InvalidArgumentException(
96
                "Operator '$operator' requires value to be string, array, Iterator or ExpressionInterface."
97
            );
98
        }
99
100
        return $value;
101
    }
102
}
103