Completed
Push — develop ( 279bca...3f20e4 )
by
unknown
03:15
created

ArrayService::findByContainedKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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