Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "key" to getCatchablePattern #22

Open
jdeniau opened this issue Oct 10, 2018 · 0 comments
Open

Add a "key" to getCatchablePattern #22

jdeniau opened this issue Oct 10, 2018 · 0 comments

Comments

@jdeniau
Copy link
Contributor

jdeniau commented Oct 10, 2018

Today, getCatchablePattern should return an array, and the matching value are passed into getType.

It could be really handy to have and indexed array and passed the matching index into the getType function like this:

In this example, I used the documented DQL lexer (not sure if the code is 100% right, but it's for the example)

    protected function getCatchablePatterns()
    {
        return [
-           '[a-z_][a-z0-9_]*\:[a-z_][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // aliased name
-           '[a-z_\\\][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // identifier or qualified name
-           '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
-           "'(?:[^']|'')*'", // quoted strings
-           '\?[0-9]*|:[a-z_][a-z0-9_]*', // parameters
+           'aliasedName' => '[a-z_][a-z0-9_]*\:[a-z_][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // aliased name
+           'idOrQualifiedName' => '[a-z_\\\][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // identifier or qualified name
+           'numbers' => '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
+           'quotedString' => "'(?:[^']|'')*'", // quoted strings
+           'parameters' => '\?[0-9]*|:[a-z_][a-z0-9_]*', // parameters
        ];
    }

    protected function getType(&$value, $patternIndex)
    {
        $type = self::T_NONE;

        switch (true) {
            // Recognize numeric values
-           case (is_numeric($value)):
+           case ('numbers' === $patternIndex):
                if (strpos($value, '.') !== false || stripos($value, 'e') !== false) {
                    return self::T_FLOAT;
                }

                return self::T_INTEGER;

            // Recognize quoted strings
-           case ($value[0] === "'"):
+           case ('quotedString' === $patternIndex):
                $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));

                return self::T_STRING;

            // Recognize identifiers, aliased or qualified names
-           case (ctype_alpha($value[0]) || $value[0] === '_' || $value[0] === '\\'):
+           case ('idOrQualifiedName' === $patternIndex || 'aliasedName' === $patternIndex):
                $name = 'Doctrine\ORM\Query\Lexer::T_' . strtoupper($value);

                if (defined($name)) {
                    $type = constant($name);

                    if ($type > 100) {
                        return $type;
                    }
                }

                if (strpos($value, ':') !== false) {
                    return self::T_ALIASED_NAME;
                }

                if (strpos($value, '\\') !== false) {
                    return self::T_FULLY_QUALIFIED_NAME;
                }

                return self::T_IDENTIFIER;

            // Recognize input parameters
-           case ($value[0] === '?' || $value[0] === ':'):
+           case ('parameters' === $patternIndex):
                return self::T_INPUT_PARAMETER;

            // Recognize symbols
            case ($value === '.'):
                return self::T_DOT;
            case ($value === ','):
                return self::T_COMMA;
            case ($value === '('):
                return self::T_OPEN_PARENTHESIS;
            case ($value === ')'):
                return self::T_CLOSE_PARENTHESIS;
            case ($value === '='):
                return self::T_EQUALS;
            case ($value === '>'):
                return self::T_GREATER_THAN;
            case ($value === '<'):
                return self::T_LOWER_THAN;
            case ($value === '+'):
                return self::T_PLUS;
            case ($value === '-'):
                return self::T_MINUS;
            case ($value === '*'):
                return self::T_MULTIPLY;
            case ($value === '/'):
                return self::T_DIVIDE;
            case ($value === '!'):
                return self::T_NEGATE;
            case ($value === '{'):
                return self::T_OPEN_CURLY_BRACE;
            case ($value === '}'):
                return self::T_CLOSE_CURLY_BRACE;

            // Default
            default:
                // Do nothing
        }

        return $type;
    }

I can of course open a PR on that if you are OK with the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant