Total Complexity | 9 |
Total Lines | 87 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
7 | class ArrayLocator implements ArrayLocatorInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $array; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $fieldMap; |
||
18 | |||
19 | /** |
||
20 | * ArrayLocator constructor. |
||
21 | */ |
||
22 | 2 | public function __construct() |
|
26 | 2 | } |
|
27 | |||
28 | |||
29 | /** |
||
30 | * ArrayLocator constructor. |
||
31 | * |
||
32 | * @param array $array |
||
33 | */ |
||
34 | 2 | public function init(array $array) |
|
35 | { |
||
36 | 2 | $this->array = $array; |
|
37 | 2 | $this->fieldMap = null; |
|
38 | 2 | } |
|
39 | |||
40 | /** |
||
41 | * @param string $path |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 2 | public function getKeysByPath(string $path) |
|
46 | { |
||
47 | 2 | if ($this->fieldMap === null) { |
|
48 | 2 | $this->buildFieldMap(); |
|
49 | } |
||
50 | |||
51 | 2 | return array_values( |
|
52 | 2 | $this->getAllKeys($this->fieldMap, $path) |
|
53 | ); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param $array |
||
58 | * @param $path |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | 2 | protected function getAllKeys($array, $path): array |
|
66 | } |
||
67 | |||
68 | 2 | protected function buildFieldMap(): void |
|
69 | { |
||
70 | 2 | $this->fieldMap = $this->getAllFields($this->array, '', []); |
|
71 | 2 | } |
|
72 | |||
73 | /** |
||
74 | * @param array $array |
||
75 | * @param string $path |
||
76 | * @param array $fieldMap |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | 2 | protected function getAllFields(array $array, string $path, array $fieldMap): array |
|
94 | } |
||
95 | } |