|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Funivan\PhpTokenizer\Query; |
|
5
|
|
|
|
|
6
|
|
|
use Funivan\PhpTokenizer\Exception\Exception; |
|
7
|
|
|
use Funivan\PhpTokenizer\Exception\InvalidArgumentException; |
|
8
|
|
|
use Funivan\PhpTokenizer\Token; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Ivan Shcherbak <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Query implements QueryInterface { |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Array of check functions |
|
18
|
|
|
* As first argument accept token |
|
19
|
|
|
* Return boolean |
|
20
|
|
|
* |
|
21
|
|
|
* @var callable[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $checkFunctions = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Storage of type conditions |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $type = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Storage of conditions conditions |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $value = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Storage of line conditions |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $line = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Storage of index conditions |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $index = []; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return static |
|
56
|
|
|
*/ |
|
57
|
176 |
|
public static function create() { |
|
58
|
176 |
|
return new static(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param int|array $type Array<Int>|Int |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
120 |
|
public function typeIs($type) { |
|
67
|
|
|
|
|
68
|
116 |
|
$types = $this->prepareIntValues($type); |
|
69
|
|
|
|
|
70
|
|
|
$this->checkFunctions[] = function (Token $token) use ($types) { |
|
71
|
114 |
|
return in_array($token->getType(), $types, true); |
|
72
|
2 |
|
}; |
|
73
|
|
|
|
|
74
|
116 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array|int $type Array<Int>|Int |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function typeNot($type) { |
|
83
|
|
|
|
|
84
|
2 |
|
$types = $this->prepareIntValues($type); |
|
85
|
|
|
|
|
86
|
|
|
$this->checkFunctions[] = function (Token $token) use ($types) { |
|
87
|
2 |
|
return !in_array($token->getType(), $types, true); |
|
88
|
|
|
}; |
|
89
|
|
|
|
|
90
|
3 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array|string $value Array<String>|String |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
151 |
|
public function valueIs($value) { |
|
99
|
142 |
|
$values = $this->prepareValues($value); |
|
100
|
|
|
|
|
101
|
|
|
$this->checkFunctions[] = function (Token $token) use ($values) { |
|
102
|
142 |
|
return in_array($token->getValue(), $values, true); |
|
103
|
|
|
}; |
|
104
|
|
|
|
|
105
|
151 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array|string $value Array<String>|String |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
8 |
|
public function valueNot($value) { |
|
114
|
|
|
|
|
115
|
8 |
|
$values = $this->prepareValues($value); |
|
116
|
|
|
|
|
117
|
|
|
$this->checkFunctions[] = function (Token $token) use ($values) { |
|
118
|
2 |
|
return !in_array($token->getValue(), $values, true); |
|
119
|
|
|
}; |
|
120
|
|
|
|
|
121
|
3 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param array|string $regex Array<String>|String |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
53 |
|
public function valueLike($regex) { |
|
130
|
42 |
|
$regexConditions = $this->prepareValues($regex); |
|
131
|
|
|
|
|
132
|
|
|
$this->checkFunctions[] = function (Token $token) use ($regexConditions) { |
|
133
|
46 |
|
if (empty($regexConditions)) { |
|
134
|
2 |
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
40 |
|
$value = $token->getValue(); |
|
138
|
|
|
|
|
139
|
40 |
|
foreach ($regexConditions as $regex) { |
|
140
|
40 |
|
if (!preg_match($regex, $value)) { |
|
141
|
24 |
|
return false; |
|
142
|
|
|
} |
|
143
|
20 |
|
} |
|
144
|
|
|
|
|
145
|
46 |
|
return true; |
|
146
|
8 |
|
}; |
|
147
|
|
|
|
|
148
|
53 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int|int[] $index |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
9 |
|
public function indexIs($index) { |
|
157
|
|
|
|
|
158
|
6 |
|
$indexNumbers = $this->prepareIntValues($index); |
|
159
|
|
|
|
|
160
|
|
|
$this->checkFunctions[] = function (Token $token) use ($indexNumbers) { |
|
161
|
6 |
|
return in_array($token->getIndex(), $indexNumbers, true); |
|
162
|
|
|
}; |
|
163
|
|
|
|
|
164
|
9 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param int|int[] $index |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
3 |
|
public function indexNot($index) { |
|
173
|
2 |
|
$indexNumbers = $this->prepareIntValues($index); |
|
174
|
|
|
|
|
175
|
|
|
$this->checkFunctions[] = function (Token $token) use ($indexNumbers) { |
|
176
|
2 |
|
return !in_array($token->getIndex(), $indexNumbers, true); |
|
177
|
|
|
}; |
|
178
|
|
|
|
|
179
|
3 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param int|int[] $index |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
2 |
|
public function indexGt($index) { |
|
188
|
2 |
|
$indexNumbers = $this->prepareIntValues($index); |
|
189
|
|
|
|
|
190
|
|
|
$this->checkFunctions[] = function (Token $token) use ($indexNumbers) { |
|
191
|
2 |
|
return ($token->getIndex() > max($indexNumbers)); |
|
192
|
|
|
}; |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
2 |
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param int|int[] $index |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
2 |
|
public function indexLt($index) { |
|
204
|
2 |
|
$indexNumbers = $this->prepareIntValues($index); |
|
205
|
|
|
|
|
206
|
2 |
|
$this->checkFunctions[] = function (Token $token) use ($indexNumbers) { |
|
207
|
2 |
|
return ($token->getIndex() < min($indexNumbers)); |
|
208
|
|
|
}; |
|
209
|
|
|
|
|
210
|
2 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @inheritdoc |
|
216
|
|
|
*/ |
|
217
|
201 |
|
public function isValid(\Funivan\PhpTokenizer\Token $token) { |
|
218
|
|
|
|
|
219
|
201 |
|
foreach ($this->checkFunctions as $check) { |
|
220
|
|
|
|
|
221
|
180 |
|
$result = $check($token); |
|
222
|
180 |
|
if (!is_bool($result)) { |
|
223
|
2 |
|
throw new Exception("Check function should return boolean value. Given:" . gettype($result)); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
193 |
|
if ($result === false) { |
|
227
|
189 |
|
return false; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
92 |
|
} |
|
231
|
|
|
|
|
232
|
173 |
|
return true; |
|
233
|
18 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string|int|array $value String|Int|Array<String>|Array<Int> |
|
238
|
|
|
* @return array Array<String> |
|
239
|
|
|
* @throws \Exception |
|
240
|
|
|
*/ |
|
241
|
163 |
|
protected function prepareValues($value) { |
|
242
|
|
|
|
|
243
|
163 |
|
if ($value == null) { |
|
244
|
25 |
|
return []; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
144 |
|
if (is_object($value)) { |
|
248
|
2 |
|
throw new InvalidArgumentException('Invalid conditions. Must be string or array of string'); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
142 |
|
$value = array_values((array) $value); |
|
252
|
|
|
|
|
253
|
142 |
|
foreach ($value as $k => $val) { |
|
254
|
142 |
|
if (!is_string($val) and !is_numeric($val)) { |
|
255
|
2 |
|
throw new InvalidArgumentException('Invalid conditions. Must be string'); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
148 |
|
$value[$k] = (string) $val; |
|
259
|
71 |
|
} |
|
260
|
148 |
|
return $value; |
|
261
|
1 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param array|int $value Array<Int>|Int |
|
266
|
|
|
* @return array |
|
267
|
|
|
* @throws \Exception |
|
268
|
|
|
*/ |
|
269
|
138 |
|
protected function prepareIntValues($value) { |
|
270
|
|
|
|
|
271
|
138 |
|
if ($value === null) { |
|
272
|
3 |
|
return []; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
124 |
|
if (is_object($value)) { |
|
276
|
2 |
|
throw new InvalidArgumentException('Invalid condition value. Must be int. Object given'); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
122 |
|
$value = array_values((array) $value); |
|
280
|
|
|
|
|
281
|
|
|
|
|
282
|
122 |
|
foreach ($value as $intValue) { |
|
283
|
122 |
|
if (!is_int($intValue)) { |
|
284
|
62 |
|
throw new InvalidArgumentException('Invalid conditions. Must be integer. Given:' . gettype($intValue)); |
|
285
|
|
|
} |
|
286
|
61 |
|
} |
|
287
|
|
|
|
|
288
|
129 |
|
return $value; |
|
289
|
1 |
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Under development |
|
294
|
|
|
* |
|
295
|
|
|
* @param callable $checkFunction |
|
296
|
|
|
* @return $this |
|
297
|
|
|
*/ |
|
298
|
6 |
|
public function custom(callable $checkFunction) { |
|
299
|
6 |
|
$this->checkFunctions[] = $checkFunction; |
|
300
|
6 |
|
return $this; |
|
301
|
2 |
|
} |
|
302
|
|
|
|
|
303
|
|
|
} |