1 | <?php |
||
32 | class Finder implements FinderInterface |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Model |
||
37 | * @var AnnotatedInterface |
||
38 | */ |
||
39 | public $model = null; |
||
40 | |||
41 | /** |
||
42 | * Mangan instance |
||
43 | * @var Mangan |
||
44 | */ |
||
45 | private $mn = null; |
||
46 | |||
47 | /** |
||
48 | * Entity manager instance |
||
49 | * @var EntityManagerInterface |
||
50 | */ |
||
51 | private $em = null; |
||
52 | |||
53 | /** |
||
54 | * Scope manager instance |
||
55 | * @var ScopeManager |
||
56 | */ |
||
57 | private $sm = null; |
||
58 | |||
59 | /** |
||
60 | * Finder criteria |
||
61 | * @var Criteria |
||
62 | */ |
||
63 | private $_criteria = null; |
||
64 | |||
65 | /** |
||
66 | * Whenever to use corsors |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $_useCursor = false; |
||
70 | |||
71 | /** |
||
72 | * Constructor |
||
73 | * |
||
74 | * @param object $model Model instance |
||
75 | * @param EntityManagerInterface $em |
||
76 | * @param Mangan $mangan |
||
77 | */ |
||
78 | 67 | public function __construct($model, $em = null, $mangan = null) |
|
79 | { |
||
80 | 67 | $this->model = $model; |
|
81 | 67 | $this->sm = new ScopeManager($model); |
|
82 | 67 | if (null === $mangan) |
|
83 | { |
||
84 | 67 | $mangan = Mangan::fromModel($model); |
|
85 | } |
||
86 | 67 | $this->em = $em ?: EntityManager::create($model, $mangan); |
|
87 | 67 | $this->mn = $mangan; |
|
88 | 67 | $this->withCursor($this->mn->useCursor); |
|
89 | 67 | } |
|
90 | |||
91 | /** |
||
92 | * Create model related finder. |
||
93 | * This will create customized finder if defined in model with Finder annotation. |
||
94 | * If no custom finder is defined this will return default Finder. |
||
95 | * |
||
96 | * @param AnnotatedInterface $model |
||
97 | * @param EntityManagerInterface $em |
||
98 | * @param Mangan $mangan |
||
99 | * @return FinderInterface |
||
100 | */ |
||
101 | 9 | public static function create(AnnotatedInterface $model, $em = null, Mangan $mangan = null) |
|
106 | |||
107 | /** |
||
108 | * Finds a single Document with the specified condition. |
||
109 | * |
||
110 | * @param array|CriteriaInterface $criteria query criteria. |
||
111 | * @return AnnotatedInterface |
||
112 | * @Ignored |
||
113 | */ |
||
114 | 52 | public function find($criteria = null) |
|
115 | { |
||
116 | 52 | if ($this->_beforeFind()) |
|
117 | { |
||
118 | 52 | $criteria = $this->sm->apply($criteria); |
|
119 | 52 | $criteria->decorateWith($this->model); |
|
120 | 52 | $data = $this->em->getCollection()->findOne($criteria->getConditions(), $criteria->getSelect()); |
|
121 | 52 | return $this->populateRecord($data); |
|
122 | } |
||
123 | return null; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Finds document with the specified primary key. Primary key by default |
||
128 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
129 | * keys use it's value. |
||
130 | * |
||
131 | * For composite use key-value with column names as keys |
||
132 | * and values for values. |
||
133 | * |
||
134 | * Example for simple pk: |
||
135 | * ```php |
||
136 | * $pk = '51b616fcc0986e30026d0748' |
||
137 | * ``` |
||
138 | * |
||
139 | * Composite pk: |
||
140 | * ```php |
||
141 | * $pk = [ |
||
142 | * 'mainPk' => 1, |
||
143 | * 'secondaryPk' => 2 |
||
144 | * ]; |
||
145 | * ``` |
||
146 | * |
||
147 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
148 | * @param array|CriteriaInterface $criteria |
||
149 | * @return AnnotatedInterface|null |
||
150 | * @Ignored |
||
151 | */ |
||
152 | 40 | public function findByPk($pkValue, $criteria = null) |
|
160 | |||
161 | /** |
||
162 | * Finds document with the specified attributes. |
||
163 | * Attributes should be specified as key-value pairs. |
||
164 | * This allows easier syntax for simple queries. |
||
165 | * |
||
166 | * Example: |
||
167 | * ```php |
||
168 | * $attributes = [ |
||
169 | * 'name' => 'John', |
||
170 | * 'title' => 'dr' |
||
171 | * ]; |
||
172 | * ``` |
||
173 | * |
||
174 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
175 | * @return AnnotatedInterface|null |
||
176 | */ |
||
177 | 2 | public function findByAttributes(array $attributes) |
|
187 | |||
188 | /** |
||
189 | * Finds all documents satisfying the specified condition. |
||
190 | * See {@link find()} for detailed explanation about $condition and $params. |
||
191 | * |
||
192 | * @param array|CriteriaInterface $criteria query criteria. |
||
193 | * @return AnnotatedInterface[]|Cursor |
||
194 | */ |
||
195 | 18 | public function findAll($criteria = null) |
|
231 | |||
232 | /** |
||
233 | * Finds all documents with the specified attributes. |
||
234 | * |
||
235 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
236 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
237 | * @since v1.0 |
||
238 | */ |
||
239 | 1 | public function findAllByAttributes(array $attributes) |
|
250 | |||
251 | /** |
||
252 | * Finds all documents with the specified primary keys. |
||
253 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
254 | * field is in use as PK by default. |
||
255 | * See {@link find()} for detailed explanation about $condition. |
||
256 | * |
||
257 | * @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). |
||
258 | * @param array|CriteriaInterface $criteria query criteria. |
||
259 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
260 | * @since v1.0 |
||
261 | */ |
||
262 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
270 | |||
271 | /** |
||
272 | * Counts all documents satisfying the specified condition. |
||
273 | * See {@link find()} for detailed explanation about $condition and $params. |
||
274 | * @param array|CriteriaInterface $criteria query criteria. |
||
275 | * @return integer Count of all documents satisfying the specified condition. |
||
276 | * @since v1.0 |
||
277 | */ |
||
278 | 19 | public function count($criteria = null) |
|
290 | |||
291 | /** |
||
292 | * Counts all documents found by attribute values. |
||
293 | * |
||
294 | * Example: |
||
295 | * ```php |
||
296 | * $attributes = [ |
||
297 | * 'name' => 'John', |
||
298 | * 'title' => 'dr' |
||
299 | * ]; |
||
300 | * ``` |
||
301 | * |
||
302 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
303 | * @return int |
||
304 | * @since v1.2.2 |
||
305 | * @Ignored |
||
306 | */ |
||
307 | 1 | public function countByAttributes(array $attributes) |
|
326 | |||
327 | /** |
||
328 | * Checks whether there is document satisfying the specified condition. |
||
329 | * |
||
330 | * @param CriteriaInterface $criteria |
||
331 | * @return bool |
||
332 | */ |
||
333 | 7 | public function exists(CriteriaInterface $criteria = null) |
|
354 | |||
355 | /** |
||
356 | * Whenever to use cursor |
||
357 | * @param bool $useCursor |
||
358 | * @return FinderInterface |
||
359 | */ |
||
360 | 67 | public function withCursor($useCursor = true) |
|
365 | |||
366 | /** |
||
367 | * Resets all scopes and criteria applied including default scope. |
||
368 | * |
||
369 | * @return Finder |
||
370 | * @since v1.0 |
||
371 | */ |
||
372 | public function resetScope() |
||
378 | |||
379 | /** |
||
380 | * Creates an model with the given attributes. |
||
381 | * This method is internally used by the find methods. |
||
382 | * @param mixed[] $data attribute values (column name=>column value) |
||
383 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
384 | * Null is returned if the input data is false. |
||
385 | * @since v1.0 |
||
386 | */ |
||
387 | 59 | protected function populateRecord($data) |
|
401 | |||
402 | /** |
||
403 | * Creates a list of documents based on the input data. |
||
404 | * This method is internally used by the find methods. |
||
405 | * @param MongoCursor $cursor Results found to populate active records. |
||
406 | * @return AnnotatedInterface[] array list of active records. |
||
407 | * @since v1.0 |
||
408 | */ |
||
409 | 17 | protected function populateRecords(MongoCursor $cursor) |
|
418 | |||
419 | /** |
||
420 | * Trigger before find event |
||
421 | * @return boolean |
||
422 | */ |
||
423 | 60 | private function _beforeFind() |
|
431 | |||
432 | /** |
||
433 | * Trigger before count event |
||
434 | * @return boolean |
||
435 | */ |
||
436 | 19 | private function _beforeCount() |
|
444 | |||
445 | /** |
||
446 | * Trigger before exists event |
||
447 | * @return boolean |
||
448 | */ |
||
449 | 7 | private function _beforeExists() |
|
457 | |||
458 | } |
||
459 |