Completed
Pull Request — master (#11)
by David
05:30
created

ContextMerger   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B mergeContexts() 0 16 6
1
<?php
2
3
4
namespace TheCodingMachine\CMS\Utils;
5
6
7
class ContextMerger implements ContextMergerInterface
8
{
9
    /**
10
     * Merges 2 contexts in a custom way.
11
     *
12
     * @param mixed[] $context1
13
     * @param mixed[] $context2
14
     * @return mixed[]
15
     */
16
    public function mergeContexts(array $context1, array $context2): array
17
    {
18
        $finalContext = $context1;
19
        foreach ($context2 as $key => $value) {
20
            if (is_numeric($key)) {
21
                $finalContext[] = $value;
22
                continue;
23
            }
24
25
            if (isset($finalContext[$key]) && is_array($finalContext[$key]) && is_array($value)) {
26
                $finalContext[$key] = $this->mergeContexts($finalContext[$key], $value);
27
                continue;
28
            }
29
            $finalContext[$key] = $value;
30
        }
31
        return $finalContext;
32
    }
33
}