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