Completed
Push — master ( 31d69b...c656e0 )
by Marcus
09:48
created

ArrayUtil   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 79
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B replace() 0 45 7
A isBranch() 0 14 3
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Utils\ArrayUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Utils;
22
23
/**
24
 * Utility class that provides custom array handling functionality.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2019 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import-configuration-jms
30
 * @link      http://www.techdivision.com
31
 */
32
class ArrayUtil implements ArrayUtilInterface
33
{
34
35
    /**
36
     * Replaces the values of the first array with the ones from the arrays
37
     * that has been passed as additional arguments.
38
     *
39
     * @param array ...$arrays The arrays with the values that replace the first one
40
     *
41
     * @return array The array with the replaced values
42
     */
43
    public function replace(...$arrays)
44
    {
45
46
        // load the passed arrays
47
        $values = func_get_args();
48
49
        // initialize the main array the others should be merged into
50
        $main = array();
51
52
        // query whether we've more then one arrays passed
53
        if (sizeof($values) > 1) {
54
            // if yes, the first one is the main array
55
            $main = array_shift($values);
56
        }
57
58
        // iterate over the other arrays and replace the
59
        // values of the main array with their values
60
        while ($arr = array_shift($values)) {
61
            // iterate over the arrays attributes
62
            foreach ($arr as $key => $src) {
63
                // query whether the attribute is an array or not, if yes we
64
                // have to replace the values of the main attribute with the
65
                // values of the actual array recursively
66
                if (is_array($src)) {
67
                    // initialize the array to merge the values into
68
                    $dest = array();
69
70
                    // override the array with the values of the main array if
71
                    // the actual attribute also contains arrays itself
72
                    if ($this->isBranch($src)) {
73
                        $dest = isset($main[$key]) ? $main[$key] : array();
74
                    }
75
76
                    // finally replace the values in the main attribute
77
                    $main[$key] = $this->replace($dest, $src);
78
                } else {
79
                    // simply override the values of the main array
80
                    $main[$key] = $src;
81
                }
82
            }
83
        }
84
85
        // return the array with the replaced values
86
        return $main;
87
    }
88
89
    /**
90
     * Query's whether or noth the passed node is a branch or a leaf.
91
     *
92
     * @param array $node The node to query for
93
     *
94
     * @return boolean TRUE if the node is a branch, else FALSE
95
     */
96
    protected function isBranch(array $node)
97
    {
98
99
        // iterate over th nodes attributes and query whether
100
        // or not at least one of it is an array
101
        foreach ($node as $attr) {
102
            if (is_array($attr)) {
103
                return true;
104
            }
105
        }
106
107
        // return false if the node only contains scalar values
108
        return false;
109
    }
110
}
111