forked from canni/YiiMongoDbSuite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EMongoCriteria.php
478 lines (437 loc) · 10.6 KB
/
EMongoCriteria.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* @author Ianaré Sévi
* @author Dariusz Górecki <[email protected]>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @license New BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
*/
/**
* EMongoCriteria
*
* This class is a helper for building MongoDB query arrays, it support three syntaxes for adding conditions:
*
* 1. 'equals' syntax:
* $criteriaObject->fieldName = $value; // this will produce fieldName == value query
* 2. fieldName call syntax
* $criteriaObject->fieldName($operator, $value); // this will produce fieldName <operator> value
* 3. addCond method
* $criteriaObject->addCond($fieldName, $operator, $vale); // this will produce fieldName <operator> value
*
* For operators list {@see EMongoCriteria::$operators}
*
* @author Dariusz Górecki <[email protected]>
* @since v1.0
*/
class EMongoCriteria extends CComponent
{
/**
* @since v1.0
* @var array $operators supported operators lists
*/
public static $operators = array(
'greater' => '$gt',
'>' => '$gt',
'greatereq' => '$gte',
'>=' => '$gte',
'less' => '$lt',
'<' => '$lt',
'lesseq' => '$lte',
'<=' => '$lte',
'noteq' => '$ne',
'!=' => '$ne',
'<>' => '$ne',
'in' => '$in',
'notin' => '$nin',
'all' => '$all',
'size' => '$size',
'type' => '$type',
'exists' => '$exists',
'notexists' => '$exists',
'elemmatch' => '$elemMatch',
'mod' => '$mod',
'%' => '$mod',
'equals' => '$$eq',
'eq' => '$$eq',
'==' => '$$eq',
'where' => '$where',
'or' => '$or'
);
const SORT_ASC = 1;
const SORT_DESC = -1;
private $_select = array();
private $_limit = null;
private $_offset = null;
private $_conditions = array();
private $_sort = array();
private $_workingFields = array();
private $_useCursor = null;
/**
* Constructor
* Example criteria:
*
* <PRE>
* 'criteria' = array(
* 'conditions'=>array(
* 'fieldName1'=>array('greater' => 0),
* 'fieldName2'=>array('>=' => 10),
* 'fieldName3'=>array('<' => 10),
* 'fieldName4'=>array('lessEq' => 10),
* 'fieldName5'=>array('notEq' => 10),
* 'fieldName6'=>array('in' => array(10, 9)),
* 'fieldName7'=>array('notIn' => array(10, 9)),
* 'fieldName8'=>array('all' => array(10, 9)),
* 'fieldName9'=>array('size' => 10),
* 'fieldName10'=>array('exists'),
* 'fieldName11'=>array('notExists'),
* 'fieldName12'=>array('mod' => array(10, 9)),
* 'fieldName13'=>array('==' => 1)
* ),
* 'select'=>array('fieldName', 'fieldName2'),
* 'limit'=>10,
* 'offset'=>20,
* 'sort'=>array('fieldName1'=>EMongoCriteria::SORT_ASC, 'fieldName2'=>EMongoCriteria::SORT_DESC),
* );
* </PRE>
* @param mixed $criteria
* @since v1.0
*/
public function __construct($criteria = null)
{
if (is_array($criteria))
{
if (isset($criteria['conditions']))
foreach ($criteria['conditions'] as $fieldName => $conditions)
{
$fieldNameArray = explode('.', $fieldName);
if (count($fieldNameArray) === 1)
$fieldName = array_shift($fieldNameArray);
else
$fieldName = array_pop($fieldNameArray);
foreach ($conditions as $operator => $value)
{
$this->setWorkingFields($fieldNameArray);
$operator = strtolower($operator);
$this->$fieldName($operator, $value);
}
}
if (isset($criteria['select']))
$this->select($criteria['select']);
if (isset($criteria['limit']))
$this->limit($criteria['limit']);
if (isset($criteria['offset']))
$this->offset($criteria['offset']);
if (isset($criteria['sort']))
$this->setSort($criteria['sort']);
if (isset($criteria['useCursor']))
$this->setUseCursor($criteria['useCursor']);
}
else if ($criteria instanceof EMongoCriteria)
$this->mergeWith($criteria);
}
/**
* Merge with other criteria
* - Field list operators will be merged
* - Limit and offet will be overriden
* - Select fields list will be merged
* - Sort fields list will be merged
* @param array|EMongoCriteria $criteria
* @since v1.0
*/
public function mergeWith($criteria)
{
if (is_array($criteria))
$criteria = new EMongoCriteria($criteria);
else if (empty($criteria))
return $this;
$opTable = array_values(self::$operators);
foreach ($criteria->_conditions as $fieldName => $conds)
{
if (
is_array($conds) &&
count(array_diff(array_keys($conds), $opTable)) == 0
)
{
if (isset($this->_conditions[$fieldName]) && is_array($this->_conditions[$fieldName]))
{
foreach ($this->_conditions[$fieldName] as $operator => $value)
if (!in_array($operator, $opTable))
unset($this->_conditions[$fieldName][$operator]);
}
else
$this->_conditions[$fieldName] = array();
foreach ($conds as $operator => $value)
$this->_conditions[$fieldName][$operator] = $value;
}
else
$this->_conditions[$fieldName] = $conds;
}
if (!empty($criteria->_limit))
$this->_limit = $criteria->_limit;
if (!empty($criteria->_offset))
$this->_offset = $criteria->_offset;
if (!empty($criteria->_sort))
$this->_sort = array_merge($this->_sort, $criteria->_sort);
if (!empty($criteria->_select))
$this->_select = array_merge($this->_select, $criteria->_select);
return $this;
}
/**
* If we have operator add it otherwise call parent implementation
* @see CComponent::__call()
* @since v1.0
*/
public function __call($fieldName, $parameters)
{
if (isset($parameters[0]))
$operatorName = strtolower($parameters[0]);
if (isset($parameters[1]))
$value = $parameters[1];
if (is_numeric($operatorName))
{
$operatorName = strtolower(trim($value));
$value = (strtolower(trim($value)) === 'exists') ? true : false;
}
if (in_array($operatorName, array_keys(self::$operators)))
{
array_push($this->_workingFields, $fieldName);
$fieldName = implode('.', $this->_workingFields);
$this->_workingFields = array();
switch ($operatorName)
{
case 'exists':
$this->addCond($fieldName, $operatorName, true);
break;
case 'notexists':
$this->addCond($fieldName, $operatorName, false);
break;
default:
$this->addCond($fieldName, $operatorName, $value);
}
return $this;
}
else
return parent::__call($fieldName, $parameters);
}
/**
* @since v1.0.2
*/
public function __get($name)
{
array_push($this->_workingFields, $name);
return $this;
}
/**
* @since v1.0.2
*/
public function __set($name, $value)
{
array_push($this->_workingFields, $name);
$fieldList = implode('.', $this->_workingFields);
$this->_workingFields = array();
$this->addCond($fieldList, '==', $value);
}
/**
* Return query array
* @return array query array
* @since v1.0
*/
public function getConditions()
{
return $this->_conditions;
}
/**
* @since v1.0
*/
public function setConditions(array $conditions)
{
$this->_conditions = $conditions;
}
/**
* @since v1.0
*/
public function getLimit()
{
return $this->_limit;
}
/**
* @since v1.0
*/
public function setLimit($limit)
{
$this->limit($limit);
}
/**
* @since v1.0
*/
public function getOffset()
{
return $this->_offset;
}
/**
* @since v1.0
*/
public function setOffset($offset)
{
$this->offset($offset);
}
/**
* @since v1.0
*/
public function getSort()
{
return $this->_sort;
}
/**
* @since v1.0
*/
public function setSort(array $sort)
{
$this->_sort = $sort;
}
/**
* @since v1.3.7
*/
public function getUseCursor()
{
return $this->_useCursor;
}
/**
* @since v1.3.7
*/
public function setUseCursor($useCursor)
{
$this->_useCursor = $useCursor;
}
/**
* Return selected fields
*
* @param boolean $forCursor MongoCursor::fields() method requires
* the fields to be specified as a hashmap. When this parameter is set
* to true, then we'll return the fields in this format.
* @since v1.3.1
*/
public function getSelect()
{
return $this->_select;
}
/**
* @since v1.3.1
*/
public function setSelect(array $select)
{
$this->_select = array();
// Convert the select array to field=>true/false format
foreach ($select as $key => $value)
{
if (is_int($key))
$this->_select[$value] = true;
else
$this->_select[$key] = $value;
}
}
/**
* @since v1.3.1
*/
public function getWorkingFields()
{
return $this->_workingFields;
}
/**
* @since v1.3.1
*/
public function setWorkingFields(array $select)
{
$this->_workingFields = $select;
}
/**
* List of fields to get from DB
* Multiple calls to this method will merge all given fields
*
* @param array $fieldList list of fields to select
* @since v1.0
*/
public function select(array $fieldList = null)
{
if ($fieldList !== null)
$this->setSelect(array_merge($this->_select, $fieldList));
return $this;
}
/**
* Set linit
* Multiple calls will overrride previous value of limit
*
* @param integer $limit limit
* @since v1.0
*/
public function limit($limit)
{
$this->_limit = intval($limit);
return $this;
}
/**
* Set offset
* Multiple calls will override previous value
*
* @param integer $offset offset
* @since v1.0
*/
public function offset($offset)
{
$this->_offset = intval($offset);
return $this;
}
/**
* Add sorting, avaliabe orders are: EMongoCriteria::SORT_ASC and EMongoCriteria::SORT_DESC
* Each call will be groupped with previous calls
* @param string $fieldName
* @param integer $order
* @since v1.0
*/
public function sort($fieldName, $order)
{
$this->_sort[$fieldName] = intval($order);
return $this;
}
/**
* Add condition
* If specified field already has a condition, values will be merged
* duplicates will be overriden by new values!
* @param string $fieldName
* @param string $op operator
* @param mixed $value
* @since v1.0
*/
public function addCond($fieldName, $op, $value)
{
$op = self::$operators[$op];
if ($op == self::$operators['or'])
{
if (!isset($this->_conditions[$op]))
$this->_conditions[$op] = array();
$this->_conditions[$op][] = array($fieldName => $value);
}
else
{
if (!isset($this->_conditions[$fieldName]) && $op != self::$operators['equals'])
$this->_conditions[$fieldName] = array();
if ($op != self::$operators['equals'])
{
if (
!is_array($this->_conditions[$fieldName])
|| count(array_diff(array_keys($this->_conditions[$fieldName]), array_values(self::$operators))) > 0
)
{
$this->_conditions[$fieldName] = array();
}
$this->_conditions[$fieldName][$op] = $value;
}
else
$this->_conditions[$fieldName] = $value;
}
return $this;
}
}