1 | <?php |
||
42 | abstract class AbstractFinder implements FinderInterface, ModelAwareInterface |
||
43 | { |
||
44 | |||
45 | use ModelAwareTrait; |
||
46 | |||
47 | /** |
||
48 | * Whenever to use cursors |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $useCursor = false; |
||
52 | |||
53 | // <editor-fold defaultstate="collapsed" desc="Required getters/setters"> |
||
54 | /** |
||
55 | * @see FinderHelpers |
||
56 | * @return FinderAdapterInterface|MongoAdapter |
||
57 | */ |
||
58 | abstract public function getAdapter(); |
||
59 | |||
60 | /** |
||
61 | * @see FinderHelpers |
||
62 | * @return ScopeManagerInterface |
||
63 | */ |
||
64 | abstract public function getScopeManager(); |
||
65 | |||
66 | /** |
||
67 | * @see FinderHelpers |
||
68 | * @return FinderEventsInterface |
||
69 | */ |
||
70 | abstract public function getFinderEvents(); |
||
71 | |||
72 | /** |
||
73 | * @see FinderHelpers |
||
74 | * @return ProfilerInterface |
||
75 | */ |
||
76 | abstract public function getProfiler(); |
||
77 | |||
78 | /** |
||
79 | * @see FinderHelpers |
||
80 | * @return static |
||
81 | */ |
||
82 | abstract public function setAdapter(FinderAdapterInterface $adapter); |
||
83 | |||
84 | /** |
||
85 | * @see FinderHelpers |
||
86 | * @return static |
||
87 | */ |
||
88 | abstract public function setScopeManager(ScopeManagerInterface $scopeManager); |
||
89 | |||
90 | /** |
||
91 | * @see FinderHelpers |
||
92 | * @return static |
||
93 | */ |
||
94 | abstract public function setFinderEvents(FinderEventsInterface $finderEvents); |
||
95 | |||
96 | /** |
||
97 | * @see FinderHelpers |
||
98 | * @return static |
||
99 | */ |
||
100 | abstract public function setProfiler(ProfilerInterface $profiler); |
||
101 | // </editor-fold> |
||
102 | |||
103 | /** |
||
104 | * Finds a single Document with the specified condition. |
||
105 | * |
||
106 | * @param array|CriteriaInterface $criteria query criteria. |
||
107 | * @return AnnotatedInterface |
||
108 | * @Ignored |
||
109 | */ |
||
110 | 65 | public function find($criteria = null) |
|
111 | { |
||
112 | 65 | if ($this->getFinderEvents()->beforeFind($this)) |
|
113 | 65 | { |
|
114 | 65 | $criteria = $this->getScopeManager()->apply($criteria); |
|
115 | 65 | $data = $this->getAdapter()->findOne($criteria, $criteria->getSelect()); |
|
116 | 65 | return $this->populateRecord($data); |
|
117 | } |
||
118 | return null; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Finds document with the specified primary key. Primary key by default |
||
123 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
124 | * keys use it's value. |
||
125 | * |
||
126 | * For composite use key-value with column names as keys |
||
127 | * and values for values. |
||
128 | * |
||
129 | * Example for simple pk: |
||
130 | * ```php |
||
131 | * $pk = '51b616fcc0986e30026d0748' |
||
132 | * ``` |
||
133 | * |
||
134 | * Composite pk: |
||
135 | * ```php |
||
136 | * $pk = [ |
||
137 | * 'mainPk' => 1, |
||
138 | * 'secondaryPk' => 2 |
||
139 | * ]; |
||
140 | * ``` |
||
141 | * |
||
142 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
143 | * @param array|CriteriaInterface $criteria |
||
144 | * @return AnnotatedInterface|null |
||
145 | * @Ignored |
||
146 | */ |
||
147 | 46 | public function findByPk($pkValue, $criteria = null) |
|
153 | |||
154 | /** |
||
155 | * Finds document with the specified attributes. |
||
156 | * Attributes should be specified as key-value pairs. |
||
157 | * This allows easier syntax for simple queries. |
||
158 | * |
||
159 | * Example: |
||
160 | * ```php |
||
161 | * $attributes = [ |
||
162 | * 'name' => 'John', |
||
163 | * 'title' => 'dr' |
||
164 | * ]; |
||
165 | * ``` |
||
166 | * |
||
167 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
168 | * @return AnnotatedInterface|null |
||
169 | */ |
||
170 | 2 | public function findByAttributes(array $attributes) |
|
171 | { |
||
172 | 2 | $criteria = $this->getScopeManager()->getNewCriteria(); |
|
173 | 2 | foreach ($attributes as $name => $value) |
|
174 | { |
||
175 | 2 | $criteria->addCond($name, '==', $value); |
|
176 | 2 | } |
|
177 | 2 | return $this->find($criteria); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Finds all documents satisfying the specified condition. |
||
182 | * See {@link find()} for detailed explanation about $condition and $params. |
||
183 | * |
||
184 | * @param array|CriteriaInterface $criteria query criteria. |
||
185 | * @return AnnotatedInterface[]|Cursor |
||
186 | */ |
||
187 | 23 | public function findAll($criteria = null) |
|
188 | { |
||
189 | 23 | if ($this->getFinderEvents()->beforeFind($this)) |
|
190 | 23 | { |
|
191 | 21 | $criteria = $this->getScopeManager()->apply($criteria); |
|
192 | 21 | $cursor = $this->getAdapter()->findMany($criteria); |
|
193 | |||
194 | 21 | assert(is_object($cursor), sprintf('Expected cursor to be compatible object, got %s', gettype($cursor))); |
|
195 | 21 | assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor)))); |
|
196 | |||
197 | 21 | if ($criteria->getSort() !== null) |
|
198 | 21 | { |
|
199 | 21 | $cursor->sort($criteria->getSort()); |
|
200 | 21 | } |
|
201 | 21 | if ($criteria->getLimit() !== null) |
|
202 | 21 | { |
|
203 | 4 | $cursor->limit($criteria->getLimit()); |
|
204 | 4 | } |
|
205 | 21 | if ($criteria->getOffset() !== null) |
|
206 | 21 | { |
|
207 | 4 | $cursor->skip($criteria->getOffset()); |
|
208 | 4 | } |
|
209 | 21 | if ($criteria->getSelect()) |
|
210 | 21 | { |
|
211 | 4 | $cursor->fields($criteria->getSelect()); |
|
212 | 4 | } |
|
213 | 21 | $this->getProfiler()->cursor($cursor); |
|
214 | 21 | if ($this->isWithCursor()) |
|
215 | 21 | { |
|
216 | 1 | return new Cursor($cursor, $this->getModel()); |
|
217 | } |
||
218 | else |
||
219 | { |
||
220 | 20 | return $this->populateRecords($cursor); |
|
221 | } |
||
222 | } |
||
223 | 2 | return []; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Finds all documents with the specified attributes. |
||
228 | * |
||
229 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
230 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
231 | * @since v1.0 |
||
232 | */ |
||
233 | 1 | public function findAllByAttributes(array $attributes) |
|
234 | { |
||
235 | 1 | $criteria = $this->getScopeManager()->getNewCriteria(); |
|
236 | 1 | foreach ($attributes as $name => $value) |
|
237 | { |
||
238 | 1 | $criteria->$name('==', $value); |
|
239 | 1 | } |
|
240 | |||
241 | 1 | return $this->findAll($criteria); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Finds all documents with the specified primary keys. |
||
246 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
247 | * field is in use as PK by default. |
||
248 | * See {@link find()} for detailed explanation about $condition. |
||
249 | * |
||
250 | * @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). |
||
251 | * @param array|CriteriaInterface $criteria query criteria. |
||
252 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
253 | * @since v1.0 |
||
254 | */ |
||
255 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
262 | |||
263 | /** |
||
264 | * Counts all documents satisfying the specified condition. |
||
265 | * See {@link find()} for detailed explanation about $condition and $params. |
||
266 | * @param array|CriteriaInterface $criteria query criteria. |
||
267 | * @return integer Count of all documents satisfying the specified condition. |
||
268 | * @since v1.0 |
||
269 | */ |
||
270 | 24 | public function count($criteria = null) |
|
271 | { |
||
272 | 24 | if ($this->getFinderEvents()->beforeCount($this)) |
|
273 | 24 | { |
|
274 | 24 | $criteria = $this->getScopeManager()->apply($criteria); |
|
275 | 24 | $count = $this->getAdapter()->count($criteria); |
|
276 | 24 | $this->getFinderEvents()->afterCount($this); |
|
277 | 24 | return $count; |
|
278 | } |
||
279 | return 0; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Counts all documents found by attribute values. |
||
284 | * |
||
285 | * Example: |
||
286 | * ```php |
||
287 | * $attributes = [ |
||
288 | * 'name' => 'John', |
||
289 | * 'title' => 'dr' |
||
290 | * ]; |
||
291 | * ``` |
||
292 | * |
||
293 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
294 | * @return int |
||
295 | * @since v1.2.2 |
||
296 | * @Ignored |
||
297 | */ |
||
298 | 1 | public function countByAttributes(array $attributes) |
|
299 | { |
||
300 | 1 | if ($this->getFinderEvents()->beforeCount($this)) |
|
301 | 1 | { |
|
302 | 1 | $criteria = $this->getScopeManager()->getNewCriteria(); |
|
303 | 1 | foreach ($attributes as $name => $value) |
|
304 | { |
||
305 | 1 | $criteria->$name = $value; |
|
306 | 1 | } |
|
307 | |||
308 | 1 | $scopedCriteria = $this->getScopeManager()->apply($criteria); |
|
309 | |||
310 | 1 | $count = $this->getAdapter()->count($scopedCriteria); |
|
311 | 1 | $this->getFinderEvents()->afterCount($this); |
|
312 | 1 | return $count; |
|
313 | } |
||
314 | return 0; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Checks whether there is document satisfying the specified condition. |
||
319 | * |
||
320 | * @param CriteriaInterface $criteria |
||
321 | * @return bool |
||
322 | */ |
||
323 | 9 | public function exists(CriteriaInterface $criteria = null) |
|
324 | { |
||
325 | 9 | if ($this->getFinderEvents()->beforeExists($this)) |
|
326 | 9 | { |
|
327 | 9 | $criteria = $this->getScopeManager()->apply($criteria); |
|
328 | |||
329 | //Select only Pk Fields to not fetch possibly large document |
||
330 | 9 | $pkKeys = PkManager::getPkKeys($this->getModel()); |
|
331 | 9 | if (is_string($pkKeys)) |
|
332 | 9 | { |
|
333 | 7 | $pkKeys = [$pkKeys]; |
|
334 | 7 | } |
|
335 | 9 | $cursor = $this->getAdapter()->findMany($criteria, $pkKeys); |
|
336 | 9 | $cursor->limit(1); |
|
337 | |||
338 | // NOTE: Cannot use count(true) here because of hhvm mongofill compatibility, see: |
||
339 | // https://github.com/mongofill/mongofill/issues/86 |
||
340 | 9 | $exists = ($cursor->count() !== 0); |
|
341 | 9 | $this->getFinderEvents()->afterExists($this); |
|
342 | 9 | return $exists; |
|
343 | } |
||
344 | return false; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Resets all scopes and criteria applied including default scope. |
||
349 | * |
||
350 | * @return Finder |
||
351 | * @since v1.0 |
||
352 | */ |
||
353 | public function resetScope() |
||
358 | |||
359 | /** |
||
360 | * Whenever to use cursor |
||
361 | * @param bool $useCursor |
||
362 | * @return FinderInterface |
||
363 | */ |
||
364 | 87 | public function withCursor($useCursor = true) |
|
369 | |||
370 | 21 | public function isWithCursor() |
|
374 | |||
375 | /** |
||
376 | * Creates an model with the given attributes. |
||
377 | * This method is internally used by the find methods. |
||
378 | * @param mixed[] $data attribute values (column name=>column value) |
||
379 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
380 | * Null is returned if the input data is false. |
||
381 | * @since v1.0 |
||
382 | */ |
||
383 | 75 | protected function populateRecord($data) |
|
397 | |||
398 | /** |
||
399 | * Creates a list of documents based on the input data. |
||
400 | * This method is internally used by the find methods. |
||
401 | * @param Iterator|array $cursor Results found to populate active records. |
||
402 | * @return AnnotatedInterface[] array list of active records. |
||
403 | * @since v1.0 |
||
404 | */ |
||
405 | 20 | private function populateRecords($cursor) |
|
414 | |||
415 | } |
||
416 |