Overview

Namespaces

  • Contentstack
    • Error
    • Result
    • Stack
      • ContentType
        • BaseQuery
        • Entry
        • Query
    • Utility
  • None

Classes

  • Contentstack\Contentstack
  • Contentstack\Result\Result
  • Contentstack\Stack\ContentType\BaseQuery\BaseQuery
  • Contentstack\Stack\ContentType\ContentType
  • Contentstack\Stack\ContentType\Entry\Entry
  • Contentstack\Stack\ContentType\Query\Query
  • Contentstack\Stack\Stack

Exceptions

  • Contentstack\Error\CSException

Functions

  • addBoolean
  • comparision
  • contains
  • Contentstack\Utility\debug
  • Contentstack\Utility\generateQuery
  • Contentstack\Utility\generateQueryParams
  • Contentstack\Utility\getDomain
  • Contentstack\Utility\getLastActivites
  • Contentstack\Utility\headers
  • Contentstack\Utility\isEmpty
  • Contentstack\Utility\isKeySet
  • Contentstack\Utility\request
  • Contentstack\Utility\URL
  • Contentstack\Utility\validateInput
  • Contentstack\Utility\wrapResult
  • createError
  • existence
  • getFunctionName
  • language
  • logical
  • pagination
  • projection
  • references
  • regexp
  • search
  • sorting
  • tags
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Contentstack\Stack\ContentType\BaseQuery;
  4: 
  5: require_once __DIR__ . "/../helper.php";
  6: 
  7: /*
  8:  * BaseQuery
  9:  * Base Class where all the Queries will be created
 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:      * toJSON
 23:      * To transform the Result object to server response content
 24:      * @return Result|array
 25:      * */
 26:     public function toJSON() {
 27:         $this->json_translate = true;
 28:         return $this->queryObject;
 29:     }
 30: 
 31:     /*
 32:      * To exclude the fields from the result set
 33:      * @param
 34:      *      $field_uids|array - field uids as array
 35:      * @return Query
 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:      * To project the fields in the result set
 44:      * @param
 45:      *      $field_uids - field uids as array
 46:      * @return Query
 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:      * To include reference(s) of other content type in entries
 55:      * @param
 56:      *      $references - array of reference field uids
 57:      * @return Query
 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:      * search
 66:      * To search the given string in the entries
 67:      * @param
 68:      *      $search - string to be search in entries
 69:      * @return Query
 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:      * regex
 78:      * To perform the regular expression test on the specified field
 79:      * @param
 80:      *      $field_uid - field on which the regular expression test is going to perform
 81:      *      $regex - Regular Expression Object
 82:      * @return 
 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:      * logicalAND
 91:      * Logical AND queries are pushed
 92:      * @param
 93:      *      $query - Query Object or plain json object
 94:      * @return Query
 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:      * or
103:      * Logical OR queries are pushed
104:      * @param
105:      *      $query - Query Object or plain json object
106:      * @return Query
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:      * ascending
115:      * To sort the entries in ascending order of the specified field
116:      * @param
117:      *      field_uid - field uid to be sorted
118:      * @return Query
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:      * descending
127:      * To sort the entries in descending order of the specified field
128:      * @param
129:      *      field_uid - field uid to be sorted
130:      * @return Query
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:      * notExists
139:      * To check field doesn't exists
140:      * @param
141:      *      field_uid - field uid against the value not existence is checked
142:      * @return Query
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:      * exists
151:      * To check field exists
152:      * @param
153:      *      field_uid - field uid against the value existence is checked
154:      * @return Query
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:      * includeSchema
163:      * To include schema along with entries
164:      * @param
165:      * @return Query
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:      * includeContentType
174:      * To include content_type along with entries
175:      * @param
176:      * @return Query
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:      * includeCount
185:      * To include the count of entries based on the results
186:      * @param
187:      * @return Query
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:      * count
196:      * To get only count result
197:      * @param
198:      * @return Query
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:      * includeOwner
208:      * To include the owner of entries based on the results
209:      * @param
210:      * @return Query
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:      * language
219:      * To set the language code in the query
220:      * @param
221:      *      langCode - Language code by default is "en-us"
222:      * @return Query
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:      * skip
230:      * Skip the specified number of entries from result set
231:      * @param
232:      *      skip - valid number
233:      * @return Query
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:      * tags
241:      * Result set entries should have tags specified
242:      * @param
243:      *      tags|array - array of tags you want to match in the entries tags
244:      * @return Query
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:      * limit
253:      * Limit the specified number of entries from result set
254:      * @param
255:      *      limit - valid number
256:      * @return Query
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:      * containedIn
265:      * Query the field value from the given set of values
266:      * @param
267:      *      $field_uid - field in the entry against which comparision needs to be done
268:      *      $value     - array value against which comparision is going to happen
269:      * @return Query
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:      * notContainedIn
278:      * Query the field value other than the given set of values
279:      * @param
280:      *      $field_uid - field in the entry against which comparision needs to be done
281:      *      $value     - array value against which comparision is going to happen
282:      * @return Query
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:      * where
291:      * Query the field which has exact value as specified
292:      * @params
293:      *      $field_uid - field in the entry against which comparision needs to be done
294:      *      $value     - value against which comparision is going to happen
295:      * @return Query
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:      * lessThan
304:      * Query the field which has less value than specified one
305:      * @params
306:      *      $field_uid - field in the entry against which comparision needs to be done
307:      *      $value     - value against which comparision is going to happen
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:      * lessThanEqualTo
316:      * Query the field which has less or equal value than specified one
317:      * @params
318:      *      $field_uid - field in the entry against which comparision needs to be done
319:      *      $value     - value against which comparision is going to happen
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:      * greaterThan
328:      * Query the field which has greater value than specified one
329:      * @params
330:      *      $field_uid - field in the entry against which comparision needs to be done
331:      *      $value     - value against which comparision is going to happen
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:      * greaterThanEqualTo
340:      * Query the field which has greater or equal value than specified one
341:      * @params
342:      *      $field_uid - field in the entry against which comparision needs to be done
343:      *      $value     - value against which comparision is going to happen
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:      * notEqualTo
352:      * Query the field which has not equal to value than specified one
353:      * @params
354:      *      $field_uid - field in the entry against which comparision needs to be done
355:      *      $value     - value against which comparision is going to happen
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:      * query is used to add the raw/array query to filter the entries
364:      * @param
365:      *      array|query - array formatted query
366:      * @return Query
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:      * Get the raw/array query from the current instance of Query/Entry
378:      * @return query
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: }
API documentation generated by ApiGen