1 | <?php |
||
22 | class DocumentSelector extends Component implements |
||
23 | \Countable, |
||
24 | \IteratorAggregate, |
||
25 | \JsonSerializable, |
||
26 | PaginatorAwareInterface |
||
27 | { |
||
28 | use LimitsTrait, PaginatorTrait; |
||
29 | |||
30 | /** |
||
31 | * Sort orders. |
||
32 | */ |
||
33 | const ASCENDING = 1; |
||
34 | const DESCENDING = -1; |
||
35 | |||
36 | /** |
||
37 | * @var Collection |
||
38 | */ |
||
39 | private $collection; |
||
40 | |||
41 | /** |
||
42 | * Document class being selected. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $class; |
||
47 | |||
48 | /** |
||
49 | * @var ODMInterface |
||
50 | */ |
||
51 | private $odm; |
||
52 | |||
53 | /** |
||
54 | * Fields and conditions to query by. |
||
55 | * |
||
56 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $query = []; |
||
61 | |||
62 | /** |
||
63 | * Fields to sort. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $sort = []; |
||
68 | |||
69 | /** |
||
70 | * @param Collection $collection |
||
71 | * @param string $class |
||
72 | * @param ODMInterface $odm |
||
73 | */ |
||
74 | public function __construct(Collection $collection, string $class, ODMInterface $odm) |
||
80 | |||
81 | /** |
||
82 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
83 | * Alias for query. |
||
84 | * |
||
85 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
86 | * |
||
87 | * @see query() |
||
88 | * |
||
89 | * @param array $query Fields and conditions to query by. |
||
90 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
91 | * MongoDate. |
||
92 | * |
||
93 | * @return self|$this |
||
94 | */ |
||
95 | public function find(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
99 | |||
100 | /** |
||
101 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
102 | * Alias for query. |
||
103 | * |
||
104 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
105 | * |
||
106 | * @see query() |
||
107 | * |
||
108 | * @param array $query Fields and conditions to query by. |
||
109 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
110 | * MongoDate. |
||
111 | * |
||
112 | * @return self|$this |
||
113 | */ |
||
114 | public function where(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
124 | |||
125 | /** |
||
126 | * Sorts the results by given fields. |
||
127 | * |
||
128 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
129 | * |
||
130 | * @param array $fields An array of fields by which to sort. Each element in the array has as |
||
131 | * key the field name, and as value either 1 for ascending sort, or -1 for |
||
132 | * descending sort. |
||
133 | * |
||
134 | * @return self|$this |
||
135 | */ |
||
136 | public function sortBy(array $fields): DocumentSelector |
||
142 | |||
143 | /** |
||
144 | * Alias for sortBy. |
||
145 | * |
||
146 | * @param string $field |
||
147 | * @param int $direction |
||
148 | * |
||
149 | * @return self|$this |
||
150 | */ |
||
151 | public function orderBy(string $field, int $direction = self::ASCENDING): DocumentSelector |
||
155 | |||
156 | /** |
||
157 | * Select one document or it's fields from collection. |
||
158 | * |
||
159 | * @param array $query Fields and conditions to query by. Query will not be added to an |
||
160 | * existed query array. |
||
161 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
162 | * MongoDate. |
||
163 | * |
||
164 | * @return CompositableInterface|null |
||
165 | */ |
||
166 | public function findOne(array $query = [], bool $normalizeDates = true) |
||
183 | |||
184 | /** |
||
185 | * Count collection. Attention, this method depends on current values set for limit and offset! |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | public function count(): int |
||
197 | |||
198 | /** |
||
199 | * Create cursor with partial selection. |
||
200 | * |
||
201 | * Example: $selector->fetchFields(['name', ...])->toArray(); |
||
202 | * |
||
203 | * @param array $fields |
||
204 | * |
||
205 | * @return Cursor |
||
206 | */ |
||
207 | public function fetchFields(array $fields): Cursor |
||
211 | |||
212 | /** |
||
213 | * Fetch all documents. |
||
214 | * |
||
215 | * @return CompositableInterface[] |
||
216 | */ |
||
217 | public function fetchAll(): array |
||
221 | |||
222 | /** |
||
223 | * Create selection and wrap it using DocumentCursor. |
||
224 | * |
||
225 | * @return DocumentCursor |
||
226 | */ |
||
227 | public function getIterator(): DocumentCursor |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function jsonSerialize() |
||
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | public function __debugInfo() |
||
258 | |||
259 | /** |
||
260 | * Destructing. |
||
261 | */ |
||
262 | public function __destruct() |
||
270 | |||
271 | /** |
||
272 | * @param array $fields Fields to be selected (keep empty to select all). |
||
273 | * |
||
274 | * @return Cursor |
||
275 | */ |
||
276 | protected function createCursor(array $fields = []) |
||
284 | |||
285 | /** |
||
286 | * Options to be send to find() method of MongoDB\Collection. Options are based on how selector |
||
287 | * was configured. |
||
288 | ** |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | protected function createOptions(): array |
||
305 | |||
306 | /** |
||
307 | * Converts DateTime objects into MongoDatetime. |
||
308 | * |
||
309 | * @param array $query |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | protected function normalizeDates(array $query): array |
||
324 | |||
325 | /** |
||
326 | * @return \Interop\Container\ContainerInterface|null |
||
327 | */ |
||
328 | protected function iocContainer() |
||
337 | } |
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.