Passed
Push — master ( 11eaad...3f8f84 )
by Allan
02:05
created

DataUtils   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 67
dl 0
loc 151
rs 10
c 4
b 0
f 0
wmc 28

10 Methods

Rating   Name   Duplication   Size   Complexity  
A extractOrderedItems() 0 7 1
A walkArrayNodes() 0 13 3
A embedGroupKeyToItems() 0 9 2
A extractValue() 0 5 2
A removeKeysByPrefix() 0 12 1
A getValueByPath() 0 17 5
A prefixArrayValues() 0 7 1
A listToGroups() 0 25 5
B getNodeReferencesByPaths() 0 27 7
A extractItems() 0 6 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Utils;
7
8
class DataUtils
9
{
10
    public function listToGroups(array $items, $keyMatcher)
11
    {
12
        $result = array();
13
14
        $key = null;
15
        
16
        foreach ($items as $item) {
17
            if (preg_match($keyMatcher, $item, $matches)) {
18
                $key = $matches[1];
19
20
                continue;
21
            }
22
23
            if (!$key) {
24
                continue;
25
            }
26
27
            if (!isset($result[$key])) {
28
                $result[$key] = array();
29
            }
30
            
31
            $result[$key][] = $item;
32
        }
33
34
        return $result;
35
    }
36
    
37
    public function embedGroupKeyToItems(array $groups, $template = '%s:%s')
38
    {
39
        $result = array();
40
        
41
        foreach ($groups as $key => $items) {
42
            $result[$key] = $this->prefixArrayValues($items, $key, $template);
43
        }
44
45
        return $result;
46
    }
47
    
48
    public function extractOrderedItems(array $items, array $targets)
49
    {
50
        $targets = array_flip($targets);
51
52
        return array_replace(
53
            array_intersect_key($targets, $items),
54
            array_intersect_key($items, $targets)
55
        );
56
    }
57
    
58
    public function prefixArrayValues(array $data, $prefix, $template = '%s%s')
59
    {
60
        return array_map(
61
            function ($value) use ($prefix, $template) {
62
                return sprintf($template, $prefix, $value);
63
            },
64
            $data
65
        );
66
    }
67
    
68
    public function extractItems(array &$data, array $keys)
69
    {
70
        $subset = array_intersect($data, $keys);
71
        $data = array_diff($data, $keys);
72
73
        return $subset;
74
    }
75
76
    public function extractValue(array $data, $key, $default = null)
77
    {
78
        return isset($data[$key])
79
            ? $data[$key]
80
            : $default;
81
    }
82
    
83
    public function removeKeysByPrefix(array $data, $prefix)
84
    {
85
        $whitelist = array_filter(
86
            array_keys($data),
87
            function ($key) use ($prefix) {
88
                return strpos($key, $prefix) !== 0;
89
            }
90
        );
91
        
92
        return array_intersect_key(
93
            $data,
94
            array_flip($whitelist)
95
        );
96
    }
97
98
    public function walkArrayNodes(array $list, \Closure $callback)
99
    {
100
        $list = $callback($list);
101
102
        foreach ($list as $key => $value) {
103
            if (!is_array($value)) {
104
                continue;
105
            }
106
107
            $list[$key] = $this->walkArrayNodes($value, $callback);
108
        }
109
110
        return $list;
111
    }
112
    
113
    public function getNodeReferencesByPaths(array &$data, array $paths)
114
    {
115
        $stack = array();
116
117
        foreach ($paths as $path) {
118
            $stack[] = array(array(&$data), explode('/', $path));
119
        }
120
121
        $result = array();
122
        
123
        while ($item = array_shift($stack)) {
124
            $segment = array_shift($item[1]);
125
126
            foreach ($item[0] as &$node) {
127
                if ($segment === null) {
128
                    $result[] = &$node;
129
                } elseif ($segment === '*') {
130
                    $stack[] = array(&$node, $item[1]);
131
                } elseif (isset($node[$segment])) {
132
                    $stack[] = array(array(&$node[$segment]), $item[1]);
133
                }
134
                
135
                unset($node);
136
            }
137
        }
138
        
139
        return $result;
140
    }
141
142
    public function getValueByPath(array $data, $path, $default = null)
143
    {
144
        if (!is_array($path)) {
145
            $path = explode('/', $path);
146
        }
147
148
        foreach ($path as $key) {
149
            if (is_array($data) && array_key_exists($key, $data)) {
150
                $data = $data[$key];
151
                
152
                continue;
153
            }
154
155
            return $default;
156
        }
157
158
        return $data;
159
    }
160
}
161