Completed
Pull Request — master (#15640)
by Robert
11:21
created

LikeCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 59
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setEscapingReplacements() 0 4 1
A getEscapingReplacements() 0 4 1
A fromArrayDefinition() 0 13 3
1
<?php
2
3
namespace yii\db\conditions;
4
5
use yii\base\InvalidArgumentException;
6
7
/**
8
 * Class LikeCondition represents a `LIKE` condition.
9
 *
10
 * @author Dmytro Naumenko <[email protected]>
11
 * @since 2.0.14
12
 */
13
class LikeCondition extends SimpleCondition
14
{
15
    /**
16
     * @var array map of chars to their replacements.
17
     * By default it's set to `null` meaning responsibility is fully on condition builder.
18
     */
19
    protected $escapingReplacements;
20
21
    /**
22
     * @param string $column the column name.
23
     * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`)
24
     * @param string[]|string $value single value or an array of values that $column should be compared with.
25
     *   If it is an empty array the generated expression will  be a `false` value if operator is `LIKE` or `OR LIKE`
26
     *   and empty if operator is `NOT LIKE` or `OR NOT LIKE`.
27
     */
28 78
    public function __construct($column, $operator, $value)
29
    {
30 78
        parent::__construct($column, $operator, $value);
31 78
    }
32
33
    /**
34
     * This method allows to specify how to escape special characters in the value(s).
35
     *
36
     * @param array an array of mappings from the special characters to their escaped counterparts.
37
     *  You may use `false` or an empty array to indicate the values are already escaped and no escape
38
     *  should be applied. Note that when using an escape mapping (or the third operand is not provided),
39
     *  the values will be automatically enclosed within a pair of percentage characters.
40
     */
41
    public function setEscapingReplacements($escapingReplacements)
42
    {
43
        $this->escapingReplacements = $escapingReplacements;
44
    }
45
46
    /**
47
     * @return array
48
     */
49 78
    public function getEscapingReplacements()
50
    {
51 78
        return $this->escapingReplacements;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     * @throws InvalidArgumentException if wrong number of operands have been given.
57
     */
58 78
    public static function fromArrayDefinition($operator, $operands)
59
    {
60 78
        if (!isset($operands[0], $operands[1])) {
61
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
62
        }
63
64 78
        $condition = new static($operands[0], $operator, $operands[1]);
65 78
        if (isset($operands[2])) {
66 3
            $condition->escapingReplacements = $operands[2];
67
        }
68
69 78
        return $condition;
70
    }
71
}
72