Completed
Push — develop ( 29c45a...9c1d06 )
by René
03:15
created

ArrayService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 34.15%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 15
c 6
b 0
f 1
lcom 0
cbo 1
dl 0
loc 90
rs 10
ccs 14
cts 41
cp 0.3415

4 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceRecursiveByKey() 0 11 4
A findByContainedKey() 0 11 2
A findKeyChainsContainingKey() 0 18 4
B findElement() 0 18 5
1
<?php
2
3
/**
4
 * ArrayService
5
 */
6
7
namespace HDNET\OnpageIntegration\Service;
8
9
/**
10
 * Class ArrayService
11
 */
12
class ArrayService extends AbstractService
13
{
14
15
    /**
16
     * Replace a key $replaceKey with $replaceItem
17
     *
18
     * @param array $array
19
     * @param       $replaceItem
20
     * @param       $replaceKey
21
     *
22
     * @return array
23
     */
24 3
    public function replaceRecursiveByKey(array $array, $replaceItem, $replaceKey)
25
    {
26 3
        foreach ($array as $key => &$item) {
27 2
            if ($key === $replaceKey) {
28 2
                $item = $replaceItem;
29 2
            } elseif (is_array($item)) {
30 1
                $item = $this->replaceRecursiveByKey($item, $replaceItem, $replaceKey);
31 1
            }
32 3
        }
33 3
        return $array;
34
    }
35
36
    /**
37
     * @param array $array
38
     * @param string $elementName
39
     * @return array
40
     */
41 1
    public function findElement(array $array, $elementName)
42
    {
43 1
        foreach ($array as $key => $element) {
44 1
            if ($key !== $elementName) {
45 1
                if (!is_array($element)) {
46 1
                    return [];
47
                }
48
                $result = $this->findElement($element, $elementName);
49
                if ($result) {
50
                    return $result;
51
                }
52
            } else {
53
                return $element;
54
            }
55
        }
56
57
        return [];
58
    }
59
60
    /**
61
     * @param array $array
62
     * @param string $searchKey
63
     * @return array
64
     */
65
    public function findByContainedKey(array $array, $searchKey)
66
    {
67
        $keyChains = $this->findKeyChainsContainingKey($array, $searchKey, [], []);
68
        $results   = [];
69
70
        foreach ($keyChains as $chain) {
71
            $results[] = implode('_', $chain);
72
        }
73
74
        return $results;
75
    }
76
77
    /**
78
     * @param array $array
79
     * @param string $searchKey
80
     * @param array $keyChains
81
     * @return array
82
     */
83
    protected function findKeyChainsContainingKey(array $array, $searchKey, array $keyChains, $keyChain)
84
    {
85
        foreach ($array as $key => $value) {
86
            if ($key !== $searchKey) {
87
                if (!is_array($value)) {
88
                    continue;
89
                }
90
                $keyChain[] = $key;
91
                $keyChains  = $this->findKeyChainsContainingKey($value, $searchKey, $keyChains, $keyChain);
92
                array_pop($keyChain);
93
            } else {
94
                $keyChains[] = $keyChain;
95
                return $keyChains;
96
            }
97
        }
98
99
        return $keyChains;
100
    }
101
}
102