1 | <?php |
||
22 | class AbstractFinder implements ModelAwareInterface |
||
23 | { |
||
24 | |||
25 | use ModelAwareTrait; |
||
26 | |||
27 | /** |
||
28 | * Whenever to use cursors |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $useCursor = false; |
||
32 | |||
33 | /** |
||
34 | * Finds a single Document with the specified condition. |
||
35 | * |
||
36 | * @param array|CriteriaInterface $criteria query criteria. |
||
37 | * @return AnnotatedInterface |
||
38 | * @Ignored |
||
39 | */ |
||
40 | 52 | public function find($criteria = null) |
|
50 | |||
51 | /** |
||
52 | * Finds document with the specified primary key. Primary key by default |
||
53 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
54 | * keys use it's value. |
||
55 | * |
||
56 | * For composite use key-value with column names as keys |
||
57 | * and values for values. |
||
58 | * |
||
59 | * Example for simple pk: |
||
60 | * ```php |
||
61 | * $pk = '51b616fcc0986e30026d0748' |
||
62 | * ``` |
||
63 | * |
||
64 | * Composite pk: |
||
65 | * ```php |
||
66 | * $pk = [ |
||
67 | * 'mainPk' => 1, |
||
68 | * 'secondaryPk' => 2 |
||
69 | * ]; |
||
70 | * ``` |
||
71 | * |
||
72 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
73 | * @param array|CriteriaInterface $criteria |
||
74 | * @return AnnotatedInterface|null |
||
75 | * @Ignored |
||
76 | */ |
||
77 | 40 | public function findByPk($pkValue, $criteria = null) |
|
83 | |||
84 | /** |
||
85 | * Finds document with the specified attributes. |
||
86 | * Attributes should be specified as key-value pairs. |
||
87 | * This allows easier syntax for simple queries. |
||
88 | * |
||
89 | * Example: |
||
90 | * ```php |
||
91 | * $attributes = [ |
||
92 | * 'name' => 'John', |
||
93 | * 'title' => 'dr' |
||
94 | * ]; |
||
95 | * ``` |
||
96 | * |
||
97 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
98 | * @return AnnotatedInterface|null |
||
99 | */ |
||
100 | 2 | public function findByAttributes(array $attributes) |
|
109 | |||
110 | /** |
||
111 | * Finds all documents satisfying the specified condition. |
||
112 | * See {@link find()} for detailed explanation about $condition and $params. |
||
113 | * |
||
114 | * @param array|CriteriaInterface $criteria query criteria. |
||
115 | * @return AnnotatedInterface[]|Cursor |
||
116 | */ |
||
117 | 18 | public function findAll($criteria = null) |
|
152 | |||
153 | /** |
||
154 | * Finds all documents with the specified attributes. |
||
155 | * |
||
156 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
157 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
158 | * @since v1.0 |
||
159 | */ |
||
160 | 1 | public function findAllByAttributes(array $attributes) |
|
170 | |||
171 | /** |
||
172 | * Finds all documents with the specified primary keys. |
||
173 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
174 | * field is in use as PK by default. |
||
175 | * See {@link find()} for detailed explanation about $condition. |
||
176 | * |
||
177 | * @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). |
||
178 | * @param array|CriteriaInterface $criteria query criteria. |
||
179 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
180 | * @since v1.0 |
||
181 | */ |
||
182 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
189 | |||
190 | /** |
||
191 | * Counts all documents satisfying the specified condition. |
||
192 | * See {@link find()} for detailed explanation about $condition and $params. |
||
193 | * @param array|CriteriaInterface $criteria query criteria. |
||
194 | * @return integer Count of all documents satisfying the specified condition. |
||
195 | * @since v1.0 |
||
196 | */ |
||
197 | 19 | public function count($criteria = null) |
|
208 | |||
209 | /** |
||
210 | * Counts all documents found by attribute values. |
||
211 | * |
||
212 | * Example: |
||
213 | * ```php |
||
214 | * $attributes = [ |
||
215 | * 'name' => 'John', |
||
216 | * 'title' => 'dr' |
||
217 | * ]; |
||
218 | * ``` |
||
219 | * |
||
220 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
221 | * @return int |
||
222 | * @since v1.2.2 |
||
223 | * @Ignored |
||
224 | */ |
||
225 | 1 | public function countByAttributes(array $attributes) |
|
243 | |||
244 | /** |
||
245 | * Checks whether there is document satisfying the specified condition. |
||
246 | * |
||
247 | * @param CriteriaInterface $criteria |
||
248 | * @return bool |
||
249 | */ |
||
250 | 9 | public function exists(CriteriaInterface $criteria = null) |
|
273 | |||
274 | /** |
||
275 | * Resets all scopes and criteria applied including default scope. |
||
276 | * |
||
277 | * @return Finder |
||
278 | * @since v1.0 |
||
279 | */ |
||
280 | public function resetScope() |
||
285 | |||
286 | /** |
||
287 | * Whenever to use cursor |
||
288 | * @param bool $useCursor |
||
289 | * @return FinderInterface |
||
290 | */ |
||
291 | 69 | public function withCursor($useCursor = true) |
|
296 | |||
297 | 18 | public function isWithCursor() |
|
301 | |||
302 | /** |
||
303 | * Creates an model with the given attributes. |
||
304 | * This method is internally used by the find methods. |
||
305 | * @param mixed[] $data attribute values (column name=>column value) |
||
306 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
307 | * Null is returned if the input data is false. |
||
308 | * @since v1.0 |
||
309 | */ |
||
310 | 59 | protected function populateRecord($data) |
|
324 | |||
325 | /** |
||
326 | * Creates a list of documents based on the input data. |
||
327 | * This method is internally used by the find methods. |
||
328 | * @param Iterator|array $cursor Results found to populate active records. |
||
329 | * @return AnnotatedInterface[] array list of active records. |
||
330 | * @since v1.0 |
||
331 | */ |
||
332 | 17 | private function populateRecords($cursor) |
|
341 | |||
342 | } |
||
343 |
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: