1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii\db\conditions; |
4
|
|
|
|
5
|
|
|
use yii\base\InvalidArgumentException; |
6
|
|
|
use yii\db\ExpressionBuilderInterface; |
7
|
|
|
use yii\db\ExpressionBuilderTrait; |
8
|
|
|
use yii\db\ExpressionInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LikeConditionBuilder builds objects of [[LikeCondition]] |
12
|
|
|
* |
13
|
|
|
* @author Dmytro Naumenko <[email protected]> |
14
|
|
|
* @since 2.0.14 |
15
|
|
|
*/ |
16
|
|
|
class LikeConditionBuilder implements ExpressionBuilderInterface |
17
|
|
|
{ |
18
|
|
|
use ExpressionBuilderTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array map of chars to their replacements in LIKE conditions. |
22
|
|
|
* By default it's configured to escape `%`, `_` and `\` with `\`. |
23
|
|
|
*/ |
24
|
|
|
protected $escapingReplacements = [ |
25
|
|
|
'%' => '\%', |
26
|
|
|
'_' => '\_', |
27
|
|
|
'\\' => '\\\\', |
28
|
|
|
]; |
29
|
|
|
/** |
30
|
|
|
* @var string|null character used to escape special characters in LIKE conditions. |
31
|
|
|
* By default it's assumed to be `\`. |
32
|
|
|
*/ |
33
|
|
|
protected $escapeCharacter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Method builds the raw SQL from the $expression that will not be additionally |
37
|
|
|
* escaped or quoted. |
38
|
|
|
* |
39
|
|
|
* @param ExpressionInterface|LikeCondition $expression the expression to be built. |
40
|
|
|
* @param array $params the binding parameters. |
41
|
|
|
* @return string the raw SQL that will not be additionally escaped or quoted. |
42
|
|
|
*/ |
43
|
78 |
|
public function build(ExpressionInterface $expression, array &$params = []) |
44
|
|
|
{ |
45
|
78 |
|
$operator = $expression->getOperator(); |
|
|
|
|
46
|
78 |
|
$column = $expression->getColumn(); |
|
|
|
|
47
|
78 |
|
$values = $expression->getValue(); |
|
|
|
|
48
|
78 |
|
$escape = $expression->getEscapingReplacements(); |
|
|
|
|
49
|
78 |
|
if ($escape === null || $escape === []) { |
50
|
75 |
|
$escape = $this->escapingReplacements; |
51
|
|
|
} |
52
|
|
|
|
53
|
78 |
|
list($andor, $not, $operator) = $this->parseOperator($operator); |
54
|
|
|
|
55
|
78 |
|
if (!is_array($values)) { |
56
|
34 |
|
$values = [$values]; |
57
|
|
|
} |
58
|
|
|
|
59
|
78 |
|
if (empty($values)) { |
60
|
16 |
|
return $not ? '' : '0=1'; |
61
|
|
|
} |
62
|
|
|
|
63
|
62 |
|
if (strpos($column, '(') === false) { |
64
|
62 |
|
$column = $this->queryBuilder->db->quoteColumnName($column); |
65
|
|
|
} |
66
|
|
|
|
67
|
62 |
|
$escapeSql = $this->getEscapeSql(); |
68
|
62 |
|
$parts = []; |
69
|
62 |
|
foreach ($values as $value) { |
70
|
62 |
|
if ($value instanceof ExpressionInterface) { |
71
|
24 |
|
$phName = $this->queryBuilder->buildExpression($value, $params); |
72
|
|
|
} else { |
73
|
50 |
|
$phName = $this->queryBuilder->bindParam(empty($escape) ? $value : ('%' . strtr($value, $escape) . '%'), $params); |
74
|
|
|
} |
75
|
62 |
|
$parts[] = "{$column} {$operator} {$phName}{$escapeSql}"; |
76
|
|
|
} |
77
|
|
|
|
78
|
62 |
|
return implode($andor, $parts); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
62 |
|
private function getEscapeSql() |
85
|
|
|
{ |
86
|
62 |
|
if ($this->escapeCharacter !== null) { |
87
|
18 |
|
return " ESCAPE '{$this->escapeCharacter}'"; |
88
|
|
|
} |
89
|
|
|
|
90
|
44 |
|
return ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $operator |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
78 |
|
protected function parseOperator($operator) |
98
|
|
|
{ |
99
|
78 |
|
if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) { |
100
|
|
|
throw new InvalidArgumentException("Invalid operator '$operator'."); |
101
|
|
|
} |
102
|
78 |
|
$andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND '); |
103
|
78 |
|
$not = !empty($matches[3]); |
104
|
78 |
|
$operator = $matches[2]; |
105
|
|
|
|
106
|
78 |
|
return [$andor, $not, $operator]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: