Passed
Push — master ( f4eeb8...0d22ee )
by Sergei
11:47 queued 01:39
created

LikeCondition::fromArrayDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Query\Conditions;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
9
/**
10
 * Class LikeCondition represents a `LIKE` condition.
11
 */
12
class LikeCondition extends SimpleCondition
13
{
14
    protected $escapingReplacements = null;
15
16
    /**
17
     * @return array|bool|null
18
     */
19 167
    public function getEscapingReplacements()
20
    {
21 167
        return $this->escapingReplacements;
22
    }
23
24
    /**
25
     * This method allows to specify how to escape special characters in the value(s).
26
     *
27
     * @param array|bool|null an array of mappings from the special characters to their escaped counterparts.
28
     * You may use `false` or an empty array to indicate the values are already escaped and no escape should be applied.
29
     * Note that when using an escape mapping (or the third operand is not provided), the values will be automatically
30
     * enclosed within a pair of percentage characters.
31
     */
32
    public function setEscapingReplacements($escapingReplacements): void
33
    {
34
        $this->escapingReplacements = $escapingReplacements;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @throws InvalidArgumentException if wrong number of operands have been given.
41
     */
42 127
    public static function fromArrayDefinition(string $operator, array $operands): self
43
    {
44 127
        if (!isset($operands[0], $operands[1])) {
45
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
46
        }
47
48 127
        $condition = new static($operands[0], $operator, $operands[1]);
49
50 127
        if (isset($operands[2])) {
51 5
            $condition->escapingReplacements = $operands[2];
52
        }
53
54 127
        return $condition;
55
    }
56
}
57