Configuration::getMergedValue()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Brofist\Configuration;
4
5
class Configuration implements ConfigurationInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    private $config;
11
12
    public function __construct(array $config = [])
13
    {
14
        $this->config = $config;
15
    }
16
17
    public function toArray()
18
    {
19
        return $this->config;
20
    }
21
22
    public function merge(ConfigurationInterface $other)
23
    {
24
        $arrayConfig = $this->mergeArray($this->toArray(), $other->toArray());
25
        return new self($arrayConfig);
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    protected function mergeArray(array $original, array $other)
32
    {
33
        $newConfig = $original;
34
35
        foreach ($other as $key => $value) {
36
            $newConfig[$key] = $this->getMergedValue($newConfig[$key], $value);
37
        }
38
39
        return $newConfig;
40
    }
41
42
    /**
43
     * @param mixed $old
44
     * @param mixed $old
45
     *
46
     * @return mixed
47
     */
48
    protected function getMergedValue($old, $new)
49
    {
50
        if (is_array($old) && is_array($new)) {
51
            return array_merge($old, $new);
52
        }
53
54
        return $new;
55
    }
56
}
57