Completed
Push — 15630-fixed-like-escaping ( 1da559 )
by Alexander
11:08
created

LikeCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
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 60
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|false map of chars to their replacements, false if characters should not be escaped
17
     * or either null or empty array if escaping is condition builder responsibility.
18
     * By default it's set to `null`.
19
     */
20
    protected $escapingReplacements;
21
22
    /**
23
     * @param string $column the column name.
24
     * @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`)
25
     * @param string[]|string $value single value or an array of values that $column should be compared with.
26
     *   If it is an empty array the generated expression will  be a `false` value if operator is `LIKE` or `OR LIKE`
27
     *   and empty if operator is `NOT LIKE` or `OR NOT LIKE`.
28
     */
29 78
    public function __construct($column, $operator, $value)
30
    {
31 78
        parent::__construct($column, $operator, $value);
32 78
    }
33
34
    /**
35
     * This method allows to specify how to escape special characters in the value(s).
36
     *
37
     * @param array an array of mappings from the special characters to their escaped counterparts.
38
     *  You may use `false` or an empty array to indicate the values are already escaped and no escape
39
     *  should be applied. Note that when using an escape mapping (or the third operand is not provided),
40
     *  the values will be automatically enclosed within a pair of percentage characters.
41
     */
42
    public function setEscapingReplacements($escapingReplacements)
43
    {
44
        $this->escapingReplacements = $escapingReplacements;
45
    }
46
47
    /**
48
     * @return array
49
     */
50 78
    public function getEscapingReplacements()
51
    {
52 78
        return $this->escapingReplacements;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     * @throws InvalidArgumentException if wrong number of operands have been given.
58
     */
59 78
    public static function fromArrayDefinition($operator, $operands)
60
    {
61 78
        if (!isset($operands[0], $operands[1])) {
62
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
63
        }
64
65 78
        $condition = new static($operands[0], $operator, $operands[1]);
66 78
        if (isset($operands[2])) {
67 3
            $condition->escapingReplacements = $operands[2];
68
        }
69
70 78
        return $condition;
71
    }
72
}
73