Jarves\ConditionOperator::singleConditionToSql PHP Method

singleConditionToSql() public method

public singleConditionToSql ( Condition $condition, array $conditionRule, array &$params, string $objectKey, array &$usedFieldNames = null ) : string
$condition Jarves\Configuration\Condition
$conditionRule array
$params array
$objectKey string
$usedFieldNames array
return string
    public function singleConditionToSql(Condition $condition, $conditionRule, &$params, $objectKey, &$usedFieldNames = null)
    {
        if ($conditionRule[0] === null) {
            return '';
        }
        $tableName = '';
        if ($condition->isTableNameSet()) {
            //custom tableName overwrites the tableName from the object definition (for alias use cases for example)
            $tableName = $condition->getTableName();
        }
        $def = $this->objects->getDefinition($objectKey);
        if ($def && !$tableName) {
            $tableName = $def->getTable();
        }
        $columnName = $fieldName = $conditionRule[0];
        if (false !== ($pos = strpos($fieldName, '.'))) {
            $tableName = substr($fieldName, 0, $pos);
            $columnName = $fieldName = substr($fieldName, $pos + 1);
        }
        if ($def) {
            $field = $def->getField($fieldName);
            if ($field) {
                $columns = $field->getFieldType()->getColumns();
                if (!$columns) {
                    throw new \RuntimeException("Field {$fieldName} ({$field->getType()}) does not have columns");
                }
                $columnName = Tools::camelcase2Underscore($columns[0]->getName());
            }
        } else {
            $columnName = Tools::camelcase2Underscore($fieldName);
        }
        if (null !== $usedFieldNames) {
            $usedFieldNames[] = $fieldName;
        }
        if (!is_numeric($conditionRule[0])) {
            $result = ($tableName ? Tools::dbQuote($tableName) . '.' : '') . Tools::dbQuote($columnName) . ' ';
        } else {
            $result = $conditionRule[0];
        }
        if (strtolower($conditionRule[1]) == 'regexp') {
            $result .= strtolower($this->jarvesConfig->getSystemConfig()->getDatabase()->getMainConnection()->getType()) == 'mysql' ? 'REGEXP' : '~';
        } else {
            $result .= $conditionRule[1];
        }
        if (!is_numeric($conditionRule[0])) {
            if (isset($conditionRule[2]) && $conditionRule[2] !== null) {
                if ($conditionRule[2] instanceof ConditionSubSelect) {
                    $result .= ' (' . $this->subSelectConditionToSql($conditionRule[2], $params, $objectKey, $usedFieldNames) . ') ';
                } else {
                    $params[':p' . (count($params) + 1)] = $conditionRule[2];
                    $p = ':p' . count($params);
                    if (strtolower($conditionRule[1]) == 'in' || strtolower($conditionRule[1]) == 'not in') {
                        $result .= " ({$p})";
                    } else {
                        $result .= ' ' . $p;
                    }
                }
            }
        } else {
            $result .= ' ' . ($conditionRule[0] + 0);
        }
        return $result;
    }