1 | <?php |
||
23 | class DocumentSelector extends Component implements |
||
24 | \Countable, |
||
25 | \IteratorAggregate, |
||
26 | \JsonSerializable, |
||
27 | PaginatorAwareInterface |
||
28 | { |
||
29 | use LimitsTrait, PaginatorTrait; |
||
30 | |||
31 | /** |
||
32 | * Sort orders. |
||
33 | */ |
||
34 | const ASCENDING = 1; |
||
35 | const DESCENDING = -1; |
||
36 | |||
37 | /** |
||
38 | * Default selection type map. |
||
39 | */ |
||
40 | const TYPE_MAP = [ |
||
41 | 'root' => 'array', |
||
42 | 'document' => 'array', |
||
43 | 'array' => 'array' |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @var Collection |
||
48 | */ |
||
49 | private $collection; |
||
50 | |||
51 | /** |
||
52 | * Document class being selected. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $class; |
||
57 | |||
58 | /** |
||
59 | * @var ODMInterface |
||
60 | */ |
||
61 | private $odm; |
||
62 | |||
63 | /** |
||
64 | * Fields and conditions to query by. |
||
65 | * |
||
66 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | private $query = []; |
||
71 | |||
72 | /** |
||
73 | * Fields to sort. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | private $sortBy = []; |
||
78 | |||
79 | /** |
||
80 | * @param Collection $collection |
||
81 | * @param string $class |
||
82 | * @param ODMInterface $odm |
||
83 | */ |
||
84 | public function __construct(Collection $collection, string $class, ODMInterface $odm) |
||
90 | |||
91 | /** |
||
92 | * Associated ODM instance. |
||
93 | * |
||
94 | * @return ODMInterface |
||
95 | */ |
||
96 | public function getODM(): ODMInterface |
||
100 | |||
101 | /** |
||
102 | * Associated class name. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getClass(): string |
||
110 | |||
111 | /** |
||
112 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
113 | * Alias for query. |
||
114 | * |
||
115 | * Attention, MongoDB is strictly typed! |
||
116 | * |
||
117 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
118 | * |
||
119 | * @see query() |
||
120 | * |
||
121 | * @param array $query Fields and conditions to query by. |
||
122 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
123 | * MongoDate. |
||
124 | * |
||
125 | * @return self|$this |
||
126 | */ |
||
127 | public function find(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
131 | |||
132 | /** |
||
133 | * Set additional query, fields will be merged to currently existed request using array_merge. |
||
134 | * Alias for query. |
||
135 | * |
||
136 | * Attention, MongoDB is strictly typed! |
||
137 | * |
||
138 | * @link http://docs.mongodb.org/manual/tutorial/query-documents/ |
||
139 | * |
||
140 | * @see find() |
||
141 | * |
||
142 | * @param array $query Fields and conditions to query by. |
||
143 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
144 | * MongoDate. |
||
145 | * |
||
146 | * @return self|$this |
||
147 | */ |
||
148 | public function where(array $query = [], bool $normalizeDates = true): DocumentSelector |
||
158 | |||
159 | /** |
||
160 | * Sorts the results by given fields. |
||
161 | * |
||
162 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
163 | * |
||
164 | * @param array $fields An array of fields by which to sort. Each element in the array has as |
||
165 | * key the field name, and as value either 1 for ascending sort, or -1 for |
||
166 | * descending sort. |
||
167 | * |
||
168 | * @return self|$this |
||
169 | */ |
||
170 | public function sortBy(array $fields): DocumentSelector |
||
176 | |||
177 | /** |
||
178 | * Alias for sortBy. |
||
179 | * |
||
180 | * @param string $field |
||
181 | * @param int $direction |
||
182 | * |
||
183 | * @return self|$this |
||
184 | */ |
||
185 | public function orderBy(string $field, int $direction = self::ASCENDING): DocumentSelector |
||
189 | |||
190 | /** |
||
191 | * Select one document or it's fields from collection. |
||
192 | * |
||
193 | * Attention, MongoDB is strictly typed! |
||
194 | * |
||
195 | * @param array $query Fields and conditions to query by. Query will not be added to an |
||
196 | * existed query array. |
||
197 | * @param bool $normalizeDates When true (default) all DateTime objects will be converted into |
||
198 | * MongoDate. |
||
199 | * |
||
200 | * @return CompositableInterface|null |
||
201 | */ |
||
202 | public function findOne(array $query = [], bool $normalizeDates = true) |
||
220 | |||
221 | /** |
||
222 | * Count collection. Attention, this method depends on current values set for limit and offset! |
||
223 | * |
||
224 | * Attention, MongoDB is strictly typed! |
||
225 | * |
||
226 | * @param array $query |
||
227 | * |
||
228 | * @return int |
||
229 | */ |
||
230 | public function count(array $query = []): int |
||
238 | |||
239 | /** |
||
240 | * Fetch all documents. |
||
241 | * |
||
242 | * @return CompositableInterface[] |
||
243 | */ |
||
244 | public function fetchAll(): array |
||
248 | |||
249 | /** |
||
250 | * Create cursor with partial selection. Attention, you can not exclude _id from result! |
||
251 | * |
||
252 | * Example: $selector->fetchFields(['name', ...])->toArray(); |
||
253 | * |
||
254 | * @param array $fields |
||
255 | * |
||
256 | * @return Cursor |
||
257 | */ |
||
258 | public function getProjection(array $fields = []): Cursor |
||
262 | |||
263 | /** |
||
264 | * Create selection and wrap it using DocumentCursor. |
||
265 | * |
||
266 | * @return DocumentCursor |
||
267 | */ |
||
268 | public function getIterator(): DocumentCursor |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function jsonSerialize() |
||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | public function __debugInfo() |
||
299 | |||
300 | /** |
||
301 | * Destructing. |
||
302 | */ |
||
303 | public function __destruct() |
||
311 | |||
312 | /** |
||
313 | * @param array $fields Fields to be selected (keep empty to select all). |
||
314 | * |
||
315 | * @return Cursor |
||
316 | */ |
||
317 | protected function createCursor(array $fields = []) |
||
336 | |||
337 | /** |
||
338 | * Options to be send to find() method of MongoDB\Collection. Options are based on how selector |
||
339 | * was configured. |
||
340 | ** |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | protected function createOptions(): array |
||
353 | |||
354 | /** |
||
355 | * Converts DateTime objects into MongoDatetime. |
||
356 | * |
||
357 | * @param array $query |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | protected function normalizeDates(array $query): array |
||
372 | |||
373 | /** |
||
374 | * @return \Interop\Container\ContainerInterface|null |
||
375 | */ |
||
376 | protected function iocContainer() |
||
385 | } |