| 
5 | 5 | namespace PhpMyAdmin\SqlParser\Components;  | 
6 | 6 | 
 
  | 
7 | 7 | use PhpMyAdmin\SqlParser\Component;  | 
8 |  | -use PhpMyAdmin\SqlParser\Parser;  | 
9 |  | -use PhpMyAdmin\SqlParser\TokensList;  | 
10 |  | -use PhpMyAdmin\SqlParser\TokenType;  | 
11 |  | - | 
12 |  | -use function implode;  | 
13 | 8 | 
 
  | 
14 | 9 | /**  | 
15 | 10 |  * `ORDER BY` keyword parser.  | 
@@ -40,92 +35,11 @@ public function __construct(Expression|null $expr = null, string $type = 'ASC')  | 
40 | 35 |         $this->type = $type;  | 
41 | 36 |     }  | 
42 | 37 | 
 
  | 
43 |  | -    /**  | 
44 |  | -     * @param Parser               $parser  the parser that serves as context  | 
45 |  | -     * @param TokensList           $list    the list of tokens that are being parsed  | 
46 |  | -     * @param array<string, mixed> $options parameters for parsing  | 
47 |  | -     *  | 
48 |  | -     * @return OrderKeyword[]  | 
49 |  | -     */  | 
50 |  | -    public static function parse(Parser $parser, TokensList $list, array $options = []): array  | 
51 |  | -    {  | 
52 |  | -        $ret = [];  | 
53 |  | - | 
54 |  | -        $expr = new static();  | 
55 |  | - | 
56 |  | -        /**  | 
57 |  | -         * The state of the parser.  | 
58 |  | -         *  | 
59 |  | -         * Below are the states of the parser.  | 
60 |  | -         *  | 
61 |  | -         *      0 --------------------[ expression ]-------------------> 1  | 
62 |  | -         *  | 
63 |  | -         *      1 ------------------------[ , ]------------------------> 0  | 
64 |  | -         *      1 -------------------[ ASC / DESC ]--------------------> 1  | 
65 |  | -         *  | 
66 |  | -         * @var int  | 
67 |  | -         */  | 
68 |  | -        $state = 0;  | 
69 |  | - | 
70 |  | -        for (; $list->idx < $list->count; ++$list->idx) {  | 
71 |  | -            /**  | 
72 |  | -             * Token parsed at this moment.  | 
73 |  | -             */  | 
74 |  | -            $token = $list->tokens[$list->idx];  | 
75 |  | - | 
76 |  | -            // End of statement.  | 
77 |  | -            if ($token->type === TokenType::Delimiter) {  | 
78 |  | -                break;  | 
79 |  | -            }  | 
80 |  | - | 
81 |  | -            // Skipping whitespaces and comments.  | 
82 |  | -            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {  | 
83 |  | -                continue;  | 
84 |  | -            }  | 
85 |  | - | 
86 |  | -            if ($state === 0) {  | 
87 |  | -                $expr->expr = Expression::parse($parser, $list);  | 
88 |  | -                $state = 1;  | 
89 |  | -            } elseif ($state === 1) {  | 
90 |  | -                if (  | 
91 |  | -                    ($token->type === TokenType::Keyword)  | 
92 |  | -                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))  | 
93 |  | -                ) {  | 
94 |  | -                    $expr->type = $token->keyword;  | 
95 |  | -                } elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {  | 
96 |  | -                    if (! empty($expr->expr)) {  | 
97 |  | -                        $ret[] = $expr;  | 
98 |  | -                    }  | 
99 |  | - | 
100 |  | -                    $expr = new static();  | 
101 |  | -                    $state = 0;  | 
102 |  | -                } else {  | 
103 |  | -                    break;  | 
104 |  | -                }  | 
105 |  | -            }  | 
106 |  | -        }  | 
107 |  | - | 
108 |  | -        // Last iteration was not processed.  | 
109 |  | -        if (! empty($expr->expr)) {  | 
110 |  | -            $ret[] = $expr;  | 
111 |  | -        }  | 
112 |  | - | 
113 |  | -        --$list->idx;  | 
114 |  | - | 
115 |  | -        return $ret;  | 
116 |  | -    }  | 
117 |  | - | 
118 | 38 |     public function build(): string  | 
119 | 39 |     {  | 
120 | 40 |         return $this->expr . ' ' . $this->type;  | 
121 | 41 |     }  | 
122 | 42 | 
 
  | 
123 |  | -    /** @param OrderKeyword[] $component the component to be built */  | 
124 |  | -    public static function buildAll(array $component): string  | 
125 |  | -    {  | 
126 |  | -        return implode(', ', $component);  | 
127 |  | -    }  | 
128 |  | - | 
129 | 43 |     public function __toString(): string  | 
130 | 44 |     {  | 
131 | 45 |         return $this->build();  | 
 | 
0 commit comments