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

ArrayService::findElement()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 9.9614

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 8.8571
ccs 5
cts 12
cp 0.4167
cc 5
eloc 11
nc 5
nop 2
crap 9.9614
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