1 | <?php |
||
29 | trait ReadQueries |
||
30 | { |
||
31 | use BaseTrait; |
||
32 | |||
33 | /** |
||
34 | * escapeIdentifier |
||
35 | * |
||
36 | * @see Model |
||
37 | */ |
||
38 | abstract protected function escapeIdentifier($string); |
||
39 | |||
40 | /** |
||
41 | * getStructure |
||
42 | * |
||
43 | * @see Model |
||
44 | */ |
||
45 | abstract public function getStructure(); |
||
46 | |||
47 | /** |
||
48 | * findAll |
||
49 | * |
||
50 | * Return all elements from a relation. If a suffix is given, it is append |
||
51 | * to the query. This is mainly useful for "order by" statements. |
||
52 | * NOTE: suffix is inserted as is with NO ESCAPING. DO NOT use it to place |
||
53 | * "where" condition nor any untrusted params. |
||
54 | * |
||
55 | * @access public |
||
56 | * @param string $suffix |
||
57 | * @return CollectionIterator |
||
58 | */ |
||
59 | public function findAll($suffix = null) |
||
72 | |||
73 | /** |
||
74 | * findWhere |
||
75 | * |
||
76 | * Perform a simple select on a given condition |
||
77 | * NOTE: suffix is inserted as is with NO ESCAPING. DO NOT use it to place |
||
78 | * "where" condition nor any untrusted params. |
||
79 | * |
||
80 | * @access public |
||
81 | * @param mixed $where |
||
82 | * @param array $values |
||
83 | * @param string $suffix order by, limit, etc. |
||
84 | * @return CollectionIterator |
||
85 | */ |
||
86 | public function findWhere($where, array $values = [], $suffix = '') |
||
94 | |||
95 | /** |
||
96 | * findByPK |
||
97 | * |
||
98 | * Return an entity upon its primary key. If no entities are found, null is |
||
99 | * returned. |
||
100 | * |
||
101 | * @access public |
||
102 | * @param array $primary_key |
||
103 | * @return FlexibleEntityInterface |
||
104 | */ |
||
105 | public function findByPK(array $primary_key) |
||
116 | |||
117 | /** |
||
118 | * countWhere |
||
119 | * |
||
120 | * Return the number of records matching a condition. |
||
121 | * |
||
122 | * @access public |
||
123 | * @param string|Where $where |
||
124 | * @param array $values |
||
125 | * @return int |
||
126 | */ |
||
127 | public function countWhere($where, array $values = []) |
||
136 | |||
137 | /** |
||
138 | * existWhere |
||
139 | * |
||
140 | * Check if rows matching the given condition do exist or not. |
||
141 | * |
||
142 | * @access public |
||
143 | * @param mixed $where |
||
144 | * @param array $values |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function existWhere($where, array $values = []) |
||
156 | |||
157 | /** |
||
158 | * fetchSingleValue |
||
159 | * |
||
160 | * Fetch a single value named « result » from a query. |
||
161 | * The query must be formatted with ":condition" as WHERE condition |
||
162 | * placeholder. If the $where argument is a string, it is turned into a |
||
163 | * Where instance. |
||
164 | * |
||
165 | * @access protected |
||
166 | * @param string $sql |
||
167 | * @param mixed $where |
||
168 | * @param array $values |
||
169 | * @return mixed |
||
170 | */ |
||
171 | protected function fetchSingleValue($sql, $where, array $values) |
||
186 | |||
187 | /** |
||
188 | * paginateFindWhere |
||
189 | * |
||
190 | * Paginate a query. |
||
191 | * |
||
192 | * @access public |
||
193 | * @param Where $where |
||
194 | * @param int $item_per_page |
||
195 | * @param int $page |
||
196 | * @param string $suffix |
||
197 | * @return Pager |
||
198 | */ |
||
199 | public function paginateFindWhere(Where $where, $item_per_page, $page = 1, $suffix = '') |
||
212 | |||
213 | /** |
||
214 | * paginateQuery |
||
215 | * |
||
216 | * Paginate a SQL query. |
||
217 | * It is important to note it adds limit and offset at the end of the given |
||
218 | * query. |
||
219 | * |
||
220 | * @access protected |
||
221 | * @param string $sql |
||
222 | * @param array $values parameters |
||
223 | * @param int $count |
||
224 | * @param int $item_per_page |
||
225 | * @param int $page |
||
226 | * @param Projection $projection |
||
227 | * @throws \InvalidArgumentException if pager args are invalid. |
||
228 | * @return Pager |
||
229 | */ |
||
230 | protected function paginateQuery($sql, array $values, $count, $item_per_page, $page = 1, Projection $projection = null) |
||
258 | |||
259 | /** |
||
260 | * getFindWhereSql |
||
261 | * |
||
262 | * This is the standard SQL query to fetch instances from the current |
||
263 | * relation. |
||
264 | * |
||
265 | * @access protected |
||
266 | * @param Where $where |
||
267 | * @param Projection $projection |
||
268 | * @param string $suffix |
||
269 | * @return string |
||
270 | */ |
||
271 | protected function getFindWhereSql(Where $where, Projection $projection, $suffix = '') |
||
283 | |||
284 | /** |
||
285 | * hasPrimaryKey |
||
286 | * |
||
287 | * Check if model has a primary key |
||
288 | * |
||
289 | * @access protected |
||
290 | * @return bool |
||
291 | */ |
||
292 | protected function hasPrimaryKey() |
||
298 | |||
299 | /** |
||
300 | * checkPrimaryKey |
||
301 | * |
||
302 | * Check if the given values fully describe a primary key. Throw a |
||
303 | * ModelException if not. |
||
304 | * |
||
305 | * @access private |
||
306 | * @param array $values |
||
307 | * @throws ModelException |
||
308 | * @return $this |
||
309 | */ |
||
310 | protected function checkPrimaryKey(array $values) |
||
335 | |||
336 | /** |
||
337 | * getWhereFrom |
||
338 | * |
||
339 | * Build a condition on given values. |
||
340 | * |
||
341 | * @access protected |
||
342 | * @param array $values |
||
343 | * @return Where |
||
344 | */ |
||
345 | protected function getWhereFrom(array $values) |
||
362 | } |
||
363 |
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: