1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maslosoft\Mangan\Abstracts; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
7
|
|
|
use Maslosoft\Mangan\Cursor; |
8
|
|
|
use Maslosoft\Mangan\Finder; |
9
|
|
|
use Maslosoft\Mangan\Helpers\PkManager; |
10
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface; |
11
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
12
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
13
|
|
|
use Maslosoft\Mangan\Interfaces\ModelAwareInterface; |
14
|
|
|
use Maslosoft\Mangan\Interfaces\ScenariosInterface; |
15
|
|
|
use Maslosoft\Mangan\ScenarioManager; |
16
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
17
|
|
|
use MongoCursor; |
18
|
|
|
use UnexpectedValueException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AbstractFinder |
22
|
|
|
* |
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
24
|
|
|
*/ |
25
|
|
|
class AbstractFinder implements ModelAwareInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
use ModelAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Whenever to use cursors |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $useCursor = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Finds a single Document with the specified condition. |
38
|
|
|
* |
39
|
|
|
* @param array|CriteriaInterface $criteria query criteria. |
40
|
|
|
* @return AnnotatedInterface |
41
|
|
|
* @Ignored |
42
|
|
|
*/ |
43
|
52 |
|
public function find($criteria = null) |
44
|
|
|
{ |
45
|
52 |
|
if ($this->getFinderEvents()->beforeFind($this)) |
|
|
|
|
46
|
|
|
{ |
47
|
52 |
|
$criteria = $this->getScopeManager()->apply($criteria); |
|
|
|
|
48
|
52 |
|
$data = $this->getAdapter()->findOne($criteria, $criteria->getSelect()); |
|
|
|
|
49
|
52 |
|
return $this->populateRecord($data); |
50
|
|
|
} |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Finds document with the specified primary key. Primary key by default |
56
|
|
|
* is defined by `_id` field. But could be any other. For simple (one column) |
57
|
|
|
* keys use it's value. |
58
|
|
|
* |
59
|
|
|
* For composite use key-value with column names as keys |
60
|
|
|
* and values for values. |
61
|
|
|
* |
62
|
|
|
* Example for simple pk: |
63
|
|
|
* ```php |
64
|
|
|
* $pk = '51b616fcc0986e30026d0748' |
65
|
|
|
* ``` |
66
|
|
|
* |
67
|
|
|
* Composite pk: |
68
|
|
|
* ```php |
69
|
|
|
* $pk = [ |
70
|
|
|
* 'mainPk' => 1, |
71
|
|
|
* 'secondaryPk' => 2 |
72
|
|
|
* ]; |
73
|
|
|
* ``` |
74
|
|
|
* |
75
|
|
|
* @param mixed $pkValue primary key value. Use array for composite key. |
76
|
|
|
* @param array|CriteriaInterface $criteria |
77
|
|
|
* @return AnnotatedInterface|null |
78
|
|
|
* @Ignored |
79
|
|
|
*/ |
80
|
40 |
|
public function findByPk($pkValue, $criteria = null) |
81
|
|
|
{ |
82
|
40 |
|
$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria); |
|
|
|
|
83
|
40 |
|
$pkCriteria->mergeWith(PkManager::prepare($this->getModel(), $pkValue)); |
84
|
40 |
|
return $this->find($pkCriteria); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Finds document with the specified attributes. |
89
|
|
|
* Attributes should be specified as key-value pairs. |
90
|
|
|
* This allows easier syntax for simple queries. |
91
|
|
|
* |
92
|
|
|
* Example: |
93
|
|
|
* ```php |
94
|
|
|
* $attributes = [ |
95
|
|
|
* 'name' => 'John', |
96
|
|
|
* 'title' => 'dr' |
97
|
|
|
* ]; |
98
|
|
|
* ``` |
99
|
|
|
* |
100
|
|
|
* @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
101
|
|
|
* @return AnnotatedInterface|null |
102
|
|
|
*/ |
103
|
2 |
|
public function findByAttributes(array $attributes) |
104
|
|
|
{ |
105
|
2 |
|
$criteria = $this->getScopeManager()->getNewCriteria(); |
|
|
|
|
106
|
2 |
|
foreach ($attributes as $name => $value) |
107
|
|
|
{ |
108
|
2 |
|
$criteria->addCond($name, '==', $value); |
109
|
|
|
} |
110
|
2 |
|
return $this->find($criteria); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Finds all documents satisfying the specified condition. |
115
|
|
|
* See {@link find()} for detailed explanation about $condition and $params. |
116
|
|
|
* |
117
|
|
|
* @param array|CriteriaInterface $criteria query criteria. |
118
|
|
|
* @return AnnotatedInterface[]|Cursor |
119
|
|
|
*/ |
120
|
18 |
|
public function findAll($criteria = null) |
121
|
|
|
{ |
122
|
18 |
|
if ($this->getFinderEvents()->beforeFind($this)) |
|
|
|
|
123
|
|
|
{ |
124
|
18 |
|
$criteria = $this->getScopeManager()->apply($criteria); |
|
|
|
|
125
|
18 |
|
$cursor = $this->getAdapter()->findMany($criteria); |
|
|
|
|
126
|
|
|
|
127
|
18 |
|
assert(is_object($cursor), sprintf('Expected cursor to be compatible object, got %s', gettype($cursor))); |
128
|
18 |
|
assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor)))); |
129
|
|
|
|
130
|
18 |
|
if ($criteria->getSort() !== null) |
131
|
|
|
{ |
132
|
18 |
|
$cursor->sort($criteria->getSort()); |
133
|
|
|
} |
134
|
18 |
|
if ($criteria->getLimit() !== null) |
135
|
|
|
{ |
136
|
1 |
|
$cursor->limit($criteria->getLimit()); |
137
|
|
|
} |
138
|
18 |
|
if ($criteria->getOffset() !== null) |
139
|
|
|
{ |
140
|
1 |
|
$cursor->skip($criteria->getOffset()); |
|
|
|
|
141
|
|
|
} |
142
|
18 |
|
if ($criteria->getSelect()) |
143
|
|
|
{ |
144
|
1 |
|
$cursor->fields($criteria->getSelect()); |
|
|
|
|
145
|
|
|
} |
146
|
18 |
|
$this->getProfiler()->cursor($cursor); |
|
|
|
|
147
|
18 |
|
if ($this->isWithCursor()) |
148
|
|
|
{ |
149
|
1 |
|
return new Cursor($cursor, $this->getModel()); |
150
|
|
|
} |
151
|
|
|
else |
152
|
|
|
{ |
153
|
17 |
|
return $this->populateRecords($cursor); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
return []; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Finds all documents with the specified attributes. |
161
|
|
|
* |
162
|
|
|
* @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
163
|
|
|
* @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
164
|
|
|
* @since v1.0 |
165
|
|
|
*/ |
166
|
1 |
|
public function findAllByAttributes(array $attributes) |
167
|
|
|
{ |
168
|
1 |
|
$criteria = $this->getScopeManager()->getNewCriteria(); |
|
|
|
|
169
|
1 |
|
foreach ($attributes as $name => $value) |
170
|
|
|
{ |
171
|
1 |
|
$criteria->$name('==', $value); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $this->findAll($criteria); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Finds all documents with the specified primary keys. |
179
|
|
|
* In MongoDB world every document has '_id' unique field, so with this method that |
180
|
|
|
* field is in use as PK by default. |
181
|
|
|
* See {@link find()} for detailed explanation about $condition. |
182
|
|
|
* |
183
|
|
|
* @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). |
184
|
|
|
* @param array|CriteriaInterface $criteria query criteria. |
185
|
|
|
* @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
186
|
|
|
* @since v1.0 |
187
|
|
|
*/ |
188
|
7 |
|
public function findAllByPk($pkValues, $criteria = null) |
189
|
|
|
{ |
190
|
7 |
|
$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria); |
|
|
|
|
191
|
7 |
|
PkManager::prepareAll($this->getModel(), $pkValues, $pkCriteria); |
192
|
|
|
|
193
|
7 |
|
return $this->findAll($pkCriteria); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Counts all documents satisfying the specified condition. |
198
|
|
|
* See {@link find()} for detailed explanation about $condition and $params. |
199
|
|
|
* @param array|CriteriaInterface $criteria query criteria. |
200
|
|
|
* @return integer Count of all documents satisfying the specified condition. |
201
|
|
|
* @since v1.0 |
202
|
|
|
*/ |
203
|
19 |
|
public function count($criteria = null) |
204
|
|
|
{ |
205
|
19 |
|
if ($this->getFinderEvents()->beforeCount($this)) |
|
|
|
|
206
|
|
|
{ |
207
|
19 |
|
$criteria = $this->getScopeManager()->apply($criteria); |
|
|
|
|
208
|
19 |
|
$count = $this->getAdapter()->count($criteria); |
|
|
|
|
209
|
19 |
|
$this->getFinderEvents()->afterCount($this); |
|
|
|
|
210
|
19 |
|
return $count; |
211
|
|
|
} |
212
|
|
|
return 0; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Counts all documents found by attribute values. |
217
|
|
|
* |
218
|
|
|
* Example: |
219
|
|
|
* ```php |
220
|
|
|
* $attributes = [ |
221
|
|
|
* 'name' => 'John', |
222
|
|
|
* 'title' => 'dr' |
223
|
|
|
* ]; |
224
|
|
|
* ``` |
225
|
|
|
* |
226
|
|
|
* @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
227
|
|
|
* @return int |
228
|
|
|
* @since v1.2.2 |
229
|
|
|
* @Ignored |
230
|
|
|
*/ |
231
|
1 |
|
public function countByAttributes(array $attributes) |
232
|
|
|
{ |
233
|
1 |
|
if ($this->getFinderEvents()->beforeCount($this)) |
|
|
|
|
234
|
|
|
{ |
235
|
1 |
|
$criteria = $this->getScopeManager()->getNewCriteria(); |
|
|
|
|
236
|
1 |
|
foreach ($attributes as $name => $value) |
237
|
|
|
{ |
238
|
1 |
|
$criteria->$name = $value; |
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
$scopedCriteria = $this->getScopeManager()->apply($criteria); |
|
|
|
|
242
|
|
|
|
243
|
1 |
|
$count = $this->getAdapter()->count($scopedCriteria); |
|
|
|
|
244
|
1 |
|
$this->getFinderEvents()->afterCount($this); |
|
|
|
|
245
|
1 |
|
return $count; |
246
|
|
|
} |
247
|
|
|
return 0; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Checks whether there is document satisfying the specified condition. |
252
|
|
|
* |
253
|
|
|
* @param CriteriaInterface $criteria |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
9 |
|
public function exists(CriteriaInterface $criteria = null) |
257
|
|
|
{ |
258
|
9 |
|
if ($this->getFinderEvents()->beforeExists($this)) |
|
|
|
|
259
|
|
|
{ |
260
|
9 |
|
$criteria = $this->getScopeManager()->apply($criteria); |
|
|
|
|
261
|
|
|
|
262
|
|
|
//Select only Pk Fields to not fetch possibly large document |
263
|
9 |
|
$pkKeys = PkManager::getPkKeys($this->getModel()); |
264
|
9 |
|
if (is_string($pkKeys)) |
265
|
|
|
{ |
266
|
7 |
|
$pkKeys = [$pkKeys]; |
267
|
|
|
} |
268
|
9 |
|
$cursor = $this->getAdapter()->findMany($criteria, $pkKeys); |
|
|
|
|
269
|
9 |
|
$cursor->limit(1); |
270
|
|
|
|
271
|
|
|
// NOTE: Cannot use count(true) here because of hhvm mongofill compatibility, see: |
272
|
|
|
// https://github.com/mongofill/mongofill/issues/86 |
273
|
9 |
|
$exists = ($cursor->count() !== 0); |
274
|
9 |
|
$this->getFinderEvents()->afterExists($this); |
|
|
|
|
275
|
9 |
|
return $exists; |
276
|
|
|
} |
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Resets all scopes and criteria applied including default scope. |
282
|
|
|
* |
283
|
|
|
* @return Finder |
284
|
|
|
* @since v1.0 |
285
|
|
|
*/ |
286
|
|
|
public function resetScope() |
287
|
|
|
{ |
288
|
|
|
$this->getScopeManager()->reset(); |
|
|
|
|
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Whenever to use cursor |
294
|
|
|
* @param bool $useCursor |
295
|
|
|
* @return FinderInterface |
296
|
|
|
*/ |
297
|
69 |
|
public function withCursor($useCursor = true) |
298
|
|
|
{ |
299
|
69 |
|
$this->useCursor = $useCursor; |
300
|
69 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
18 |
|
public function isWithCursor() |
304
|
|
|
{ |
305
|
18 |
|
return $this->useCursor; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Creates an model with the given attributes. |
310
|
|
|
* This method is internally used by the find methods. |
311
|
|
|
* @param mixed[] $data attribute values (column name=>column value) |
312
|
|
|
* @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
313
|
|
|
* Null is returned if the input data is false. |
314
|
|
|
* @since v1.0 |
315
|
|
|
*/ |
316
|
59 |
|
protected function populateRecord($data) |
317
|
|
|
{ |
318
|
59 |
|
if ($data !== null) |
319
|
|
|
{ |
320
|
58 |
|
$model = $this->createModel($data); |
|
|
|
|
321
|
58 |
|
ScenarioManager::setScenario($model, ScenariosInterface::Update); |
322
|
58 |
|
$this->getFinderEvents()->afterFind($this, $model); |
|
|
|
|
323
|
58 |
|
return $model; |
324
|
|
|
} |
325
|
|
|
else |
326
|
|
|
{ |
327
|
3 |
|
return null; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Creates a list of documents based on the input data. |
333
|
|
|
* This method is internally used by the find methods. |
334
|
|
|
* @param Iterator|array $cursor Results found to populate active records. |
335
|
|
|
* @return AnnotatedInterface[] array list of active records. |
336
|
|
|
* @since v1.0 |
337
|
|
|
*/ |
338
|
17 |
|
private function populateRecords($cursor) |
339
|
|
|
{ |
340
|
17 |
|
$records = array(); |
341
|
17 |
|
foreach ($cursor as $data) |
342
|
|
|
{ |
343
|
17 |
|
$records[] = $this->populateRecord($data); |
344
|
|
|
} |
345
|
17 |
|
return $records; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: