1 | <?php |
||
25 | class AbstractFinder implements ModelAwareInterface |
||
26 | { |
||
27 | |||
28 | use ModelAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * Whenever to use cursors |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $useCursor = false; |
||
35 | |||
36 | /** |
||
37 | * Finds a single Document with the specified condition. |
||
38 | * |
||
39 | * @param array|CriteriaInterface $criteria query criteria. |
||
40 | * @return AnnotatedInterface |
||
41 | * @Ignored |
||
42 | */ |
||
43 | 52 | public function find($criteria = null) |
|
53 | |||
54 | /** |
||
55 | * Finds document with the specified primary key. Primary key by default |
||
56 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
57 | * keys use it's value. |
||
58 | * |
||
59 | * For composite use key-value with column names as keys |
||
60 | * and values for values. |
||
61 | * |
||
62 | * Example for simple pk: |
||
63 | * ```php |
||
64 | * $pk = '51b616fcc0986e30026d0748' |
||
65 | * ``` |
||
66 | * |
||
67 | * Composite pk: |
||
68 | * ```php |
||
69 | * $pk = [ |
||
70 | * 'mainPk' => 1, |
||
71 | * 'secondaryPk' => 2 |
||
72 | * ]; |
||
73 | * ``` |
||
74 | * |
||
75 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
76 | * @param array|CriteriaInterface $criteria |
||
77 | * @return AnnotatedInterface|null |
||
78 | * @Ignored |
||
79 | */ |
||
80 | 40 | public function findByPk($pkValue, $criteria = null) |
|
86 | |||
87 | /** |
||
88 | * Finds document with the specified attributes. |
||
89 | * Attributes should be specified as key-value pairs. |
||
90 | * This allows easier syntax for simple queries. |
||
91 | * |
||
92 | * Example: |
||
93 | * ```php |
||
94 | * $attributes = [ |
||
95 | * 'name' => 'John', |
||
96 | * 'title' => 'dr' |
||
97 | * ]; |
||
98 | * ``` |
||
99 | * |
||
100 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
101 | * @return AnnotatedInterface|null |
||
102 | */ |
||
103 | 2 | public function findByAttributes(array $attributes) |
|
112 | |||
113 | /** |
||
114 | * Finds all documents satisfying the specified condition. |
||
115 | * See {@link find()} for detailed explanation about $condition and $params. |
||
116 | * |
||
117 | * @param array|CriteriaInterface $criteria query criteria. |
||
118 | * @return AnnotatedInterface[]|Cursor |
||
119 | */ |
||
120 | 18 | public function findAll($criteria = null) |
|
158 | |||
159 | /** |
||
160 | * Finds all documents with the specified attributes. |
||
161 | * |
||
162 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
163 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
164 | * @since v1.0 |
||
165 | */ |
||
166 | 1 | public function findAllByAttributes(array $attributes) |
|
176 | |||
177 | /** |
||
178 | * Finds all documents with the specified primary keys. |
||
179 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
180 | * field is in use as PK by default. |
||
181 | * See {@link find()} for detailed explanation about $condition. |
||
182 | * |
||
183 | * @param mixed $pkValues primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value). |
||
184 | * @param array|CriteriaInterface $criteria query criteria. |
||
185 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
186 | * @since v1.0 |
||
187 | */ |
||
188 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
195 | |||
196 | /** |
||
197 | * Counts all documents satisfying the specified condition. |
||
198 | * See {@link find()} for detailed explanation about $condition and $params. |
||
199 | * @param array|CriteriaInterface $criteria query criteria. |
||
200 | * @return integer Count of all documents satisfying the specified condition. |
||
201 | * @since v1.0 |
||
202 | */ |
||
203 | 19 | public function count($criteria = null) |
|
214 | |||
215 | /** |
||
216 | * Counts all documents found by attribute values. |
||
217 | * |
||
218 | * Example: |
||
219 | * ```php |
||
220 | * $attributes = [ |
||
221 | * 'name' => 'John', |
||
222 | * 'title' => 'dr' |
||
223 | * ]; |
||
224 | * ``` |
||
225 | * |
||
226 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
227 | * @return int |
||
228 | * @since v1.2.2 |
||
229 | * @Ignored |
||
230 | */ |
||
231 | 1 | public function countByAttributes(array $attributes) |
|
249 | |||
250 | /** |
||
251 | * Checks whether there is document satisfying the specified condition. |
||
252 | * |
||
253 | * @param CriteriaInterface $criteria |
||
254 | * @return bool |
||
255 | */ |
||
256 | 9 | public function exists(CriteriaInterface $criteria = null) |
|
279 | |||
280 | /** |
||
281 | * Resets all scopes and criteria applied including default scope. |
||
282 | * |
||
283 | * @return Finder |
||
284 | * @since v1.0 |
||
285 | */ |
||
286 | public function resetScope() |
||
291 | |||
292 | /** |
||
293 | * Whenever to use cursor |
||
294 | * @param bool $useCursor |
||
295 | * @return FinderInterface |
||
296 | */ |
||
297 | 69 | public function withCursor($useCursor = true) |
|
302 | |||
303 | 18 | public function isWithCursor() |
|
307 | |||
308 | /** |
||
309 | * Creates an model with the given attributes. |
||
310 | * This method is internally used by the find methods. |
||
311 | * @param mixed[] $data attribute values (column name=>column value) |
||
312 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
313 | * Null is returned if the input data is false. |
||
314 | * @since v1.0 |
||
315 | */ |
||
316 | 59 | protected function populateRecord($data) |
|
330 | |||
331 | /** |
||
332 | * Creates a list of documents based on the input data. |
||
333 | * This method is internally used by the find methods. |
||
334 | * @param Iterator|array $cursor Results found to populate active records. |
||
335 | * @return AnnotatedInterface[] array list of active records. |
||
336 | * @since v1.0 |
||
337 | */ |
||
338 | 17 | private function populateRecords($cursor) |
|
347 | |||
348 | } |
||
349 |
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 sub-classes 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 parent class: