-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathRecursiveFilter.php
More file actions
50 lines (41 loc) · 1.09 KB
/
RecursiveFilter.php
File metadata and controls
50 lines (41 loc) · 1.09 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
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
declare(strict_types=1);
namespace Flow\JSONPath\Filters;
use Flow\JSONPath\AccessHelper;
use Flow\JSONPath\JSONPathException;
class RecursiveFilter extends AbstractFilter
{
/**
* @inheritDoc
*
* @throws JSONPathException
*/
public function filter(array|object $collection): array
{
$result = [];
$this->recurse($result, $collection);
return $result;
}
/**
* @param array<int, array<array-key, mixed>> $result
* @param array<array-key, mixed>|object $data
*
* @throws JSONPathException
*/
private function recurse(array &$result, array|object $data): void
{
$result[] = (array)$data;
if (AccessHelper::isCollectionType($data)) {
foreach (AccessHelper::arrayValues($data) as $value) {
if (AccessHelper::isCollectionType($value)) {
$this->recurse($result, $value);
}
}
}
}
}