forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayObj.php
More file actions
182 lines (157 loc) · 4.91 KB
/
ArrayObj.php
File metadata and controls
182 lines (157 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use function implode;
use function is_array;
use function strlen;
use function trim;
/**
* Parses an array.
*/
final class ArrayObj implements Component
{
/**
* The array that contains the unprocessed value of each token.
*
* @var string[]
*/
public $raw = [];
/**
* The array that contains the processed value of each token.
*
* @var string[]
*/
public $values = [];
/**
* @param string[] $raw the unprocessed values
* @param string[] $values the processed values
*/
public function __construct(array $raw = [], array $values = [])
{
$this->raw = $raw;
$this->values = $values;
}
/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
* @param array<string, mixed> $options parameters for parsing
*
* @return ArrayObj|Component[]
*/
public static function parse(Parser $parser, TokensList $list, array $options = [])
{
$ret = empty($options['type']) ? new static() : [];
/**
* The last raw expression.
*
* @var string
*/
$lastRaw = '';
/**
* The last value.
*
* @var string
*/
$lastValue = '';
/**
* Counts brackets.
*
* @var int
*/
$brackets = 0;
/**
* Last separator (bracket or comma).
*
* @var bool
*/
$isCommaLast = false;
for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
*/
$token = $list->tokens[$list->idx];
// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
break;
}
// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
$lastRaw .= $token->token;
$lastValue = trim($lastValue) . ' ';
continue;
}
if (($brackets === 0) && (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '('))) {
$parser->error('An opening bracket was expected.', $token);
break;
}
if ($token->type === Token::TYPE_OPERATOR) {
if ($token->value === '(') {
if (++$brackets === 1) { // 1 is the base level.
continue;
}
} elseif ($token->value === ')') {
if (--$brackets === 0) { // Array ended.
break;
}
} elseif ($token->value === ',') {
if ($brackets === 1) {
$isCommaLast = true;
if (empty($options['type'])) {
$ret->raw[] = trim($lastRaw);
$ret->values[] = trim($lastValue);
$lastRaw = $lastValue = '';
}
}
continue;
}
}
if (empty($options['type'])) {
$lastRaw .= $token->token;
$lastValue .= $token->value;
} else {
$ret[] = $options['type']::parse(
$parser,
$list,
empty($options['typeOptions']) ? [] : $options['typeOptions']
);
}
}
// Handling last element.
//
// This is treated differently to treat the following cases:
//
// => []
// [,] => ['', '']
// [] => []
// [a,] => ['a', '']
// [a] => ['a']
$lastRaw = trim($lastRaw);
if (empty($options['type']) && ((strlen($lastRaw) > 0) || ($isCommaLast))) {
$ret->raw[] = $lastRaw;
$ret->values[] = trim($lastValue);
}
return $ret;
}
/**
* @param ArrayObj|ArrayObj[] $component the component to be built
* @param array<string, mixed> $options parameters for building
*/
public static function build($component, array $options = []): string
{
if (is_array($component)) {
return implode(', ', $component);
}
if (! empty($component->raw)) {
return '(' . implode(', ', $component->raw) . ')';
}
return '(' . implode(', ', $component->values) . ')';
}
public function __toString(): string
{
return static::build($this);
}
}