=!]+|LIKE|NOT LIKE|IN|NOT IN|CONTAINS|NOT CONTAINS|BETWEEN|NOT BETWEEN|EXISTS)/", $crstr, $matches, PREG_OFFSET_CAPTURE); // extract operator $op = $matches[0][0]; $op_pos = $matches[0][1]; // extract operands $left = trim(substr($crstr, 0, $op_pos)); $right = trim(substr($crstr, $op_pos + strlen($op), strlen($crstr))); // automatic type conversion if (str_starts_with($right, "[") && str_ends_with($right, "]")) { // is it an array? $right = substr($right, 1, -1); // strip leading and trailing brackets $elements = explode(",", $right); // extract array elements $right = []; // re-init right value, since it's an array foreach ($elements as $element) { // insert array elements $element = trim($element); if ($element !== "") { $right[] = automatic_typecast($element); } } } else { // it must be a single value $right = automatic_typecast($right); } return [$left, $op, $right]; } // Build SleekDB query expression. Processes encapsulated expressions recursively as well. static function buildQuery(string $filter): array { // skip empty filter processing if (trim($filter) === "") { return []; } // subfilters and operations $subfilts = []; $operations = []; // buffer and scoring $k = 0; $k_prev = 0; $buffer = ""; for ($i = 0; $i < strlen($filter); $i++) { $c = $filter[$i]; // extract groups surrounded by parantheses if ($c === "(") { $k++; } elseif ($c === ")") { $k--; } // only omit parentheses at the top-level expression if (!((($c === "(") && ($k === 1)) || (($c === ")") && ($k === 0)))) { $buffer .= $c; } // if k = 0, then we found a subfilter if (($k === 0) && ($k_prev === 1)) { $subfilts[] = trim($buffer); $buffer = ""; } elseif (($k === 1) && ($k_prev === 0)) { $op = trim($buffer); if ($op !== "") { $operations[] = $op; } $buffer = ""; } // save k to be used next iteration $k_prev = $k; } // decide, whether further expansion of condition is needed $criteria = []; for ($i = 0; $i < count($subfilts); $i++) { $subfilt = $subfilts[$i]; // add subcriterion if ($subfilt[0] === "(") { $criteria[] = build_query($subfilt); } else { $criteria[] = split_criterion($subfilt); } // add operator if (($i + 1) < count($subfilts)) { $criteria[] = $operations[$i]; } } return $criteria; } // Build SleekDB ordering. static function buildOrdering(string $orderby): array { // don't process empty order instructions if ($orderby === "") { return []; } // explode string at tokens delimiting separate order criteria $ordering = []; $subcriteria = explode(";", $orderby); foreach ($subcriteria as $subcriterion) { $parts = explode(":", $subcriterion); // fetch parts $field_name = trim($parts[0], "\ \n\r\t\v\0\"'"); // strip leading and trailing quotes if exists $direction = strtolower(trim($parts[1])); // fetch ordering direction $ordering[$field_name] = $direction; // build ordering instruction } return $ordering; } }