Conditions | 6 |
Paths | 6 |
Total Lines | 29 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php declare(strict_types=1); |
||
7 | 3 | public static function makeFromFlatArray( |
|
8 | array &$data, |
||
9 | $parentId = 0, |
||
10 | string $key = 'id', |
||
11 | string $parentKey = 'parentId', |
||
12 | string $childKey = 'nodes' |
||
13 | ) { |
||
14 | 3 | $branch = []; |
|
15 | |||
16 | 3 | foreach ($data as $k => $v) { |
|
17 | 3 | if (!isset($v[$key])) { |
|
18 | 1 | throw new \InvalidArgumentException('Invalid identifier key'); |
|
19 | } |
||
20 | |||
21 | 2 | if (!isset($v[$parentKey])) { |
|
22 | 1 | throw new \InvalidArgumentException('Invalid parent key'); |
|
23 | } |
||
24 | |||
25 | 1 | if ($v[$parentKey] == $parentId) { |
|
26 | 1 | $childNodes = static::makeFromFlatArray($data, $v[$key], $key, $parentKey, $childKey); |
|
27 | 1 | if ($childNodes) { |
|
28 | 1 | $v[$childKey] = $childNodes; |
|
29 | } |
||
30 | 1 | $branch[$v[$key]] = $v; |
|
31 | 1 | unset($data[$k]); |
|
32 | } |
||
33 | } |
||
34 | |||
35 | 1 | return $branch; |
|
36 | } |
||
38 |