0 follower

Class Yiisoft\Db\Syntax\ColumnDefinitionParser

InheritanceYiisoft\Db\Syntax\ColumnDefinitionParser

Parses column definition string. For example, string(255) or int unsigned.

Psalm Types

Name Value
ExtraInfo array{check?: string, collation?: string, comment?: string, defaultValueRaw?: string, extra?: string, notNull?: boolean, unique?: boolean, unsigned?: boolean}

Public Methods

Hide inherited methods

Method Description Defined By
parse() Parses column definition string. Yiisoft\Db\Syntax\ColumnDefinitionParser

Method Details

Hide inherited methods

enumInfo() protected method

protected array enumInfo ( string $values )
$values string

                protected function enumInfo(string $values): array
{
    preg_match_all("/'((?:''|[^'])*)'/", $values, $matches);
    $values = array_map(
        static fn(string $value): string => str_replace("''", "'", $value),
        $matches[1],
    );
    return ['values' => $values];
}

            
extraInfo() protected method

protected array extraInfo ( string $extra )
$extra string

                protected function extraInfo(string $extra): array
{
    if (empty($extra)) {
        return [];
    }
    $info = [];
    $bracketsPattern = '(\(((?>[^()]+)|(?-2))*\))';
    $defaultPattern = "/\\s*\\bDEFAULT\\s+('(?:[^']|'')*'|\"(?:[^\"]|\"\")*\"|[^(\\s]*$bracketsPattern?\\S*)/i";
    $extra = $this->parseStringValue($extra, $defaultPattern, 'defaultValueRaw', $info);
    $extra = $this->parseStringValue($extra, "/\\s*\\bCOMMENT\\s+'((?:[^']|'')*)'/i", 'comment', $info);
    $extra = $this->parseStringValue($extra, "/\\s*\\bCHECK\\s+$bracketsPattern/i", 'check', $info);
    $extra = $this->parseStringValue($extra, '/\s*\bCOLLATE\s+(\S+)/i', 'collation', $info);
    $extra = $this->parseBoolValue($extra, '/\s*\bUNSIGNED\b/i', 'unsigned', $info);
    $extra = $this->parseBoolValue($extra, '/\s*\bUNIQUE\b/i', 'unique', $info);
    $extra = $this->parseBoolValue($extra, '/\s*\bNOT\s+NULL\b/i', 'notNull', $info);
    if (empty($info['notNull'])) {
        $extra = $this->parseBoolValue($extra, '/\s*\bNULL\b/i', 'notNull', $info);
        if (!empty($info['notNull'])) {
            $info['notNull'] = false;
        }
    }
    /** @psalm-var ExtraInfo $info */
    if (!empty($info['comment'])) {
        $info['comment'] = str_replace("''", "'", $info['comment']);
    }
    if (!empty($info['check'])) {
        $info['check'] = substr($info['check'], 1, -1);
    }
    if (!empty($extra)) {
        $info['extra'] = $extra;
    }
    return $info;
}

            
parse() public method

Parses column definition string.

public array parse ( string $definition )
$definition string

The column definition string. For example, string(255) or int unsigned.

return array

The column information.

                public function parse(string $definition): array
{
    preg_match("/^(\w*)(?:\(((?:'[^']*'|[^)])+)\))?(\[[\d\[\]]*\])?\s*/", $definition, $matches);
    $type = strtolower($matches[1]);
    $info = ['type' => $type];
    if (isset($matches[2]) && $matches[2] !== '') {
        if ($type === 'enum') {
            $info += $this->enumInfo($matches[2]);
        } else {
            $info += $this->sizeInfo($matches[2]);
        }
    }
    if (isset($matches[3])) {
        /** @psalm-var positive-int */
        $info['dimension'] = substr_count($matches[3], '[');
    }
    $extra = substr($definition, strlen($matches[0]));
    return $info + $this->extraInfo($extra);
}

            
parseBoolValue() protected method

protected string parseBoolValue ( string $extra, string $pattern, string $name, array &$info )
$extra string
$pattern string
$name string
$info array

                protected function parseBoolValue(string $extra, string $pattern, string $name, array &$info): string
{
    if (empty($extra)) {
        return '';
    }
    /** @psalm-suppress PossiblyNullArgument */
    $extra = trim(preg_replace($pattern, '', $extra, 1, $count));
    if ($count > 0) {
        $info[$name] = true;
    }
    return $extra;
}

            
parseStringValue() protected method

protected string parseStringValue ( string $extra, string $pattern, string $name, array &$info )
$extra string
$pattern string
$name string
$info array

                protected function parseStringValue(string $extra, string $pattern, string $name, array &$info): string
{
    if (!empty($extra) && preg_match($pattern, $extra, $matches) === 1) {
        $info[$name] = $matches[1];
        return trim(str_replace($matches[0], '', $extra));
    }
    return $extra;
}

            
sizeInfo() protected method

protected array sizeInfo ( string $size )
$size string

                protected function sizeInfo(string $size): array
{
    $values = explode(',', $size);
    $info = [
        'size' => (int) $values[0],
    ];
    if (isset($values[1])) {
        $info['scale'] = (int) $values[1];
    }
    return $info;
}