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
|
|
|
|