Completed
Push — refactoring ( 84ac1f...ab1ebc )
by Oleg
04:25
created

Attributes   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 167
wmc 21
lcom 1
cbo 1
ccs 58
cts 58
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A set() 0 6 1
A all() 0 4 1
A has() 0 4 1
A forget() 0 4 1
A push() 0 6 1
A put() 0 6 1
A merge() 0 6 1
B mergeArrayValues() 0 27 6
A mergeValues() 0 13 2
A build() 0 12 3
A __toString() 0 4 1
1
<?php
2
3
namespace Malezha\Menu\Entity;
4
5
use Illuminate\Support\Collection;
6
7
class Attributes
8
{
9
    protected $list;
10
11
    /**
12
     * @param array $attributes
13
     */
14 24
    function __construct(array $attributes)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16 24
        $this->list = new Collection($attributes);
17 24
    }
18
19
    /**
20
     * @param string $name
21
     * @param mixed $default
22
     * @return mixed
23
     */
24 4
    public function get($name, $default = null)
25
    {
26 4
        return $this->list->get($name, $default);
27
    }
28
29
    /**
30
     * @param array $attributes
31
     * @return $this
32
     */
33 2
    public function set(array $attributes)
34
    {
35 2
        $this->list = new Collection($attributes);
36
37 2
        return $this;
38
    }
39
40
    /**
41
     * @return array
42
     */
43 8
    public function all()
44
    {
45 8
        return $this->list->all();
46
    }
47
48
    /**
49
     * @param string $name
50
     * @return bool
51
     */
52 1
    public function has($name)
53
    {
54 1
        return $this->list->has($name);
55
    }
56
57
    /**
58
     * @param string $name
59
     */
60 1
    public function forget($name)
61
    {
62 1
        $this->list->forget($name);
63 1
    }
64
65
    /**
66
     * @param array $attributes
67
     * @return $this
68
     */
69 2
    public function push(array $attributes)
70
    {
71 2
        $this->list = $this->list->merge($attributes);
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * @param string $name
78
     * @param mixed $value
79
     * @return $this
80
     */
81 1
    public function put($name, $value)
82
    {
83 1
        $this->list->put($name, $value);
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @param array $attributes
90
     * @return $this
91
     */
92 1
    public function merge(array $attributes)
93
    {
94 1
        $this->set(self::mergeArrayValues($this->list->toArray(), $attributes));
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @return array|bool
101
     */
102 6
    public static function mergeArrayValues()
103
    {
104 6
        if (func_num_args() <= 1) {
105 1
            throw new \RuntimeException("Must has min two parameters.");
106
        }
107
108 5
        $arrays = func_get_args();
109 5
        $keys = [];
110
111 5
        foreach ($arrays as $array) {
112 5
            $keys = array_merge($keys, array_keys($array));
113 5
        }
114
115 5
        $keys = array_unique($keys);
116
117 5
        $merged = array_fill_keys($keys, null);
118
119 5
        foreach ($arrays as $array) {
120 5
            foreach ($keys as $key) {
121 4
                if (array_key_exists($key, $array)) {
122 4
                    $merged[$key] = self::mergeValues($merged[$key], $array[$key]);
123 4
                }
124 5
            }
125 5
        }
126
127 5
        return $merged;
128
    }
129
130
    /**
131
     * @param string $valueOne
132
     * @param string $valueTwo
133
     * @return string
134
     */
135 4
    protected static function mergeValues($valueOne, $valueTwo)
136
    {
137 4
        if (is_null($valueOne)) {
138 4
            return $valueTwo;
139
        }
140
        
141 2
        $valueOne = explode(' ', $valueOne);
142 2
        $valueTwo = explode(' ', $valueTwo);
143
144 2
        $merged = array_merge($valueOne, $valueTwo);
145
146 2
        return trim(implode(' ', $merged));
147
    }
148
149
    /**
150
     * @param array $attributes
151
     * @return string
152
     */
153 4
    public function build($attributes = [])
154
    {
155 4
        $attributes = $this->mergeArrayValues($this->all(), $attributes);
156
157 4
        $html = (count($attributes) > 0) ? ' ' : '';
158
159 4
        foreach ($attributes as $key => $value) {
160 3
            $html .= $key . '="' . $value . '" ';
161 4
        }
162
163 4
        return rtrim($html);
164
    }
165
166
    /**
167
     * @return string
168
     */
169 1
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
170
    {
171 1
        return $this->build();
172
    }
173
}