|
| 1 | +<?php |
| 2 | +if(!function_exists('getFunctionName')) { |
| 3 | + /* |
| 4 | + * To get the Query method name |
| 5 | + * @return string|function-name |
| 6 | + * */ |
| 7 | + function getFunctionName() { |
| 8 | + $stack = debug_backtrace(); |
| 9 | + if(count($stack) > 2) { |
| 10 | + return $stack[3]['function']; |
| 11 | + } |
| 12 | + return $stack[0]['function']; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +if(!function_exists('createError')) { |
| 17 | + /* |
| 18 | + * Create exception object based on messages |
| 19 | + * @param |
| 20 | + * string|msg - Exception message to be delivered |
| 21 | + * @return Exception |
| 22 | + * */ |
| 23 | + function createError($msg = '') { |
| 24 | + if(!\Contentstack\Utility\isEmpty($msg)) return new Exception($msg); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +if (!function_exists('search')) { |
| 29 | + /* |
| 30 | + * search |
| 31 | + * search |
| 32 | + * @param |
| 33 | + * $operator - query operator |
| 34 | + * $query - Query object |
| 35 | + * $value - value to be search |
| 36 | + * @return $query |
| 37 | + * */ |
| 38 | + function search($operator = '', $query = array(), $value = '') { |
| 39 | + if(!(!\Contentstack\Utility\isEmpty($value) && is_string($value))) |
| 40 | + throw createError('Invalid input for "'.getFunctionName().'". String value expected.'); |
| 41 | + $query[$operator] = $value; |
| 42 | + return $query; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +if (!function_exists('references')) { |
| 47 | + /* |
| 48 | + * references |
| 49 | + * references |
| 50 | + * @param |
| 51 | + * $query - Query object |
| 52 | + * $values - array of fields to be included in the result set |
| 53 | + * @return $query |
| 54 | + * */ |
| 55 | + function references($operator = '', $query = array(), $value = array()) { |
| 56 | + if(!is_array($value)) |
| 57 | + throw createError('Invalid input for includeReferences. Array expected.'); |
| 58 | + $query[$operator] = $value; |
| 59 | + return $query; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +if (!function_exists('projection')) { |
| 64 | + /* |
| 65 | + * projection |
| 66 | + * projection |
| 67 | + * @param |
| 68 | + * $query - Query object |
| 69 | + * $values - array of fields to be included in the result set |
| 70 | + * @return $query |
| 71 | + * */ |
| 72 | + function projection($operator = '', $query = array(), $level = 'BASE', $value = array()) { |
| 73 | + if(is_array($level)) { |
| 74 | + $value = $level; |
| 75 | + $level = 'BASE'; |
| 76 | + } |
| 77 | + if(!(!\Contentstack\Utility\isEmpty($level) && is_string($level) && is_array($value))) throw createError('Invalid Input'); |
| 78 | + if(!\Contentstack\Utility\isKeySet($query, $operator)) $query[$operator] = array(); |
| 79 | + if(!\Contentstack\Utility\isKeySet($query[$operator], $level)) $query[$operator][$level] = array(); |
| 80 | + $query[$operator][$level] = array_merge($query[$operator][$level], $value); |
| 81 | + return $query; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +if (!function_exists('regexp')) { |
| 86 | + /* |
| 87 | + * regexp |
| 88 | + * regexp |
| 89 | + * @param |
| 90 | + * $operator - query operator |
| 91 | + * $query - Query object |
| 92 | + * $key - key of the query |
| 93 | + * $value - value to be set against key |
| 94 | + * $options - options for the regular expression |
| 95 | + * @return $query |
| 96 | + * */ |
| 97 | + function regexp($operator = '', $query = array(), $values = array()) { |
| 98 | + if(count($values) === 2 || count($values) === 3) { |
| 99 | + if(\Contentstack\Utility\isEmpty($values[0]) && \Contentstack\Utility\isEmpty($values[1]) && is_string($values[0]) && is_string($values[1])) |
| 100 | + throw createError('Invalid input for regex.Key must be string and value must be valid RegularExpression'); |
| 101 | + if(isset($values[2]) && !(is_string($values[2]) && strlen($values[2]) > 0)) { |
| 102 | + throw createError('Invalid options for regex. Please provide the valid options'); |
| 103 | + } |
| 104 | + $query[$values[0]] = array($operator => $values[1]); |
| 105 | + if(isset($values[2])) |
| 106 | + $query[$values[0]]['$options'] = $values[2]; |
| 107 | + return $query; |
| 108 | + } else { |
| 109 | + throw createError('Invalid input for regex. At least 2 or maximum 3 arguments are required.'); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +if (!function_exists('tags')) { |
| 115 | + /* |
| 116 | + * tags |
| 117 | + * tags |
| 118 | + * @param |
| 119 | + * $operator - query operator |
| 120 | + * $query - Query object |
| 121 | + * $value - array of tags |
| 122 | + * @return $query |
| 123 | + * */ |
| 124 | + function tags($operator = '', $query = array(), $value = '') { |
| 125 | + if(!(is_array($value) && count($value) > 0)) |
| 126 | + throw createError('Invalid input for tags.Value must be valid array of tags'); |
| 127 | + $query[$operator] = $value; |
| 128 | + return $query; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +if (!function_exists('search')) { |
| 133 | + /* |
| 134 | + * search |
| 135 | + * search |
| 136 | + * @param |
| 137 | + * $operator - query operator |
| 138 | + * $query - Query object |
| 139 | + * $value - search string |
| 140 | + * @return $query |
| 141 | + * */ |
| 142 | + function search($operator = '', $query = array(), $value = '') { |
| 143 | + if(!(is_string($value) && strlen($value) > 0)) |
| 144 | + throw createError('Invalid input for search.Value must be of type string'); |
| 145 | + $query[$operator] = $value; |
| 146 | + return $query; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +if (!function_exists('comparision')) { |
| 151 | + /* |
| 152 | + * comparision |
| 153 | + * comparision |
| 154 | + * @param |
| 155 | + * $operator - query operator |
| 156 | + * $query - Query object |
| 157 | + * $key - key of the query |
| 158 | + * $value - value to be set against key |
| 159 | + * @return $query |
| 160 | + * */ |
| 161 | + function comparision($operator = '', $query = array(), $key = '', $value = '') { |
| 162 | + if(!(!\Contentstack\Utility\isEmpty($key) && is_string($key) && !\Contentstack\Utility\isEmpty($value))) |
| 163 | + throw createError('Invalid input for "'.getFunctionName().'". Key must be string and value should be valid not empty.'); |
| 164 | + $query[$key] = array($operator => $value); |
| 165 | + return $query; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +if (!function_exists('logical')) { |
| 170 | + /* |
| 171 | + * logical |
| 172 | + * logical operations |
| 173 | + * @param |
| 174 | + * $operator - query operator |
| 175 | + * $query - Query object |
| 176 | + * $value - array of Query object or json query |
| 177 | + * @return $query |
| 178 | + * @ignore |
| 179 | + * */ |
| 180 | + function logical($operator = '', $query = array(), $value = array()) { |
| 181 | + if(!(is_array($value) && count($value) > 0)) |
| 182 | + throw createError('Invalid input for "'.getFunctionName().'". At least one Query or array object is expected'); |
| 183 | + foreach($value as $key => $_qry) { |
| 184 | + if(!\Contentstack\Utility\isKeySet($query, $operator)) $query[$operator] = array(); |
| 185 | + if($_qry instanceof \Contentstack\Stack\ContentType\BaseQuery\BaseQuery) |
| 186 | + array_push($query[$operator], $_qry->subQuery); |
| 187 | + else if(is_array($_qry)) |
| 188 | + array_push($query[$operator], $_qry); |
| 189 | + else { |
| 190 | + unset($query[$operator]); |
| 191 | + throw createError('Query objects are expected as arguments'); |
| 192 | + } |
| 193 | + } |
| 194 | + return $query; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +if (!function_exists('contains')) { |
| 199 | + /* |
| 200 | + * contains |
| 201 | + * contains |
| 202 | + * @param |
| 203 | + * $operator - query operator |
| 204 | + * $query - Query object |
| 205 | + * $key - key of the query |
| 206 | + * $value - array of value to be set against key |
| 207 | + * @return $query |
| 208 | + * */ |
| 209 | + function contains($operator = '', $query = array(), $key = '', $value = array()) { |
| 210 | + if (!(!\Contentstack\Utility\isEmpty($key) && is_string($key) && is_array($value))) |
| 211 | + throw createError('Invalid input for "'.getFunctionName().'". Key should be string and value must be array.'); |
| 212 | + $query[$key] = array($operator => $value); |
| 213 | + return $query; |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +if (!function_exists('pagination')) { |
| 218 | + /* |
| 219 | + * pagination |
| 220 | + * Creates the skip and limit parameters |
| 221 | + * @param |
| 222 | + * $operator - key of the query |
| 223 | + * $query - Query object |
| 224 | + * $value - value to be set against key |
| 225 | + * @return $query |
| 226 | + * */ |
| 227 | + function pagination($operator = '', $query = array(), $value = '') { |
| 228 | + if (!(!\Contentstack\Utility\isEmpty($value) && is_numeric($value))) |
| 229 | + throw createError('Invalid input for "'.getFunctionName().'", it should be Numeric.'); |
| 230 | + $query[$operator] = $value; |
| 231 | + return $query; |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +if (!function_exists('language')) { |
| 236 | + /* |
| 237 | + * language |
| 238 | + * Set the locale on the Query |
| 239 | + * @param |
| 240 | + * $operator - key of the query |
| 241 | + * $query - Query object |
| 242 | + * $value - value to be set against key |
| 243 | + * @return $query |
| 244 | + * */ |
| 245 | + function language($operator = '', $query = array(), $value = '') { |
| 246 | + if (!(!\Contentstack\Utility\isEmpty($value) && is_string($value))) |
| 247 | + throw createError('Invalid input for "'.getFunctionName().'", it should be String.'); |
| 248 | + $query[$operator] = $value; |
| 249 | + return $query; |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +if (!function_exists('sorting')) { |
| 254 | + /* |
| 255 | + * sort |
| 256 | + * sort the field based on the query |
| 257 | + * @param |
| 258 | + * $operator - key of the query |
| 259 | + * $query - Query object |
| 260 | + * $field_uid - field_uid which is to be use for sorting |
| 261 | + * @return $query |
| 262 | + * */ |
| 263 | + function sorting($operator = '', $query = array(), $key = '') { |
| 264 | + if (!(!\Contentstack\Utility\isEmpty($key) && is_string($key))) |
| 265 | + throw createError('Invalid input for "'.getFunctionName().'". Value should be valid field in entry'); |
| 266 | + $query[$operator] = $key; |
| 267 | + return $query; |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +if (!function_exists('addBoolean')) { |
| 272 | + /* |
| 273 | + * addBoolean |
| 274 | + * Set the boolean parameter on the Query |
| 275 | + * @param |
| 276 | + * $operator - key of the query |
| 277 | + * $query - Query object |
| 278 | + * $value - value to be set against key |
| 279 | + * @return $query |
| 280 | + * */ |
| 281 | + function addBoolean($operator = '', $query = array()) { |
| 282 | + $query[$operator] = 'true'; |
| 283 | + return $query; |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +if (!function_exists('existence')) { |
| 288 | + /* |
| 289 | + * existence |
| 290 | + * Set the boolean parameter on the Query |
| 291 | + * @param |
| 292 | + * $operator - $operator of the query |
| 293 | + * $query - Query object |
| 294 | + * $key - field_uid against which query to be checked |
| 295 | + * $value - value to be set against key |
| 296 | + * @return $query |
| 297 | + * */ |
| 298 | + function existence($operator = '', $query = array(), $key = '', $value = false) { |
| 299 | + if (!(!\Contentstack\Utility\isEmpty($key) && is_string($key))) |
| 300 | + throw createError('Invalid input for "'.getFunctionName().'". Key should be valid String field uid'); |
| 301 | + $query[$key] = array($operator => $value); |
| 302 | + return $query; |
| 303 | + } |
| 304 | +} |
| 305 | + |
0 commit comments