Passed
Pull Request — master (#343)
by Sergei
08:05 queued 05:38
created

LikeCondition::fromArrayDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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\LikeConditionInterface;
12
13
/**
14
 * Class LikeCondition represents a `LIKE` condition.
15
 */
16
final class LikeCondition implements LikeConditionInterface
17
{
18
    protected ?array $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
     * @psalm-suppress MixedArgument
56
     */
57
    public static function fromArrayDefinition(string $operator, array $operands): self
58
    {
59
        if (!isset($operands[0], $operands[1])) {
60
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
61
        }
62
63
        $condition = new self($operands[0], $operator, $operands[1]);
64
65
        if (array_key_exists(2, $operands)) {
66
            $condition->setEscapingReplacements($operands[2]);
67
        }
68
69
        return $condition;
70
    }
71
}
72