1 | <?php |
||
21 | class InConditionBuilder implements ExpressionBuilderInterface |
||
22 | { |
||
23 | use ExpressionBuilderTrait; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Method builds the raw SQL from the $expression that will not be additionally |
||
28 | * escaped or quoted. |
||
29 | * |
||
30 | * @param ExpressionInterface|InCondition $expression the expression to be built. |
||
31 | * @param array $params the binding parameters. |
||
32 | * @return string the raw SQL that will not be additionally escaped or quoted. |
||
33 | */ |
||
34 | 274 | public function build(ExpressionInterface $expression, array &$params = []) |
|
77 | |||
78 | /** |
||
79 | * Builds $values to be used in [[InCondition]] |
||
80 | * |
||
81 | * @param ConditionInterface|InCondition $condition |
||
82 | * @param array $values |
||
83 | * @param array $params the binding parameters |
||
84 | * @return array of prepared for SQL placeholders |
||
85 | */ |
||
86 | 245 | protected function buildValues(ConditionInterface $condition, $values, &$params) |
|
87 | { |
||
88 | 245 | $sqlValues = []; |
|
89 | 245 | $column = $condition->getColumn(); |
|
90 | |||
91 | 245 | foreach ($values as $i => $value) { |
|
92 | 242 | if (is_array($value) || $value instanceof \ArrayAccess) { |
|
93 | $value = $value[$column] ?? null; |
||
94 | } |
||
95 | 242 | if ($value === null) { |
|
96 | $sqlValues[$i] = 'NULL'; |
||
97 | 242 | } elseif ($value instanceof ExpressionInterface) { |
|
98 | 3 | $sqlValues[$i] = $this->queryBuilder->buildExpression($value, $params); |
|
99 | } else { |
||
100 | 242 | $sqlValues[$i] = $this->queryBuilder->bindParam($value, $params); |
|
101 | } |
||
102 | } |
||
103 | |||
104 | 245 | return $sqlValues; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Builds SQL for IN condition. |
||
109 | * |
||
110 | * @param string $operator |
||
111 | * @param array|string $columns |
||
112 | * @param Query $values |
||
113 | * @param array $params |
||
114 | * @return string SQL |
||
115 | */ |
||
116 | 14 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
136 | |||
137 | /** |
||
138 | * Builds SQL for IN condition. |
||
139 | * |
||
140 | * @param string $operator |
||
141 | * @param array|\Traversable $columns |
||
142 | * @param array $values |
||
143 | * @param array $params |
||
144 | * @return string SQL |
||
145 | */ |
||
146 | 12 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
172 | } |
||
173 |
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: