1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\ClickHouseUnavailableException; |
6
|
|
|
use ClickHouseDB\Exception\DatabaseException; |
7
|
|
|
use ClickHouseDB\Exception\QueryException; |
8
|
|
|
use ClickHouseDB\Query\Query; |
9
|
|
|
use ClickHouseDB\Transport\CurlerRequest; |
10
|
|
|
use ClickHouseDB\Transport\CurlerResponse; |
11
|
|
|
|
12
|
|
|
class Statement implements \Iterator |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* @var string|mixed |
16
|
|
|
*/ |
17
|
|
|
private $_rawData; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private $_http_code = -1; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @var CurlerRequest |
26
|
|
|
*/ |
27
|
|
|
private $_request = null; |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $_init = false; |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @var Query |
36
|
|
|
*/ |
37
|
|
|
private $query; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @var mixed |
41
|
|
|
*/ |
42
|
|
|
private $format; |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $sql = ''; |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @var array |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
private $meta; |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @var array |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
private $totals; |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @var array |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
private $extremes; |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @var int |
66
|
|
|
*/ |
67
|
|
|
private $rows; |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var bool|integer |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
private $rows_before_limit_at_least = false; |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @var array |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
private $array_data = []; |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var array|null |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
private $statistics = null; |
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
public $iterator = 0; |
88
|
|
|
|
89
|
|
|
|
90
|
45 |
|
public function __construct(CurlerRequest $request) |
|
|
|
|
91
|
|
|
{ |
92
|
45 |
|
$this->_request = $request; |
93
|
45 |
|
$this->format = $this->_request->getRequestExtendedInfo('format'); |
|
|
|
|
94
|
45 |
|
$this->query = $this->_request->getRequestExtendedInfo('query'); |
|
|
|
|
95
|
45 |
|
$this->sql = $this->_request->getRequestExtendedInfo('sql'); |
|
|
|
|
96
|
45 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return CurlerRequest |
100
|
|
|
*/ |
101
|
|
|
public function getRequest() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
return $this->_request; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return CurlerResponse |
|
|
|
|
108
|
|
|
* @throws Exception\TransportException |
109
|
|
|
*/ |
110
|
39 |
|
private function response() |
|
|
|
|
111
|
|
|
{ |
112
|
39 |
|
return $this->_request->response(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
|
|
|
|
117
|
|
|
* @throws Exception\TransportException |
118
|
|
|
*/ |
119
|
1 |
|
public function responseInfo() |
120
|
|
|
{ |
121
|
1 |
|
return $this->response()->info(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed|string |
126
|
|
|
*/ |
127
|
10 |
|
public function sql() |
128
|
|
|
{ |
129
|
10 |
|
return $this->sql; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $body |
|
|
|
|
134
|
|
|
* @return array|bool |
|
|
|
|
135
|
|
|
*/ |
136
|
5 |
|
private function parseErrorClickHouse($body) |
|
|
|
|
137
|
|
|
{ |
138
|
5 |
|
$body = trim($body); |
|
|
|
|
139
|
5 |
|
$mathes = []; |
140
|
|
|
|
141
|
|
|
// Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception |
142
|
|
|
// Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception |
143
|
|
|
// Code: 60. DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception |
144
|
|
|
// Code: 516. DB::Exception: test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED) (version 22.8.3.13 (official build)) |
145
|
5 |
|
|
146
|
4 |
|
if (preg_match("%Code:\s(\d+).\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%ius", $body, $mathes)) { |
|
|
|
|
147
|
|
|
return ['code' => $mathes[1], 'message' => $mathes[2]]; |
148
|
|
|
} |
149
|
1 |
|
|
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
|
|
|
|
155
|
|
|
* @throws Exception\TransportException |
156
|
8 |
|
*/ |
157
|
|
|
public function error() |
|
|
|
|
158
|
8 |
|
{ |
159
|
2 |
|
if (!$this->isError()) { |
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} |
162
|
6 |
|
|
163
|
6 |
|
$body = $this->response()->body(); |
|
|
|
|
164
|
6 |
|
$error_no = $this->response()->error_no(); |
|
|
|
|
165
|
|
|
$error = $this->response()->error(); |
|
|
|
|
166
|
6 |
|
|
167
|
6 |
|
$dumpStatement = false; |
168
|
5 |
|
if (!$error_no && !$error) { |
|
|
|
|
169
|
|
|
$parse = $this->parseErrorClickHouse($body); |
170
|
5 |
|
|
171
|
4 |
|
if ($parse) { |
|
|
|
|
172
|
|
|
throw new DatabaseException($parse['message'] . "\nIN:" . $this->sql(), $parse['code']); |
173
|
1 |
|
} else { |
|
|
|
|
174
|
1 |
|
$code = $this->response()->http_code(); |
|
|
|
|
175
|
1 |
|
$message = "HttpCode:" . $this->response()->http_code() . " ; " . $this->response()->error() . " ;" . $body; |
|
|
|
|
176
|
|
|
$dumpStatement = true; |
177
|
|
|
} |
178
|
1 |
|
} else { |
179
|
1 |
|
$code = $error_no; |
|
|
|
|
180
|
|
|
$message = $this->response()->error(); |
181
|
|
|
} |
182
|
2 |
|
|
183
|
2 |
|
$exception = new QueryException($message, $code); |
184
|
|
|
if ($code === CURLE_COULDNT_CONNECT) { |
|
|
|
|
185
|
|
|
$exception = new ClickHouseUnavailableException($message, $code); |
186
|
|
|
} |
187
|
2 |
|
|
188
|
1 |
|
if ($dumpStatement) { |
|
|
|
|
189
|
1 |
|
$exception->setRequestDetails($this->_request->getDetails()); |
190
|
|
|
$exception->setResponseDetails($this->response()->getDetails()); |
191
|
|
|
} |
192
|
2 |
|
|
193
|
|
|
throw $exception; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return bool |
|
|
|
|
198
|
|
|
* @throws Exception\TransportException |
199
|
39 |
|
*/ |
200
|
|
|
public function isError() |
|
|
|
|
201
|
39 |
|
{ |
202
|
|
|
return ($this->response()->http_code() !== 200 || $this->response()->error_no()); |
|
|
|
|
203
|
|
|
} |
204
|
32 |
|
|
205
|
|
|
private function check() : bool |
|
|
|
|
206
|
32 |
|
{ |
207
|
|
|
if (!$this->_request->isResponseExists()) { |
|
|
|
|
208
|
|
|
throw QueryException::noResponse(); |
209
|
|
|
} |
210
|
32 |
|
|
211
|
1 |
|
if ($this->isError()) { |
212
|
|
|
$this->error(); |
213
|
|
|
} |
214
|
31 |
|
|
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
30 |
|
public function isInited() |
|
|
|
|
222
|
|
|
{ |
223
|
30 |
|
return $this->_init; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return bool |
|
|
|
|
228
|
30 |
|
* @throws Exception\TransportException |
229
|
|
|
*/ |
230
|
|
|
private function init() |
|
|
|
|
231
|
29 |
|
{ |
232
|
|
|
if ($this->_init) { |
233
|
29 |
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$this->check(); |
237
|
29 |
|
|
238
|
29 |
|
$this->_rawData = $this->response()->rawDataOrJson($this->format); |
239
|
|
|
|
240
|
29 |
|
if (!$this->_rawData) { |
|
|
|
|
241
|
29 |
|
$this->_init = true; |
242
|
|
|
return false; |
|
|
|
|
243
|
29 |
|
} |
244
|
|
|
|
245
|
|
|
$data = []; |
246
|
29 |
|
foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) { |
|
|
|
|
247
|
|
|
|
248
|
|
|
if (isset($this->_rawData[$key])) { |
|
|
|
|
249
|
|
|
if ($key=='data') { |
|
|
|
|
250
|
|
|
$data=$this->_rawData[$key]; |
|
|
|
|
251
|
|
|
} else { |
252
|
29 |
|
$this->{$key} = $this->_rawData[$key]; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
29 |
|
|
257
|
29 |
|
if (empty($this->meta)) { |
258
|
29 |
|
throw new QueryException('Can`t find meta'); |
259
|
29 |
|
} |
260
|
|
|
|
261
|
|
|
$isJSONCompact=(stripos($this->format,'JSONCompact')!==false?true:false); |
|
|
|
|
262
|
29 |
|
$this->array_data = []; |
|
|
|
|
263
|
|
|
foreach ($data as $rows) { |
264
|
1 |
|
$r = []; |
265
|
|
|
|
266
|
|
|
if ($isJSONCompact) { |
267
|
28 |
|
$r[] = $rows; |
268
|
28 |
|
} else { |
269
|
|
|
foreach ($this->meta as $meta) { |
270
|
|
|
$r[$meta['name']] = $rows[$meta['name']]; |
271
|
|
|
} |
272
|
29 |
|
} |
273
|
|
|
|
274
|
|
|
$this->array_data[] = $r; |
|
|
|
|
275
|
|
|
} |
276
|
29 |
|
|
277
|
|
|
$this->_init = true; |
278
|
|
|
|
279
|
|
|
return true; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return array |
|
|
|
|
284
|
|
|
* @throws \Exception |
|
|
|
|
285
|
|
|
*/ |
286
|
|
|
public function extremes() |
|
|
|
|
287
|
|
|
{ |
288
|
|
|
$this->init(); |
289
|
|
|
return $this->extremes; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
1 |
|
* @return mixed |
|
|
|
|
294
|
|
|
* @throws Exception\TransportException |
295
|
1 |
|
*/ |
296
|
1 |
|
public function totalTimeRequest() |
297
|
|
|
{ |
298
|
|
|
$this->check(); |
299
|
|
|
return $this->response()->total_time(); |
|
|
|
|
300
|
|
|
|
301
|
|
|
} |
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
1 |
|
* @return array |
|
|
|
|
305
|
|
|
* @throws \Exception |
|
|
|
|
306
|
1 |
|
*/ |
307
|
|
|
public function extremesMin() |
|
|
|
|
308
|
1 |
|
{ |
309
|
|
|
$this->init(); |
310
|
|
|
|
311
|
|
|
if (empty($this->extremes['min'])) { |
312
|
1 |
|
return []; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $this->extremes['min']; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return array |
|
|
|
|
320
|
|
|
* @throws \Exception |
|
|
|
|
321
|
|
|
*/ |
322
|
|
|
public function extremesMax() |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
$this->init(); |
325
|
|
|
|
326
|
|
|
if (empty($this->extremes['max'])) { |
327
|
|
|
return []; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $this->extremes['max']; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
1 |
|
* @return array |
|
|
|
|
335
|
|
|
* @throws Exception\TransportException |
336
|
1 |
|
*/ |
337
|
1 |
|
public function totals() |
|
|
|
|
338
|
|
|
{ |
339
|
|
|
$this->init(); |
340
|
|
|
return $this->totals; |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
|
|
|
|
344
|
|
|
* |
345
|
|
|
*/ |
346
|
|
|
public function dump() |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
$this->_request->dump(); |
349
|
|
|
$this->response()->dump(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @return bool|int |
|
|
|
|
354
|
|
|
* @throws Exception\TransportException |
355
|
|
|
*/ |
356
|
|
|
public function countAll() |
357
|
|
|
{ |
358
|
|
|
$this->init(); |
359
|
|
|
return $this->rows_before_limit_at_least; |
|
|
|
|
360
|
|
|
} |
361
|
2 |
|
|
362
|
|
|
/** |
363
|
2 |
|
* @param bool $key |
|
|
|
|
364
|
2 |
|
* @return array|mixed|null |
|
|
|
|
365
|
|
|
* @throws Exception\TransportException |
366
|
|
|
*/ |
367
|
|
|
public function statistics($key = false) |
|
|
|
|
368
|
|
|
{ |
369
|
|
|
$this->init(); |
370
|
|
|
|
371
|
|
|
if (!is_array($this->statistics)) { |
|
|
|
|
372
|
|
|
return null; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
if (!$key) return $this->statistics; |
|
|
|
|
376
|
|
|
|
377
|
|
|
if (!isset($this->statistics[$key])) { |
|
|
|
|
378
|
|
|
return null; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this->statistics[$key]; |
|
|
|
|
382
|
|
|
|
383
|
|
|
} |
|
|
|
|
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return int |
|
|
|
|
387
|
|
|
* @throws Exception\TransportException |
388
|
|
|
*/ |
389
|
|
|
public function count() |
|
|
|
|
390
|
|
|
{ |
391
|
|
|
$this->init(); |
392
|
|
|
return $this->rows; |
|
|
|
|
393
|
|
|
} |
394
|
8 |
|
|
395
|
|
|
/** |
396
|
8 |
|
* @return mixed|string |
|
|
|
|
397
|
8 |
|
* @throws Exception\TransportException |
398
|
|
|
*/ |
399
|
|
|
public function rawData() |
400
|
|
|
{ |
401
|
|
|
if ($this->_init) { |
402
|
|
|
return $this->_rawData; |
403
|
|
|
} |
404
|
3 |
|
|
405
|
|
|
$this->check(); |
406
|
3 |
|
|
407
|
|
|
return $this->response()->rawDataOrJson($this->format); |
408
|
|
|
} |
409
|
|
|
|
410
|
3 |
|
/** |
|
|
|
|
411
|
|
|
* |
412
|
3 |
|
*/ |
413
|
|
|
public function resetIterator() |
|
|
|
|
414
|
|
|
{ |
415
|
|
|
$this->iterator=0; |
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
2 |
|
public function fetchRow($key = null) |
|
|
|
|
419
|
|
|
{ |
420
|
2 |
|
$this->init(); |
421
|
2 |
|
|
422
|
|
|
$position=$this->iterator; |
|
|
|
|
423
|
2 |
|
|
424
|
|
|
if (!isset($this->array_data[$position])) { |
|
|
|
|
425
|
2 |
|
return null; |
426
|
|
|
} |
427
|
2 |
|
|
428
|
|
|
$this->iterator++; |
429
|
2 |
|
|
430
|
|
|
if (!$key) { |
|
|
|
|
431
|
|
|
return $this->array_data[$position]; |
|
|
|
|
432
|
|
|
} |
433
|
2 |
|
if (!isset($this->array_data[$position][$key])) { |
|
|
|
|
434
|
|
|
return null; |
435
|
2 |
|
} |
436
|
|
|
|
437
|
|
|
return $this->array_data[$position][$key]; |
|
|
|
|
438
|
2 |
|
|
439
|
1 |
|
} |
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @param string $key |
|
|
|
|
442
|
2 |
|
* @return mixed|null |
443
|
|
|
* @throws Exception\TransportException |
444
|
|
|
*/ |
445
|
|
|
public function fetchOne($key = null) |
|
|
|
|
446
|
|
|
{ |
447
|
|
|
$this->init(); |
448
|
|
|
if (!isset($this->array_data[0])) { |
|
|
|
|
449
|
|
|
return null; |
450
|
18 |
|
} |
451
|
|
|
|
452
|
18 |
|
if (!$key) { |
|
|
|
|
453
|
18 |
|
return $this->array_data[0]; |
|
|
|
|
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
if (!isset($this->array_data[0][$key])) { |
|
|
|
|
457
|
18 |
|
return null; |
458
|
3 |
|
} |
459
|
|
|
|
460
|
|
|
return $this->array_data[0][$key]; |
|
|
|
|
461
|
18 |
|
} |
462
|
2 |
|
|
463
|
|
|
/** |
464
|
|
|
* @param string|null $path |
|
|
|
|
465
|
17 |
|
* @return array |
|
|
|
|
466
|
|
|
* @throws Exception\TransportException |
467
|
|
|
*/ |
468
|
|
|
public function rowsAsTree($path) |
|
|
|
|
469
|
|
|
{ |
470
|
|
|
$this->init(); |
471
|
|
|
|
472
|
|
|
$out = []; |
473
|
4 |
|
foreach ($this->array_data as $row) { |
|
|
|
|
474
|
|
|
$d = $this->array_to_tree($row, $path); |
|
|
|
|
475
|
4 |
|
$out = array_replace_recursive($d, $out); |
|
|
|
|
476
|
|
|
} |
477
|
4 |
|
|
478
|
4 |
|
return $out; |
479
|
4 |
|
} |
480
|
4 |
|
|
481
|
|
|
/** |
482
|
|
|
* Return size_upload,upload_content,speed_upload,time_request |
483
|
4 |
|
* |
484
|
|
|
* @return array |
|
|
|
|
485
|
|
|
* @throws Exception\TransportException |
486
|
|
|
*/ |
487
|
|
|
public function info_upload() |
|
|
|
|
488
|
|
|
{ |
489
|
|
|
$this->check(); |
490
|
|
|
return [ |
|
|
|
|
491
|
|
|
'size_upload' => $this->response()->size_upload(), |
492
|
|
|
'upload_content' => $this->response()->upload_content_length(), |
493
|
|
|
'speed_upload' => $this->response()->speed_upload(), |
494
|
|
|
'time_request' => $this->response()->total_time() |
|
|
|
|
495
|
|
|
]; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Return size_upload,upload_content,speed_upload,time_request,starttransfer_time,size_download,speed_download |
500
|
|
|
* |
501
|
|
|
* @return array |
|
|
|
|
502
|
|
|
* @throws Exception\TransportException |
503
|
|
|
*/ |
504
|
|
|
public function info() |
|
|
|
|
505
|
|
|
{ |
506
|
|
|
$this->check(); |
507
|
|
|
return [ |
|
|
|
|
508
|
|
|
'starttransfer_time' => $this->response()->starttransfer_time(), |
509
|
1 |
|
'size_download' => $this->response()->size_download(), |
510
|
|
|
'speed_download' => $this->response()->speed_download(), |
511
|
1 |
|
'size_upload' => $this->response()->size_upload(), |
512
|
|
|
'upload_content' => $this->response()->upload_content_length(), |
513
|
1 |
|
'speed_upload' => $this->response()->speed_upload(), |
514
|
1 |
|
'time_request' => $this->response()->total_time() |
|
|
|
|
515
|
1 |
|
]; |
516
|
1 |
|
} |
517
|
1 |
|
|
518
|
1 |
|
/** |
519
|
1 |
|
* get format in sql |
520
|
|
|
* @return mixed |
|
|
|
|
521
|
|
|
*/ |
522
|
|
|
public function getFormat() |
523
|
|
|
{ |
524
|
|
|
return $this->format; |
525
|
|
|
} |
526
|
|
|
|
527
|
1 |
|
/** |
528
|
|
|
* @return array |
|
|
|
|
529
|
1 |
|
* @throws Exception\TransportException |
530
|
|
|
*/ |
531
|
|
|
public function rows() |
|
|
|
|
532
|
|
|
{ |
533
|
|
|
$this->init(); |
534
|
|
|
return $this->array_data; |
|
|
|
|
535
|
|
|
} |
536
|
9 |
|
|
537
|
|
|
/** |
538
|
9 |
|
* @return false|string |
539
|
8 |
|
*/ |
|
|
|
|
540
|
|
|
public function jsonRows() |
541
|
|
|
{ |
542
|
|
|
return json_encode($this->rows(), JSON_PRETTY_PRINT); |
|
|
|
|
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* @param array|string $arr |
|
|
|
|
547
|
4 |
|
* @param null|string|array $path |
|
|
|
|
548
|
|
|
* @return array |
|
|
|
|
549
|
4 |
|
*/ |
550
|
|
|
private function array_to_tree($arr, $path = null) |
|
|
|
|
551
|
|
|
{ |
552
|
4 |
|
if (is_array($path)) { |
|
|
|
|
553
|
4 |
|
$keys = $path; |
554
|
|
|
} else { |
555
|
4 |
|
$args = func_get_args(); |
|
|
|
|
556
|
4 |
|
array_shift($args); |
|
|
|
|
557
|
4 |
|
|
558
|
|
|
if (sizeof($args) < 2) { |
|
|
|
|
559
|
|
|
$separator = '.'; |
560
|
|
|
$keys = explode($separator, $path); |
|
|
|
|
561
|
|
|
} else { |
562
|
|
|
$keys = $args; |
563
|
|
|
} |
564
|
4 |
|
} |
565
|
4 |
|
|
566
|
4 |
|
// |
|
|
|
|
567
|
|
|
$tree = $arr; |
568
|
4 |
|
while (count($keys)) { |
|
|
|
|
569
|
4 |
|
$key = array_pop($keys); |
|
|
|
|
570
|
|
|
|
571
|
|
|
if (isset($arr[$key])) { |
572
|
|
|
$val = $arr[$key]; |
573
|
|
|
} else { |
574
|
4 |
|
$val = $key; |
575
|
|
|
} |
576
|
4 |
|
|
577
|
|
|
$tree = array($val => $tree); |
|
|
|
|
578
|
|
|
} |
579
|
4 |
|
if (!is_array($tree)) { |
|
|
|
|
580
|
|
|
return []; |
581
|
|
|
} |
582
|
|
|
return $tree; |
|
|
|
|
583
|
|
|
} |
|
|
|
|
584
|
|
|
|
585
|
|
|
|
586
|
|
|
public function rewind(): void { |
587
|
|
|
$this->iterator = 0; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* @return mixed |
592
|
|
|
*/ |
593
|
|
|
#[\ReturnTypeWillChange] |
|
|
|
|
594
|
|
|
public function current() { |
595
|
|
|
if (!isset($this->array_data[$this->iterator])) { |
|
|
|
|
596
|
|
|
return null; |
597
|
|
|
} |
598
|
|
|
return $this->array_data[$this->iterator]; |
|
|
|
|
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
public function key(): int { |
602
|
|
|
return $this->iterator; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
public function next(): void { |
606
|
|
|
++$this->iterator; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
public function valid(): bool { |
610
|
|
|
$this->init(); |
611
|
|
|
return isset($this->array_data[$this->iterator]); |
|
|
|
|
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|