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