DboSource::_parseKey PHP Method

_parseKey() protected method

Extracts a Model.field identifier and an SQL condition operator from a string, formats and inserts values, and composes them into an SQL snippet.
protected _parseKey ( string $key, mixed $value, Model $Model = null ) : string
$key string An SQL key snippet containing a field and optional SQL operator
$value mixed The value(s) to be inserted in the string
$Model Model Model object initiating the query
return string
    protected function _parseKey($key, $value, Model $Model = null)
    {
        $operatorMatch = '/^(((' . implode(')|(', $this->_sqlOps);
        $operatorMatch .= ')\\x20?)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is';
        $bound = strpos($key, '?') !== false || is_array($value) && strpos($key, ':') !== false;
        if (strpos($key, ' ') === false) {
            $operator = '=';
        } else {
            list($key, $operator) = explode(' ', trim($key), 2);
            if (!preg_match($operatorMatch, trim($operator)) && strpos($operator, ' ') !== false) {
                $key = $key . ' ' . $operator;
                $split = strrpos($key, ' ');
                $operator = substr($key, $split);
                $key = substr($key, 0, $split);
            }
        }
        $virtual = false;
        $type = null;
        if ($Model !== null) {
            if ($Model->isVirtualField($key)) {
                $key = $this->_quoteFields($Model->getVirtualField($key));
                $virtual = true;
            }
            $type = $Model->getColumnType($key);
        }
        $null = $value === null || is_array($value) && empty($value);
        if (strtolower($operator) === 'not') {
            $data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $Model);
            return $data[0];
        }
        $value = $this->value($value, $type);
        if (!$virtual && $key !== '?') {
            $isKey = strpos($key, '(') !== false || strpos($key, ')') !== false || strpos($key, '|') !== false;
            $key = $isKey ? $this->_quoteFields($key) : $this->name($key);
        }
        if ($bound) {
            return CakeText::insert($key . ' ' . trim($operator), $value);
        }
        if (!preg_match($operatorMatch, trim($operator))) {
            $operator .= is_array($value) ? ' IN' : ' =';
        }
        $operator = trim($operator);
        if (is_array($value)) {
            $value = implode(', ', $value);
            switch ($operator) {
                case '=':
                    $operator = 'IN';
                    break;
                case '!=':
                case '<>':
                    $operator = 'NOT IN';
                    break;
            }
            $value = "({$value})";
        } elseif ($null || $value === 'NULL') {
            switch ($operator) {
                case '=':
                    $operator = 'IS';
                    break;
                case '!=':
                case '<>':
                    $operator = 'IS NOT';
                    break;
            }
        }
        if ($virtual) {
            return "({$key}) {$operator} {$value}";
        }
        return "{$key} {$operator} {$value}";
    }
DboSource