ArrayService::findKeyChainsContainingKey()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
ccs 0
cts 13
cp 0
cc 4
eloc 12
nc 4
nop 4
crap 20
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
        if (array_key_exists($elementName, $array)) {
44 1
            return $array[$elementName];
45
        }
46
47 1
        foreach ($array as $key => $element) {
48 1
            if (!is_array($element)) {
49 1
                continue;
50
            }
51
52 1
            $result = $this->findElement($element, $elementName);
53 1
            if ($result) {
54 1
                return $result;
55
            }
56
        }
57
58
        return [];
59
    }
60
61
    /**
62
     * @param array $array
63
     * @param string $searchKey
64
     * @return array
65
     */
66
    public function findByContainedKey(array $array, $searchKey)
67
    {
68
        $keyChains = $this->findKeyChainsContainingKey($array, $searchKey, [], []);
69
        $results = [];
70
71
        foreach ($keyChains as $chain) {
72
            $results[] = implode('_', $chain);
73
        }
74
75
        return $results;
76
    }
77
78
    /**
79
     * @param array $array
80
     * @param string $searchKey
81
     * @param array $keyChains
82
     * @return array
83
     */
84
    protected function findKeyChainsContainingKey(array $array, $searchKey, array $keyChains, $keyChain)
85
    {
86
        foreach ($array as $key => $value) {
87
            if ($key !== $searchKey) {
88
                if (!is_array($value)) {
89
                    continue;
90
                }
91
                $keyChain[] = $key;
92
                $keyChains = $this->findKeyChainsContainingKey($value, $searchKey, $keyChains, $keyChain);
93
                array_pop($keyChain);
94
            } else {
95
                $keyChains[] = $keyChain;
96
                return $keyChains;
97
            }
98
        }
99
100
        return $keyChains;
101
    }
102
}
103