1 | <?php |
||
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 = []) |
|
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | 62 | private function getEscapeSql() |
|
92 | |||
93 | /** |
||
94 | * @param string $operator |
||
95 | * @return array |
||
96 | */ |
||
97 | 78 | protected function parseOperator($operator) |
|
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: