Lines 100.00% 14 / 14
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getValue 100.00% 1 / 1 100.00% 1 / 1 1
 getUnflattenedArray 100.00% 12 / 12 100.00% 1 / 1 5
5class UnflattedArray
6{
7    protected $value = null;
8
9    public function __construct(mixed $value)
10    {
11        $this->value = $value;
12    }
13
14    public function getValue(): mixed
15    {
16        return $this->value;
17    }
18
19    /**
20      * split fields
21      * If a key to a field has two underscores "__" it should go into a subarray
22      * ATTENTION: performance critical function, keep highly optimized!
23      *
24      * @return array
25      */
26    public function getUnflattenedArray()
27    {
28        $hash = $this->value;
29        foreach ($hash as $key => $value) {
30            if (false !== strpos($key, '__')) {
31                $currentLevel =& $hash;
32                unset($hash[$key]);
33                foreach (explode('__', $key) as $currentKey) {
34                    if (!isset($currentLevel[$currentKey])) {
35                        $currentLevel[$currentKey] = [];
36                    }
37                    $currentLevel =& $currentLevel[$currentKey];
38                }
39                $currentLevel = $value;
40            }
41        }
42        $this->value = $hash;
43        return (array)$hash;
44    }
45}