1 | <?php |
||
41 | abstract class AbstractFinder implements FinderInterface, ModelAwareInterface |
||
42 | { |
||
43 | |||
44 | use ModelAwareTrait; |
||
45 | |||
46 | /** |
||
47 | * Whenever to use cursors |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $useCursor = false; |
||
51 | |||
52 | // <editor-fold defaultstate="collapsed" desc="Required getters/setters"> |
||
53 | /** |
||
54 | * @see FinderHelpers |
||
55 | * @return FinderAdapterInterface |
||
56 | */ |
||
57 | abstract public function getAdapter(); |
||
58 | |||
59 | /** |
||
60 | * @see FinderHelpers |
||
61 | * @return ScopeManagerInterface |
||
62 | */ |
||
63 | abstract public function getScopeManager(); |
||
64 | |||
65 | /** |
||
66 | * @see FinderHelpers |
||
67 | * @return FinderEventsInterface |
||
68 | */ |
||
69 | abstract public function getFinderEvents(); |
||
70 | |||
71 | /** |
||
72 | * @see FinderHelpers |
||
73 | * @return ProfilerInterface |
||
74 | */ |
||
75 | abstract public function getProfiler(); |
||
76 | |||
77 | /** |
||
78 | * @see FinderHelpers |
||
79 | * @return static |
||
80 | */ |
||
81 | abstract public function setAdapter(FinderAdapterInterface $adapter); |
||
82 | |||
83 | /** |
||
84 | * @see FinderHelpers |
||
85 | * @return static |
||
86 | */ |
||
87 | abstract public function setScopeManager(ScopeManagerInterface $scopeManager); |
||
88 | |||
89 | /** |
||
90 | * @see FinderHelpers |
||
91 | * @return static |
||
92 | */ |
||
93 | abstract public function setFinderEvents(FinderEventsInterface $finderEvents); |
||
94 | |||
95 | /** |
||
96 | * @see FinderHelpers |
||
97 | * @return static |
||
98 | */ |
||
99 | abstract public function setProfiler(ProfilerInterface $profiler); |
||
100 | // </editor-fold> |
||
101 | |||
102 | /** |
||
103 | * Finds a single Document with the specified condition. |
||
104 | * |
||
105 | * @param array|CriteriaInterface $criteria query criteria. |
||
106 | * @return AnnotatedInterface |
||
107 | * @Ignored |
||
108 | */ |
||
109 | 52 | public function find($criteria = null) |
|
119 | |||
120 | /** |
||
121 | * Finds document with the specified primary key. Primary key by default |
||
122 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
123 | * keys use it's value. |
||
124 | * |
||
125 | * For composite use key-value with column names as keys |
||
126 | * and values for values. |
||
127 | * |
||
128 | * Example for simple pk: |
||
129 | * ```php |
||
130 | * $pk = '51b616fcc0986e30026d0748' |
||
131 | * ``` |
||
132 | * |
||
133 | * Composite pk: |
||
134 | * ```php |
||
135 | * $pk = [ |
||
136 | * 'mainPk' => 1, |
||
137 | * 'secondaryPk' => 2 |
||
138 | * ]; |
||
139 | * ``` |
||
140 | * |
||
141 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
142 | * @param array|CriteriaInterface $criteria |
||
143 | * @return AnnotatedInterface|null |
||
144 | * @Ignored |
||
145 | */ |
||
146 | 40 | public function findByPk($pkValue, $criteria = null) |
|
152 | |||
153 | /** |
||
154 | * Finds document with the specified attributes. |
||
155 | * Attributes should be specified as key-value pairs. |
||
156 | * This allows easier syntax for simple queries. |
||
157 | * |
||
158 | * Example: |
||
159 | * ```php |
||
160 | * $attributes = [ |
||
161 | * 'name' => 'John', |
||
162 | * 'title' => 'dr' |
||
163 | * ]; |
||
164 | * ``` |
||
165 | * |
||
166 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
167 | * @return AnnotatedInterface|null |
||
168 | */ |
||
169 | 2 | public function findByAttributes(array $attributes) |
|
178 | |||
179 | /** |
||
180 | * Finds all documents satisfying the specified condition. |
||
181 | * See {@link find()} for detailed explanation about $condition and $params. |
||
182 | * |
||
183 | * @param array|CriteriaInterface $criteria query criteria. |
||
184 | * @return AnnotatedInterface[]|Cursor |
||
185 | */ |
||
186 | 21 | public function findAll($criteria = null) |
|
224 | |||
225 | /** |
||
226 | * Finds all documents with the specified attributes. |
||
227 | * |
||
228 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
229 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
230 | * @since v1.0 |
||
231 | */ |
||
232 | 1 | public function findAllByAttributes(array $attributes) |
|
242 | |||
243 | /** |
||
244 | * Finds all documents with the specified primary keys. |
||
245 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
246 | * field is in use as PK by default. |
||
247 | * See {@link find()} for detailed explanation about $condition. |
||
248 | * |
||
249 | * @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). |
||
250 | * @param array|CriteriaInterface $criteria query criteria. |
||
251 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
252 | * @since v1.0 |
||
253 | */ |
||
254 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
261 | |||
262 | /** |
||
263 | * Counts all documents satisfying the specified condition. |
||
264 | * See {@link find()} for detailed explanation about $condition and $params. |
||
265 | * @param array|CriteriaInterface $criteria query criteria. |
||
266 | * @return integer Count of all documents satisfying the specified condition. |
||
267 | * @since v1.0 |
||
268 | */ |
||
269 | 22 | public function count($criteria = null) |
|
280 | |||
281 | /** |
||
282 | * Counts all documents found by attribute values. |
||
283 | * |
||
284 | * Example: |
||
285 | * ```php |
||
286 | * $attributes = [ |
||
287 | * 'name' => 'John', |
||
288 | * 'title' => 'dr' |
||
289 | * ]; |
||
290 | * ``` |
||
291 | * |
||
292 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
293 | * @return int |
||
294 | * @since v1.2.2 |
||
295 | * @Ignored |
||
296 | */ |
||
297 | 1 | public function countByAttributes(array $attributes) |
|
315 | |||
316 | /** |
||
317 | * Checks whether there is document satisfying the specified condition. |
||
318 | * |
||
319 | * @param CriteriaInterface $criteria |
||
320 | * @return bool |
||
321 | */ |
||
322 | 9 | public function exists(CriteriaInterface $criteria = null) |
|
345 | |||
346 | /** |
||
347 | * Resets all scopes and criteria applied including default scope. |
||
348 | * |
||
349 | * @return Finder |
||
350 | * @since v1.0 |
||
351 | */ |
||
352 | public function resetScope() |
||
357 | |||
358 | /** |
||
359 | * Whenever to use cursor |
||
360 | * @param bool $useCursor |
||
361 | * @return FinderInterface |
||
362 | */ |
||
363 | 72 | public function withCursor($useCursor = true) |
|
368 | |||
369 | 21 | public function isWithCursor() |
|
373 | |||
374 | /** |
||
375 | * Creates an model with the given attributes. |
||
376 | * This method is internally used by the find methods. |
||
377 | * @param mixed[] $data attribute values (column name=>column value) |
||
378 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
379 | * Null is returned if the input data is false. |
||
380 | * @since v1.0 |
||
381 | */ |
||
382 | 62 | protected function populateRecord($data) |
|
396 | |||
397 | /** |
||
398 | * Creates a list of documents based on the input data. |
||
399 | * This method is internally used by the find methods. |
||
400 | * @param Iterator|array $cursor Results found to populate active records. |
||
401 | * @return AnnotatedInterface[] array list of active records. |
||
402 | * @since v1.0 |
||
403 | */ |
||
404 | 20 | private function populateRecords($cursor) |
|
413 | |||
414 | } |
||
415 |