ArrHelper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 49
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isStrKeysArray() 0 8 1
A unflatten() 0 11 4
A set() 0 12 4
1
<?php
2
3
namespace Bricks\Helper;
4
5
/**
6
 * Class ArrHelper
7
 * @package Bricks\Helper
8
 */
9
class ArrHelper
10
{
11
    /**
12
     * @param array $array
13
     * @return bool
14
     */
15
    public static function isStrKeysArray(array $array): bool
16
    {
17 13
        $filtered = array_filter($array, function ($key) {
18 13
            return !is_string($key);
19 13
        }, ARRAY_FILTER_USE_KEY);
20
21 13
        return empty($filtered);
22
    }
23
24
    /**
25
     * @param array $array
26
     * @return array
27
     */
28 20
    public static function unflatten(array $array): array
29
    {
30 20
        $output = [];
31 20
        foreach ($array as $key => $value) {
32 20
            self::set($output, $key, $value);
33 20
            if (is_array($value) && strpos($key, '.') === false) {
34 20
                $output[$key] = static::unflatten($value);
35
            }
36
        }
37 20
        return $output;
38
    }
39
40
    /**
41
     * @param array $array
42
     * @param string $key
43
     * @param mixed $value
44
     */
45 20
    private static function set(array &$array, string $key, $value)
46
    {
47 20
        $keys = explode('.', $key);
48 20
        while (sizeof($keys) > 1) {
49 3
            $key = array_shift($keys);
50 3
            if (!isset($array[$key]) || !is_array($array[$key])) {
51 3
                $array[$key] = [];
52
            }
53 3
            $array =& $array[$key];
54
        }
55 20
        $array[array_shift($keys)] = $value;
56
    }
57
}