1: <?php
2:
3: namespace Contentstack\Stack\ContentType\BaseQuery;
4:
5: require_once __DIR__ . "/../helper.php";
6:
7: 8: 9: 10:
11: abstract class BaseQuery {
12: var $subQuery;
13:
14: public function __construct($contentType = '', $parent = '') {
15: $this->contentType = $contentType;
16: $this->queryObject = $parent;
17: $this->queryObject->_query = array();
18: $this->subQuery = array();
19: }
20:
21: 22: 23: 24: 25:
26: public function toJSON() {
27: $this->json_translate = true;
28: return $this->queryObject;
29: }
30:
31: 32: 33: 34: 35: 36:
37: public function except($level = 'BASE', $field_uids = array()) {
38: $this->queryObject->_query = call_user_func('projection', 'except', $this->queryObject->_query, $level, $field_uids);
39: return $this->queryObject;
40: }
41:
42: 43: 44: 45: 46: 47:
48: public function only($level = 'BASE', $field_uids = array()) {
49: $this->queryObject->_query = call_user_func('projection', 'only', $this->queryObject->_query, $level, $field_uids);
50: return $this->queryObject;
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function includeReference($field_uids = array()) {
60: $this->queryObject->_query = call_user_func('references', 'include', $this->queryObject->_query, $field_uids);
61: return $this->queryObject;
62: }
63:
64: 65: 66: 67: 68: 69: 70:
71: public function search($search = '') {
72: $this->queryObject->_query = call_user_func('search', 'typeahead', $this->queryObject->_query, $search);
73: return $this->queryObject;
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83:
84: public function regex() {
85: $this->subQuery = call_user_func_array('regexp', array('$regex', $this->subQuery, func_get_args()));
86: return $this->queryObject;
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: public function logicalAND() {
97: $this->subQuery = call_user_func('logical', '$and', $this->subQuery, func_get_args());
98: return $this->queryObject;
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public function logicalOR() {
109: $this->subQuery = call_user_func('logical', '$or', $this->subQuery, func_get_args());
110: return $this->queryObject;
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120: public function ascending($field_uid = '') {
121: $this->queryObject->_query = call_user_func('sorting', 'asc', $this->queryObject->_query, $field_uid);
122: return $this->queryObject;
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: public function descending($field_uid = '') {
133: $this->queryObject->_query = call_user_func('sorting', 'desc', $this->queryObject->_query, $field_uid);
134: return $this->queryObject;
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function notExists($field_uid = '') {
145: $this->subQuery = call_user_func('existence', '$exists', $this->subQuery, $field_uid, false);
146: return $this->queryObject;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function exists($field_uid = '') {
157: $this->subQuery = call_user_func('existence', '$exists', $this->subQuery, $field_uid, true);
158: return $this->queryObject;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function includeSchema() {
168: $this->queryObject->_query = call_user_func('addBoolean', 'include_schema', $this->queryObject->_query);
169: return $this->queryObject;
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function includeContentType() {
179: $this->queryObject->_query = call_user_func('addBoolean', 'include_content_type', $this->queryObject->_query);
180: return $this->queryObject;
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function includeCount() {
190: $this->queryObject->_query = call_user_func('addBoolean', 'include_count', $this->queryObject->_query);
191: return $this->queryObject;
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function count() {
201: $this->operation = __FUNCTION__;
202: $this->queryObject->_query = call_user_func('addBoolean', 'count', $this->queryObject->_query);
203: return $this->queryObject;
204: }
205:
206: 207: 208: 209: 210: 211:
212: public function includeOwner() {
213: $this->queryObject->_query = call_user_func('addBoolean', 'include_owner', $this->queryObject->_query);
214: return $this->queryObject;
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: public function language($lang = '') {
225: $this->queryObject->_query = call_user_func('language', $this->queryObject->_query, 'locale', $lang);
226: return $this->queryObject;
227: }
228: 229: 230: 231: 232: 233: 234:
235: public function skip($skip = 0) {
236: $this->queryObject->_query = call_user_func('pagination', 'skip', $this->queryObject->_query, $skip);
237: return $this->queryObject;
238: }
239: 240: 241: 242: 243: 244: 245:
246: public function tags($tags = array()) {
247: $this->queryObject->_query = call_user_func('tags', 'tags', $this->queryObject->_query, $tags);
248: return $this->queryObject;
249: }
250:
251: 252: 253: 254: 255: 256: 257:
258: public function limit($limit = '') {
259: $this->queryObject->_query = call_user_func('pagination', 'limit', $this->queryObject->_query, $limit);
260: return $this->queryObject;
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270:
271: public function containedIn($field = '', $value = array()) {
272: $this->subQuery = call_user_func('contains', '$in', $this->subQuery, $field, $value);
273: return $this->queryObject;
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283:
284: public function notContainedIn($field = '', $value = array()) {
285: $this->subQuery = call_user_func('contains', '$nin', $this->subQuery, $field, $value);
286: return $this->queryObject;
287: }
288:
289: 290: 291: 292: 293: 294: 295: 296:
297: public function where($key = '', $value = '') {
298: if(!\Contentstack\Utility\isEmpty($key)) $this->subQuery[$key] = $value;
299: return $this->queryObject;
300: }
301:
302: 303: 304: 305: 306: 307: 308:
309: public function lessThan($field = '', $value = '') {
310: $this->subQuery = call_user_func('comparision', '$lt', $this->subQuery, $field, $value);
311: return $this->queryObject;
312: }
313:
314: 315: 316: 317: 318: 319: 320:
321: public function lessThanEqualTo($field = '', $value = '') {
322: $this->subQuery = call_user_func('comparision', '$lte', $this->subQuery, $field, $value);
323: return $this->queryObject;
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: public function greaterThan($field = '', $value = '') {
334: $this->subQuery = call_user_func('comparision', '$gt', $this->subQuery, $field, $value);
335: return $this->queryObject;
336: }
337:
338: 339: 340: 341: 342: 343: 344:
345: public function greaterThanEqualTo($field = '', $value = '') {
346: $this->subQuery = call_user_func('comparision', '$gte', $this->subQuery, $field, $value);
347: return $this->queryObject;
348: }
349:
350: 351: 352: 353: 354: 355: 356:
357: public function notEqualTo($field = '', $value = '') {
358: $this->subQuery = call_user_func('comparision', '$ne', $this->subQuery, $field, $value);
359: return $this->queryObject;
360: }
361:
362: 363: 364: 365: 366: 367:
368: public function query($_query = array()) {
369: if($_query && is_array($_query)) {
370: $this->subQuery = json_encode($_query);
371: return $this->queryObject;
372: }
373: throw createError("Provide valid query");
374: }
375:
376: 377: 378: 379:
380: public function getQuery() {
381: try {
382: return json_decode(json_encode($this->subQuery), true);
383: } catch(\Exception $e) {
384: echo $e->getMessage();
385: }
386: }
387: }