1 | <?php |
||
49 | class Builder extends ObjectAbstract implements BuilderInterface |
||
50 | { |
||
51 | use DialectAwareTrait, SettingsAwareTrait, ParameterAwareTrait; |
||
52 | |||
53 | /** |
||
54 | * tables |
||
55 | * |
||
56 | * @var array |
||
57 | * @access protected |
||
58 | */ |
||
59 | protected $tables = []; |
||
60 | |||
61 | /** |
||
62 | * Constructor |
||
63 | * |
||
64 | * ```php |
||
65 | * // builder with default table `users` and Mysql dialect |
||
66 | * $users = new Builder('users', new Mysql()) |
||
67 | * |
||
68 | * // builder with defult tables: `users` and `accounts` AS `a` |
||
69 | * $builder = new Builder(['users', 'accounts' => 'a']) |
||
70 | * |
||
71 | * // change default settings |
||
72 | * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]); |
||
73 | * ``` |
||
74 | * |
||
75 | * @param string|array $table table[s] to build upon |
||
76 | * @param DialectInterface $dialect default dialect is `Mysql` |
||
77 | * @param array $settings builder settings |
||
78 | * @access public |
||
79 | */ |
||
80 | public function __construct( |
||
91 | |||
92 | /** |
||
93 | * Change table[s] |
||
94 | * |
||
95 | * @param $table change to table[s] |
||
96 | * @return $this |
||
97 | * @access public |
||
98 | */ |
||
99 | public function __invoke($table) |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | public function expr()/*# : ExpressionInterface */ |
||
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | public function raw( |
||
124 | |||
125 | /** |
||
126 | * If has existing tables, return a new instance with provided table[s] |
||
127 | * |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public function table($table, /*# string */ $alias = '') |
||
137 | |||
138 | /** |
||
139 | * Append to existing tables |
||
140 | * |
||
141 | * {@inheritDoc} |
||
142 | */ |
||
143 | public function from($table, /*# string */ $alias = '') |
||
149 | |||
150 | /** |
||
151 | * {@inheritDoc} |
||
152 | */ |
||
153 | public function select()/*# : SelectStatementInterface */ |
||
159 | |||
160 | /** |
||
161 | * {@inheritDoc} |
||
162 | */ |
||
163 | public function insert(array $values = [])/*# : InsertStatementInterface */ |
||
169 | |||
170 | /** |
||
171 | * {@inheritDoc} |
||
172 | */ |
||
173 | public function replace(array $values = [])/*# : InsertStatementInterface */ |
||
179 | |||
180 | /** |
||
181 | * {@inheritDoc} |
||
182 | */ |
||
183 | public function update(array $values = [])/*# : UpdateStatementInterface */ |
||
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | */ |
||
193 | public function delete()/*# : DeleteStatementInterface */ |
||
199 | |||
200 | /** |
||
201 | * {@inheritDoc} |
||
202 | */ |
||
203 | public function union()/*# : UnionStatementInterface */ |
||
215 | |||
216 | /** |
||
217 | * Convert to [$table => alias] or [$table] |
||
218 | * |
||
219 | * @param string|string[] $table |
||
220 | * @param string $alias |
||
221 | * @return array |
||
222 | * @access protected |
||
223 | */ |
||
224 | protected function fixTable( |
||
235 | |||
236 | /** |
||
237 | * Builder default settings |
||
238 | * |
||
239 | * @return array |
||
240 | * @access protected |
||
241 | */ |
||
242 | protected function defaultSettings()/*# : array */ |
||
254 | } |
||
255 |
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: