forked from Sammaye/MongoYii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMongoCriteria.php
216 lines (191 loc) · 6.26 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
<?php
/**
* This is the extensions version of CDbCriteria.
*
* This class is by no means required however it can help in your programming.
*/
class EMongoCriteria extends CComponent {
private $_condition = array();
private $_sort = array();
private $_skip = 0;
private $_limit = 0;
/**
* Constructor.
* @param array $data criteria initial property values (indexed by property name)
*/
public function __construct($data=array()){
foreach($data as $name=>$value)
$this->$name=$value;
}
/**
* Sets the condition
* @param array $condition
*/
public function setCondition(array $condition=array()) {
$this->_condition = CMap::mergeArray($condition, $this->_condition);
return $this;
}
/**
* Gets the condition
* @return array
*/
public function getCondition() {
return $this->_condition;
}
/**
* Gets the sort
* @return array
*/
public function getSort() {
return $this->_sort;
}
/**
* Gets the skip
* @return int
*/
public function getSkip() {
return $this->_skip;
}
/**
* Gets the limit
* @return int
*/
public function getLimit() {
return $this->_limit;
}
/**
* Sets the sort
* @param array $sort
* @return EMongoCriteria
*/
public function setSort(array $sort) {
$this->_sort = CMap::mergeArray($sort, $this->_sort);
return $this;
}
/**
* Sets the skip
* @param int $skip
* @return EMongoCriteria
*/
public function setSkip($skip) {
$this->_skip = (int)$skip;
return $this;
}
/**
* Sets the limit
* @param int $limit
* @return EMongoCriteria
*/
public function setLimit($limit) {
$this->_limit = (int)$limit;
return $this;
}
/**
* Append condition to previous ones
* @param string $column
* @param mixin $value
* @param string $operator
* @return EMongoCriteria
*/
public function addCondition($column, $value, $operator = null) {
$this->_condition[$column] = $operator === null ? $value : array($operator => $value);
return $this;
}
/**
* Adds an $or condition to the criteria
* @param array $condition
*/
public function addOrCondition($condition){
$this->_condition['$or'] = $condition;
}
/**
* Base search functionality
* @param string $column
* @param [null|string] $value
* @param boolean $strong
* @return EMongoCriteria
*/
public function compare($column, $value = null, $strong = false) {
if ($value===null)
return $this;
$query = array();
if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $value, $matches)) {
$value = $matches[2];
$op = $matches[1];
if (!$strong && !preg_match('/^([0-9]|[1-9]{1}\d+)$/', $value))
$value = new MongoRegex("/$value/i");
else {
if (preg_match('/^([0-9]|[1-9]{1}\d+)$/', $value))
$value = (int) $value;
}
switch($op){
case "<>":
$query[$column] = array('$ne' => $value);
break;
case "<=":
$query[$column] = array('$lte' => $value);
break;
case ">=":
$query[$column] = array('$gte' => $value);
break;
case "<":
$query[$column] = array('$lt' => $value);
break;
case ">":
$query[$column] = array('$gt' => $value);
break;
case "=":
default:
$query[$column] = $value;
break;
}
}
if (!$query)
$query[$column] = $value;
$this->_condition = CMap::mergeArray($query, $this->_condition);
return $this;
}
/**
* Meges either an array of criteria or another criteria object with this one
* @param [array|EMongoCriteria] $criteria
* @return EMongoCriteria
*/
public function mergeWith($criteria) {
if ($criteria instanceof EMongoCriteria) {
if (isset($criteria->condition) && is_array($criteria->condition))
$this->_condition = CMap::mergeArray($this->condition, $criteria->condition);
if (isset($criteria->sort) && is_array($criteria->sort))
$this->_sort = CMap::mergeArray($this->condition, $criteria->sort);
if (isset($criteria->skip) && is_numeric($criteria->skip))
$this->_skip = $criteria->skip;
if (isset($criteria->limit) && is_numeric($criteria->limit))
$this->_limit = $criteria->limit;
return $this;
} elseif (is_array($criteria)) {
if (isset($criteria['condition']) && is_array($criteria['condition']))
$this->_condition = CMap::mergeArray($this->condition, $criteria['condition']);
if (isset($criteria['sort']) && is_array($criteria['sort']))
$this->_sort = CMap::mergeArray($this->condition, $criteria['sort']);
if (isset($criteria['skip']) && is_numeric($criteria['skip']))
$this->_skip = $criteria['skip'];
if (isset($criteria['limit']) && is_numeric($criteria['limit']))
$this->_limit = $criteria['limit'];
return $this;
}
}
/**
* @param boolean $onlyCondition indicates whether to return only condition part or criteria.
* Should be setted in "true" if criteria it is used at EMongoDocument::find() and common find methods.
* @return array native representation of the criteria
*/
public function toArray($onlyCondition = false) {
$result = array();
if ($onlyCondition === true) {
$result = $this->condition;
} else {
foreach (array('_condition', '_limit', '_skip', '_sort') as $name)
$result[substr($name, 1)] = $this->$name;
}
return $result;
}
}