Completed
Push — master ( 38e886...df2920 )
by Oleg
04:55
created

MergeAttributes   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 67
wmc 9
lcom 1
cbo 0
ccs 28
cts 28
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mergeValues() 0 13 2
B merge() 0 27 6
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 11
    public function __construct()
15
    {
16 11
        $this->arrays = func_get_args();
17 11
    }
18
    
19
    /**
20
     * Merge array values as html attributes
21
     *
22
     * @return array
23
     */
24 11
    public function merge()
25
    {
26 11
        $arrays = $this->arrays;
27
28 11
        if (empty($arrays)) {
29 1
            return [];
30
        }
31
32 10
        $keys = [];
33 10
        foreach ($arrays as $array) {
34 10
            $keys = array_merge($keys, array_keys($array));
35 10
        }
36
37 10
        $keys = array_unique($keys);
38
39 10
        $merged = array_fill_keys($keys, null);
40
41 10
        foreach ($arrays as $array) {
42 10
            foreach ($keys as $key) {
43 9
                if (array_key_exists($key, $array)) {
44 9
                    $merged[$key] = $this->mergeValues($merged[$key], $array[$key]);
45 9
                }
46 10
            }
47 10
        }
48
49 10
        return $merged;
50
    }
51
    
52
    /**
53
     * @param string $valueOne
54
     * @param string $valueTwo
55
     * @return string
56
     */
57 9
    protected function mergeValues($valueOne, $valueTwo)
58
    {
59 9
        if (is_null($valueOne)) {
60 9
            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
}