Passed
Push — master ( e5b7b4...ec2be8 )
by Julien
14:08
created

Transform   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 74
ccs 24
cts 28
cp 0.8571
rs 10
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
F flattenKeys() 0 61 18
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Utils;
12
13
/**
14
 * Class Slug
15
 *
16
 * @author Julien Turbide <[email protected]>
17
 * @copyright Zemit Team <[email protected]>
18
 *
19
 * @since 1.0
20
 * @version 1.0
21
 *
22
 * @package Zemit\Utils
23
 */
24
class Transform
25
{
26
    /**
27
     * Here to parse the columns parameter into some kind of flatten array with
28
     * the key path separated by dot "my.path" and the value true, false or a callback function
29
     * including the ExposeBuilder object
30
     *
31
     * @param mixed $array
32
     * @param string|null $context
33
     * @param string|null $delimiter
34
     *
35
     * @return array|null
36
     */
37 1
    final public static function flattenKeys($array = null, ?string $delimiter = '.', ?bool $lowerKey = true, ?string $context = null) : ?array
38
    {
39
        // nothing passed
40 1
        if (!isset($array)) {
41
            return null;
42
        }
43
        
44 1
        $ret = [];
45
        
46 1
        if (!is_array($array)) {
47 1
            $array = [$array];
48
        }
49
        
50 1
        foreach ($array as $key => $value) {
51
            
52
            // handle our special attribute
53 1
            if (is_bool($key)) {
54
                $value = $key;
55
                $key = null;
56
            }
57
            
58
            // flip value to key
59 1
            if (is_int($key)) {
60 1
                if (is_string($value)) {
61 1
                    $key = $value;
62 1
                    $value = true;
63
                }
64
                else {
65 1
                    $key = null;
66
                }
67
            }
68
            
69
            // force lower case key
70 1
            if (is_string($key)) {
71 1
                $key = trim($lowerKey ? mb_strtolower($key) : $key);
72
            }
73
            
74
            //
75 1
            if (is_string($value) && empty($value)) {
76
                $value = true;
77
            }
78
            
79
            // set the key
80 1
            $currentKey = (!empty($context) ? $context . (!empty($key) ? $delimiter : null) : null) . $key;
81
            
82 1
            if (is_array($value) || is_object($value)) {
83 1
                $subRet = self::flattenKeys(is_callable($value)? $value() : $value, $delimiter, $lowerKey, $currentKey);
84 1
                if (is_array($subRet)) {
85 1
                    $ret = array_merge_recursive($ret, $subRet);
86
                }
87
                
88 1
                if (!isset($ret[$currentKey])) {
89 1
                    $ret[$currentKey] = false;
90
                }
91
            }
92
            else {
93 1
                $ret[$currentKey] = $value;
94
            }
95
        }
96
        
97 1
        return $ret;
98
    }
99
}
100