1 | <?php |
||
48 | class DocumentSelector extends Component implements |
||
49 | \Countable, |
||
50 | \IteratorAggregate, |
||
51 | PaginableInterface, |
||
52 | LoggerAwareInterface, |
||
53 | \JsonSerializable |
||
54 | { |
||
55 | /** |
||
56 | * Collection queries can be paginated, in addition profiling messages will be dumped into log. |
||
57 | */ |
||
58 | use LoggerTrait, PaginatorTrait; |
||
59 | |||
60 | /** |
||
61 | * Sort order. |
||
62 | * |
||
63 | * @link http://php.net/manual/en/class.mongocollection.php#mongocollection.constants.ascending |
||
64 | */ |
||
65 | const ASCENDING = 1; |
||
66 | |||
67 | /** |
||
68 | * Sort order. |
||
69 | * |
||
70 | * @link http://php.net/manual/en/class.mongocollection.php#mongocollection.constants.descending |
||
71 | */ |
||
72 | const DESCENDING = -1; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $name = ''; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | private $database = 'default'; |
||
83 | |||
84 | /** |
||
85 | * Associated MongoCollection. |
||
86 | * |
||
87 | * @var \MongoCollection |
||
88 | */ |
||
89 | private $collection = null; |
||
90 | |||
91 | /** |
||
92 | * Fields and conditions to query by. |
||
93 | * |
||
94 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $query = []; |
||
98 | |||
99 | /** |
||
100 | * Fields to sort. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $sort = []; |
||
105 | |||
106 | /** |
||
107 | * @invisible |
||
108 | * @var ODM |
||
109 | */ |
||
110 | protected $odm = null; |
||
111 | |||
112 | /** |
||
113 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
114 | * @param ODM $odm ODMManager component instance. |
||
115 | * @param string $database Associated database name/id. |
||
116 | * @param string $collection Collection name. |
||
117 | * @param array $query Fields and conditions to query by. |
||
118 | */ |
||
119 | public function __construct(ODM $odm, $database, $collection, array $query = []) |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getName() |
||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getDatabase() |
||
144 | |||
145 | /** |
||
146 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
147 | * |
||
148 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
149 | * @param array $query Fields and conditions to query by. |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function query(array $query = []) |
||
165 | |||
166 | /** |
||
167 | * Set additional query field, fields will be merged to currently existed request using |
||
168 | * array_merge. Alias for query. |
||
169 | * |
||
170 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
171 | * @param array $query Fields and conditions to query by. |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function where(array $query = []) |
||
178 | |||
179 | /** |
||
180 | * Set additional query field, fields will be merged to currently existed request using |
||
181 | * array_merge. Alias for query. |
||
182 | * |
||
183 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
184 | * @param array $query Fields and conditions to query by. |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function find(array $query = []) |
||
191 | |||
192 | /** |
||
193 | * Fetch one record from database using it's primary key. You can use INLOAD and JOIN_ONLY |
||
194 | * loaders with HAS_MANY or MANY_TO_MANY relations with this method as no limit were used. |
||
195 | * |
||
196 | * @see findOne() |
||
197 | * @param mixed $id Primary key value. |
||
198 | * @return DocumentEntity|null |
||
199 | */ |
||
200 | public function findByPK($id) |
||
204 | |||
205 | /** |
||
206 | * Select one document or it's fields from collection. |
||
207 | * |
||
208 | * @param array $query Fields and conditions to query by. |
||
209 | * @return DocumentEntity|array |
||
210 | */ |
||
211 | public function findOne(array $query = []) |
||
215 | |||
216 | /** |
||
217 | * Current fields and conditions to query by. |
||
218 | * |
||
219 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getQuery() |
||
226 | |||
227 | /** |
||
228 | * Sorts the results by given fields. |
||
229 | * |
||
230 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
231 | * @param array $fields An array of fields by which to sort. Each element in the array has as |
||
232 | * key the field name, and as value either 1 for ascending sort, or -1 for |
||
233 | * descending sort. |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function sortBy(array $fields) |
||
242 | |||
243 | /** |
||
244 | * Fetch all available document instances from query. |
||
245 | * |
||
246 | * @return Document[] |
||
247 | */ |
||
248 | public function fetchDocuments() |
||
257 | |||
258 | /** |
||
259 | * Fetch all available documents as arrays of fields. |
||
260 | * |
||
261 | * @param array $fields Fields of the results to return. |
||
262 | * @return array |
||
263 | */ |
||
264 | public function fetchFields($fields = []) |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function count() |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | * |
||
285 | * @return DocumentCursor|Document[] |
||
286 | */ |
||
287 | public function getIterator() |
||
291 | |||
292 | /** |
||
293 | * Get instance of DocumentCursor. |
||
294 | * |
||
295 | * @return DocumentCursor |
||
296 | */ |
||
297 | public function getCursor() |
||
301 | |||
302 | /** |
||
303 | * Bypass call to MongoCollection. |
||
304 | * |
||
305 | * @param string $method Method name. |
||
306 | * @param array $arguments Method arguments. |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function __call($method, array $arguments = []) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function jsonSerialize() |
||
321 | |||
322 | /** |
||
323 | * Destructing. |
||
324 | */ |
||
325 | public function __destruct() |
||
330 | |||
331 | /** |
||
332 | * @return Object |
||
333 | */ |
||
334 | public function __debugInfo() |
||
344 | |||
345 | /** |
||
346 | * Create CursorReader based on stored query, limits and sorting. |
||
347 | * |
||
348 | * @param array $query Fields and conditions to query by. |
||
349 | * @param array $fields Fields of the results to return. |
||
350 | * @param int|null $limit Custom limit value. |
||
351 | * @return DocumentCursor |
||
352 | * @throws \MongoException |
||
353 | */ |
||
354 | protected function createCursor($query = [], $fields = [], $limit = null) |
||
404 | |||
405 | /** |
||
406 | * Associated document class. |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | protected function primaryDocument() |
||
414 | |||
415 | /** |
||
416 | * MongoDatabase instance. |
||
417 | * |
||
418 | * @return MongoDatabase |
||
419 | */ |
||
420 | protected function mongoDatabase() |
||
424 | |||
425 | /** |
||
426 | * Get associated mongo collection. |
||
427 | * |
||
428 | * @return \MongoCollection |
||
429 | */ |
||
430 | protected function mongoCollection() |
||
438 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.