ContextMerger::mergeContexts()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 2
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
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
}