MergeAttributes::mergeValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
namespace Malezha\Menu\Support;
3
4
class MergeAttributes
5
{
6
    /**
7
     * @var array
8
     */
9
    protected $arrays = [];
10
11
    /**
12
     * MergeAttributes constructor.
13
     */
14 15
    public function __construct()
15
    {
16 15
        $this->arrays = func_get_args();
17 15
    }
18
    
19
    /**
20
     * Merge array values as html attributes
21
     *
22
     * @return array
23
     */
24 15
    public function merge()
25
    {
26 15
        $arrays = $this->arrays;
27
28 15
        if (empty($arrays)) {
29 1
            return [];
30
        }
31
32 14
        $keys = [];
33 14
        foreach ($arrays as $array) {
34 14
            $keys = array_merge($keys, array_keys($array));
35
        }
36
37 14
        $keys = array_unique($keys);
38
39 14
        $merged = array_fill_keys($keys, null);
40
41 14
        foreach ($arrays as $array) {
42 14
            foreach ($keys as $key) {
43 12
                if (array_key_exists($key, $array)) {
44 14
                    $merged[$key] = $this->mergeValues($merged[$key], $array[$key]);
45
                }
46
            }
47
        }
48
49 14
        return $merged;
50
    }
51
    
52
    /**
53
     * @param string $valueOne
54
     * @param string $valueTwo
55
     * @return string
56
     */
57 12
    protected function mergeValues($valueOne, $valueTwo)
58
    {
59 12
        if (is_null($valueOne)) {
60 12
            return $valueTwo;
61
        }
62
        
63 2
        $valueOne = explode(' ', $valueOne);
64 2
        $valueTwo = explode(' ', $valueTwo);
65
66 2
        $merged = array_merge($valueOne, $valueTwo);
67
68 2
        return trim(implode(' ', $merged));
69
    }
70
}