FOF30\Database\Installer::conditionMet PHP Method

conditionMet() protected method

Checks if a condition is met
protected conditionMet ( string $table, SimpleXMLElement $node ) : boolean
$table string The table we're operating on
$node SimpleXMLElement The condition definition node
return boolean
    protected function conditionMet($table, SimpleXMLElement $node)
    {
        if (empty(static::$allTables)) {
            static::$allTables = $this->db->getTableList();
        }
        // Does the table exist?
        $tableNormal = $this->db->replacePrefix($table);
        $tableExists = in_array($tableNormal, static::$allTables);
        // Initialise
        $condition = false;
        // Get the condition's attributes
        $attributes = $node->attributes();
        $type = $attributes->type ? $attributes->type : null;
        $value = $attributes->value ? (string) $attributes->value : null;
        switch ($type) {
            // Check if a table or column is missing
            case 'missing':
                $fieldName = (string) $value;
                if (empty($fieldName)) {
                    $condition = !$tableExists;
                } else {
                    try {
                        $tableColumns = $this->db->getTableColumns($tableNormal, true);
                    } catch (\Exception $e) {
                        $tableColumns = array();
                    }
                    $condition = !array_key_exists($fieldName, $tableColumns);
                }
                break;
                // Check if a column type matches the "coltype" attribute
            // Check if a column type matches the "coltype" attribute
            case 'type':
                try {
                    $tableColumns = $this->db->getTableColumns($tableNormal, true);
                } catch (\Exception $e) {
                    $tableColumns = array();
                }
                $condition = false;
                if (array_key_exists($value, $tableColumns)) {
                    $coltype = $attributes->coltype ? $attributes->coltype : null;
                    if (!empty($coltype)) {
                        $coltype = strtolower($coltype);
                        $currentType = strtolower($tableColumns[$value]->Type);
                        $condition = $coltype == $currentType;
                    }
                }
                break;
                // Check if a (named) index exists on the table. Currently only supported on MySQL.
            // Check if a (named) index exists on the table. Currently only supported on MySQL.
            case 'index':
                $indexName = (string) $value;
                $condition = true;
                if (!empty($indexName)) {
                    $indexName = str_replace('#__', $this->db->getPrefix(), $indexName);
                    $condition = $this->hasIndex($tableNormal, $indexName);
                }
                break;
                // Check if a table or column needs to be upgraded to utf8mb4
            // Check if a table or column needs to be upgraded to utf8mb4
            case 'utf8mb4upgrade':
                $condition = false;
                // Check if the driver and the database connection have UTF8MB4 support
                if (method_exists($this->db, 'hasUTF8mb4Support') && $this->db->hasUTF8mb4Support()) {
                    $fieldName = (string) $value;
                    if (empty($fieldName)) {
                        $collation = $this->getTableCollation($tableNormal);
                    } else {
                        $collation = $this->getColumnCollation($tableNormal, $fieldName);
                    }
                    $parts = explode('_', $collation, 3);
                    $encoding = empty($parts[0]) ? '' : strtolower($parts[0]);
                    $condition = $encoding != 'utf8mb4';
                }
                break;
                // Check if the result of a query matches our expectation
            // Check if the result of a query matches our expectation
            case 'equals':
                $query = (string) $node;
                $this->db->setQuery($query);
                try {
                    $result = $this->db->loadResult();
                    $condition = $result == $value;
                } catch (Exception $e) {
                    return false;
                }
                break;
                // Always returns true
            // Always returns true
            case 'true':
                return true;
                break;
            default:
                return false;
                break;
        }
        return $condition;
    }