Passed
Push — master ( d5c5e0...724c44 )
by Allan
02:09
created

DataUtils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeKeysByPrefix() 0 9 1
A walkArrayNodes() 0 13 3
A renderConstant() 0 7 2
A renderValue() 0 7 2
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Utils;
7
8
class DataUtils
9
{
10
    public function removeKeysByPrefix(array $data, $prefix)
11
    {
12
        return array_intersect_key(
13
            $data,
14
            array_flip(
15
                array_filter(
16
                    array_keys($data),
17
                    function ($key) use ($prefix) {
18
                        return strpos($key, $prefix) !== 0;
19
                    }
20
                )
21
            )
22
        );
23
    }
24
25
    public function walkArrayNodes(array $list, \Closure $callback)
26
    {
27
        $list = $callback($list);
28
29
        foreach ($list as $key => $value) {
30
            if (!is_array($value)) {
31
                continue;
32
            }
33
34
            $list[$key] = $this->walkArrayNodes($value, $callback);
35
        }
36
37
        return $list;
38
    }
39
40
    public function renderConstant(array $data, array $keys, $constant, $default = '')
41
    {
42
        if (!array_intersect_key($data, array_flip($keys))) {
43
            return $default;
44
        }
45
46
        return $constant;
47
    }
48
49
    public function renderValue(array $data, $key, $format, $default = '')
50
    {
51
        if (!isset($data[$key])) {
52
            return $default;
53
        }
54
55
        return sprintf($format, $data[$key]);
56
    }
57
}
58