/**
* Process the WHERE clause
*
* @param $arr
* @return string
* @throws GQL
*/
private function recordWhere($arr)
{
$arr_conditions = explode('AND', $arr['where']);
$str_regex = '/(?<lhs>[^\\s<>=]*)\\s*(?<comp>=|<|<=|>|>=|IN|CONTAINS|HAS ANCESTOR|HAS DESCENDANT)\\s*(?<rhs>[^\\s<>=]+)/i';
foreach ($arr_conditions as $str_condition) {
$arr_matches = [];
if (preg_match($str_regex, trim($str_condition), $arr_matches)) {
$str_comp = strtoupper($arr_matches['comp']);
if (isset($this->arr_operators[$str_comp])) {
$int_operator = $this->arr_operators[$str_comp];
} else {
throw new GQL("Unsupported operator in condition: [{$arr_matches['comp']}] [{$str_condition}]");
}
// If schema is set and its properties is not empty, then we use it to test the validity
if (isset($this->obj_schema) && !empty($this->obj_schema->getProperties())) {
// Check left hand side's type
$arr_properties = $this->obj_schema->getProperties();
if (isset($arr_properties[$arr_matches['lhs']])) {
$int_current_type = $arr_properties[$arr_matches['lhs']]['type'];
switch ($int_current_type) {
case \GDS\Schema::PROPERTY_STRING:
if ('@' !== $arr_matches['rhs'][0]) {
// Skip named parameters
if (substr($arr_matches['rhs'], 0, strlen(self::TOKEN_PREFIX)) != self::TOKEN_PREFIX) {
// If the right hand side has not been tokenized
throw new GQL("Invalid string representation in: [{$str_condition}]");
}
}
break;
// @todo Add support for other type's validity here
}
} else {
// We have a Schema, but it does not contain a definition for this property.
// So, skip validation checks (we must support onl-the-fly Schemas)
}
}
$this->arr_conditions[] = ['lhs' => $arr_matches['lhs'], 'comp' => $str_comp, 'op' => $int_operator, 'rhs' => $this->lookupToken($arr_matches['rhs'])];
} else {
throw new GQL("Failed to parse condition: [{$str_condition}]");
}
}
return '';
}