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 | * Default selection type map. |
||
38 | */ |
||
39 | const TYPE_MAP = [ |
||
40 | 'root' => 'array', |
||
41 | 'document' => 'array', |
||
42 | 'array' => 'array' |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @var Collection |
||
47 | */ |
||
48 | private $collection; |
||
49 | |||
50 | /** |
||
51 | * Document class being selected. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $class; |
||
56 | |||
57 | /** |
||
58 | * @var ODMInterface |
||
59 | */ |
||
60 | private $odm; |
||
61 | |||
62 | /** |
||
63 | * Fields and conditions to query by. |
||
64 | * |
||
65 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | private $query = []; |
||
70 | |||
71 | /** |
||
72 | * Fields to sort. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private $sort = []; |
||
77 | |||
78 | /** |
||
79 | * @param Collection $collection |
||
80 | * @param string $class |
||
81 | * @param ODMInterface $odm |
||
82 | */ |
||
83 | public function __construct(Collection $collection, string $class, ODMInterface $odm) |
||
89 | |||
90 | /** |
||
91 | * Associated class name. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getClass(): string |
||
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 find(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
118 | |||
119 | /** |
||
120 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
121 | * Alias for query. |
||
122 | * |
||
123 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
124 | * |
||
125 | * @see query() |
||
126 | * |
||
127 | * @param array $query Fields and conditions to query by. |
||
128 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
129 | * MongoDate. |
||
130 | * |
||
131 | * @return self|$this |
||
132 | */ |
||
133 | public function where(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
143 | |||
144 | /** |
||
145 | * Sorts the results by given fields. |
||
146 | * |
||
147 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
148 | * |
||
149 | * @param array $fields An array of fields by which to sort. Each element in the array has as |
||
150 | * key the field name, and as value either 1 for ascending sort, or -1 for |
||
151 | * descending sort. |
||
152 | * |
||
153 | * @return self|$this |
||
154 | */ |
||
155 | public function sortBy(array $fields): DocumentSelector |
||
161 | |||
162 | /** |
||
163 | * Alias for sortBy. |
||
164 | * |
||
165 | * @param string $field |
||
166 | * @param int $direction |
||
167 | * |
||
168 | * @return self|$this |
||
169 | */ |
||
170 | public function orderBy(string $field, int $direction = self::ASCENDING): DocumentSelector |
||
174 | |||
175 | /** |
||
176 | * Select one document or it's fields from collection. |
||
177 | * |
||
178 | * @param array $query Fields and conditions to query by. Query will not be added to an |
||
179 | * existed query array. |
||
180 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
181 | * MongoDate. |
||
182 | * |
||
183 | * @return CompositableInterface|null |
||
184 | */ |
||
185 | public function findOne(array $query = [], bool $normalizeDates = true) |
||
202 | |||
203 | /** |
||
204 | * Count collection. Attention, this method depends on current values set for limit and offset! |
||
205 | * |
||
206 | * @return int |
||
207 | */ |
||
208 | public function count(): int |
||
216 | |||
217 | /** |
||
218 | * Fetch all documents. |
||
219 | * |
||
220 | * @return CompositableInterface[] |
||
221 | */ |
||
222 | public function fetchAll(): array |
||
226 | |||
227 | /** |
||
228 | * Create cursor with partial selection. Attention, you can not exclude _id from result! |
||
229 | * |
||
230 | * Example: $selector->fetchFields(['name', ...])->toArray(); |
||
231 | * |
||
232 | * @param array $fields |
||
233 | * |
||
234 | * @return Cursor |
||
235 | */ |
||
236 | public function getProjection(array $fields = []): Cursor |
||
240 | |||
241 | /** |
||
242 | * Create selection and wrap it using DocumentCursor. |
||
243 | * |
||
244 | * @return DocumentCursor |
||
245 | */ |
||
246 | public function getIterator(): DocumentCursor |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function jsonSerialize() |
||
262 | |||
263 | /** |
||
264 | * @return array |
||
265 | */ |
||
266 | public function __debugInfo() |
||
277 | |||
278 | /** |
||
279 | * Destructing. |
||
280 | */ |
||
281 | public function __destruct() |
||
289 | |||
290 | /** |
||
291 | * @param array $fields Fields to be selected (keep empty to select all). |
||
292 | * |
||
293 | * @return Cursor |
||
294 | */ |
||
295 | protected function createCursor(array $fields = []) |
||
314 | |||
315 | /** |
||
316 | * Options to be send to find() method of MongoDB\Collection. Options are based on how selector |
||
317 | * was configured. |
||
318 | ** |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | protected function createOptions(): array |
||
331 | |||
332 | /** |
||
333 | * Converts DateTime objects into MongoDatetime. |
||
334 | * |
||
335 | * @param array $query |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | protected function normalizeDates(array $query): array |
||
350 | |||
351 | /** |
||
352 | * @return \Interop\Container\ContainerInterface|null |
||
353 | */ |
||
354 | protected function iocContainer() |
||
363 | } |
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.