Overview

Namespaces

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

Classes

  • Contentstack\Contentstack
  • Contentstack\Result\Result
  • Contentstack\Stack\Assets\Assets
  • 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

  • Contentstack\Utility\contentstackRequest
  • Contentstack\Utility\contentstackUrl
  • 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\validateInput
  • Contentstack\Utility\wrapResult
  • contentstackAddBoolean
  • contentstackAddParam
  • contentstackComparision
  • contentstackContains
  • contentstackCreateError
  • contentstackExistence
  • contentstackGetFunctionName
  • contentstackLanguage
  • contentstackLogical
  • contentstackPagination
  • contentstackProjection
  • contentstackReferences
  • contentstackRegexp
  • contentstackSearch
  • contentstackSorting
  • contentstackTags
  • Overview
  • Namespace
  • Class
  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: 
<?php

namespace Contentstack\Stack\ContentType\BaseQuery;


require_once __DIR__ . "/../helper.php";

/*
 * BaseQuery
 * Base Class where all the Queries will be created
 * */
abstract class BaseQuery {
    var $subQuery;

    public function __construct($data = '', $parent = '') {

        if($data->type === 'assets'){
            $this->assets = $data;
            $this->queryObject = $parent;
            $this->queryObject->_query = array();
            $this->subQuery = array();
            
        }elseif ($data->type === 'asset'){
            $this->stack = $data;
            $this->queryObject = $parent;
            $this->queryObject->_query = array();
            $this->subQuery = array();
        }else{
            $this->contentType = $data;
            $this->queryObject = $parent;
            $this->queryObject->_query = array();
            $this->subQuery = array();        
        }
    }

    /*
     * toJSON
     * To transform the Result object to server response content
     * @return Result|array
     * */
    public function toJSON() {
        $this->json_translate = true;
        return $this->queryObject;
    }

    /*
     * To exclude the fields from the result set
     * @param
     *      $field_uids|array - field uids as array
     * @return Query
     * */
    public function except($level = 'BASE', $field_uids = array()) {
        $this->queryObject->_query = call_user_func('contentstackProjection', 'except', $this->queryObject->_query, $level, $field_uids);
        return $this->queryObject;
    }

    /*
     * To project the fields in the result set
     * @param
     *      $field_uids - field uids as array
     * @return Query
     * */
    public function only($level = 'BASE', $field_uids = array()) {
    $this->queryObject->_query = call_user_func('contentstackProjection', 'only', $this->queryObject->_query, $level, $field_uids);
        return $this->queryObject;
    }

    /*
     * To include reference(s) of other content type in entries
     * @param
     *      $references - array of reference field uids
     * @return Query
     * */
    public function includeReference($field_uids = array()) {
        $this->queryObject->_query = call_user_func('contentstackReferences', 'include', $this->queryObject->_query, $field_uids);
        return $this->queryObject;
    }

    /*
     * contentstackSearch
     * To search the given string in the entries
     * @param
     *      $search - string to be search in entries
     * @return Query
     * */
    public function search($search = '') {
        $this->queryObject->_query = call_user_func('contentstackSearch', 'typeahead', $this->queryObject->_query, $search);
        return $this->queryObject;
    }

    /*
     * regex
     * To perform the regular expression test on the specified field
     * @param
     *      $field_uid - field on which the regular expression test is going to perform
     *      $regex - Regular Expression Object
     * @return 
     * */
    public function regex() {
        $this->subQuery = call_user_func_array('contentstackRegexp', array('$regex', $this->subQuery, func_get_args()));
        return $this->queryObject;
    }
    
    /*
     * logicalAND
     * Logical AND queries are pushed
     * @param
     *      $query - Query Object or plain json object
     * @return Query
     * */
    public function logicalAND() {
        $this->subQuery = call_user_func('contentstackLogical', '$and', $this->subQuery, func_get_args());
        return $this->queryObject;
    }

    /*
     * or
     * Logical OR queries are pushed
     * @param
     *      $query - Query Object or plain json object
     * @return Query
     * */
    public function logicalOR() {
        $this->subQuery = call_user_func('contentstackLogical', '$or', $this->subQuery, func_get_args());
        return $this->queryObject;
    }

    /*
     * ascending
     * To sort the entries in ascending order of the specified field
     * @param
     *      field_uid - field uid to be sorted
     * @return Query
     * */
    public function ascending($field_uid = '') {
        $this->queryObject->_query = call_user_func('contentstackSorting', 'asc', $this->queryObject->_query, $field_uid);
        return $this->queryObject;
    }

    /*
     * descending
     * To sort the entries in descending order of the specified field
     * @param
     *      field_uid - field uid to be sorted
     * @return Query
     * */
    public function descending($field_uid = '') {
        $this->queryObject->_query = call_user_func('contentstackSorting', 'desc', $this->queryObject->_query, $field_uid);
        return $this->queryObject;
    }

    /*
     * notExists
     * To check field doesn't exists
     * @param
     *      field_uid - field uid against the value not existence is checked
     * @return Query
     * */
    public function notExists($field_uid = '') {
        $this->subQuery = call_user_func('contentstackExistence', '$exists', $this->subQuery, $field_uid, false);
        return $this->queryObject;
    }

    /*
     * exists
     * To check field exists
     * @param
     *      field_uid - field uid against the value existence is checked
     * @return Query
     * */
    public function exists($field_uid = '') {
        $this->subQuery = call_user_func('contentstackExistence', '$exists', $this->subQuery, $field_uid, true);
        return $this->queryObject;
    }

    /*
     * includeSchema
     * @deprecated since verion 1.1.0
     * @Alternate includeContentType
     * To include schema along with entries
     * @param
     * @return Query
     * */
    public function includeSchema() {
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'include_schema', $this->queryObject->_query);
        return $this->queryObject;
    }

        /*
     * IncludeReferenceContentTypeUID
     * @Alternate includeContentType
     * This method includes the content type UIDs of the referenced entries returned in the response.
     * @param
     * @return Query
     * */
    public function includeReferenceContentTypeUID() {
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'include_reference_content_type_uid', $this->queryObject->_query);
        return $this->queryObject;
    }

    /*
     * includeContentType
     * To include content_type along with entries
     * @param
     * @return Query
     * */
    public function includeContentType() {
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'include_content_type', $this->queryObject->_query);
        return $this->queryObject;
    }

    /*
     * includeCount
     * To include the count of entries based on the results
     * @param
     * @return Query
     * */
    public function includeCount() {
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'include_count', $this->queryObject->_query);
        return $this->queryObject;
    }

    /*
     * count
     * To get only count result
     * @param
     * @return Query
     * */
    public function count() {
        $this->operation = __FUNCTION__;
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'count', $this->queryObject->_query);
        return $this->queryObject;
    }

    /*
     * includeOwner
     * To include the owner of entries based on the results
     * @param
     * @return Query
     * */
    public function includeOwner() {
        $this->queryObject->_query = call_user_func('contentstackAddBoolean', 'include_owner', $this->queryObject->_query);
        return $this->queryObject;
    }

    /*
     * addParam
     * To add query parameter in query
     * @param
     *      key - Name of key in string
     *      value - Value of the key in string
     * @return Query
     * */
    public function addParam($key = '', $value = '') {
       $this->queryObject->_query  = call_user_func('contentstackAddParam', $key, $this->queryObject->_query, $value);
        return $this->queryObject;
    }

    /*
     * language
     * To set the language code in the query
     * @param
     *      langCode - Language code by default is "en-us"
     * @return Query
     * */
    public function language($lang = '') {
        $this->queryObject->_query = call_user_func('contentstackLanguage', 'locale', $this->queryObject->_query, $lang);
        return $this->queryObject;
    }
    /*
     * skip
     * Skip the specified number of entries from result set
     * @param
     *      skip - valid number
     * @return Query
     * */
    public function skip($skip = 0) {
        $this->queryObject->_query = call_user_func('contentstackPagination', 'skip', $this->queryObject->_query, $skip);
        return $this->queryObject;
    }
    /*
     * tags
     * Result set entries should have tags specified
     * @param
     *      tags|array - array of tags you want to match in the entries tags
     * @return Query
     * */
    public function tags($tags = array()) {
        $this->queryObject->_query = call_user_func('contentstackTags', 'tags', $this->queryObject->_query, $tags);
        return $this->queryObject;
    }

    /*
     * limit
     * Limit the specified number of entries from result set
     * @param
     *      limit - valid number
     * @return Query
     * */
    public function limit($limit = '') {

        $this->queryObject->_query = call_user_func('contentstackPagination', 'limit', $this->queryObject->_query, $limit);
        return $this->queryObject;
    }

    /*
     * containedIn
     * Query the field value from the given set of values
     * @param
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - array value against which comparision is going to happen
     * @return Query
     * */
    public function containedIn($field = '', $value = array()) {
        $this->subQuery = call_user_func('contentstackContains', '$in', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * notContainedIn
     * Query the field value other than the given set of values
     * @param
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - array value against which comparision is going to happen
     * @return Query
     * */
    public function notContainedIn($field = '', $value = array()) {
        $this->subQuery = call_user_func('contentstackContains', '$nin', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * where
     * Query the field which has exact value as specified
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happe  n
     * @return Query
     * */
    public function where($key = '', $value = '') {
        if(!\Contentstack\Utility\isEmpty($key)) $this->subQuery[$key] = $value;
        return $this->queryObject;
    }

    /*
     * lessThan
     * Query the field which has less value than specified one
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happen
     * */
    public function lessThan($field = '', $value = '') {
        $this->subQuery = call_user_func('contentstackComparision', '$lt', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * lessThanEqualTo
     * Query the field which has less or equal value than specified one
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happen
     * */
    public function lessThanEqualTo($field = '', $value = '') {
        $this->subQuery = call_user_func('contentstackComparision', '$lte', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * greaterThan
     * Query the field which has greater value than specified one
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happen
     * */
    public function greaterThan($field = '', $value = '') {
        $this->subQuery = call_user_func('contentstackComparision', '$gt', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * greaterThanEqualTo
     * Query the field which has greater or equal value than specified one
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happen
     * */
    public function greaterThanEqualTo($field = '', $value = '') {
        $this->subQuery = call_user_func('contentstackComparision', '$gte', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * notEqualTo
     * Query the field which has not equal to value than specified one
     * @params
     *      $field_uid - field in the entry against which comparision needs to be done
     *      $value     - value against which comparision is going to happen
     * */
    public function notEqualTo($field = '', $value = '') {
        $this->subQuery = call_user_func('contentstackComparision', '$ne', $this->subQuery, $field, $value);
        return $this->queryObject;
    }

    /*
     * query is used to add the raw/array query to filter the entries
     * @param
     *      array|query - array formatted query
     * @return Query
     * */
    public function query($_query = array()) {
        if($_query && is_array($_query)) {
            $this->subQuery = json_encode($_query);
            return $this->queryObject;
        }
        throw contentstackCreateError("Provide valid query");
    }

    /*
     * Get the raw/array query from the current instance of Query/Entry
     * @return query
     * */
    public function getQuery() {
        try {
            return json_decode(json_encode($this->subQuery), true);
        } catch(\Exception $e) {
            echo $e->getMessage();
        }
    }   
}
API documentation generated by ApiGen