Attributes   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 160
rs 10
c 0
b 0
f 0
ccs 50
cts 50
cp 1
wmc 20
lcom 1
cbo 1

16 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A set() 0 6 1
A all() 0 4 1
A has() 0 4 1
A push() 0 6 1
A put() 0 6 1
A merge() 0 6 1
A build() 0 13 3
A __toString() 0 4 1
A toArray() 0 7 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A __construct() 0 4 1
A forget() 0 6 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
namespace Malezha\Menu\Support;
3
4
use Malezha\Menu\Contracts\Attributes as AttributesContract;
5
6
/**
7
 * Class Attributes
8
 * @package Malezha\Menu
9
 */
10
class Attributes implements AttributesContract
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $attributes;
16
17
    /**
18
     * @inheritDoc
19
     */
20 44
    public function __construct(array $attributes)
21
    {
22 44
        $this->attributes = $attributes;
23 44
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28 6
    public function get($name, $default = null)
29
    {
30 6
        if (array_key_exists($name, $this->attributes)) {
31 5
            return $this->attributes[$name];
32
        }
33 1
        return $default;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 2
    public function set(array $attributes)
40
    {
41 2
        $this->attributes = $attributes;
42
43 2
        return $this;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 18
    public function all()
50
    {
51 18
        return $this->attributes;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 3
    public function has($name)
58
    {
59 3
        return array_key_exists($name, $this->attributes);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 1
    public function forget($name)
66
    {
67 1
        if ($this->has($name)) {
68 1
            unset($this->attributes[$name]);
69
        }
70 1
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 4
    public function push(array $attributes)
76
    {
77 4
        $this->attributes = array_merge($this->attributes, $attributes);
78
79 4
        return $this;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 6
    public function put($name, $value)
86
    {
87 6
        $this->attributes[$name] = $value;
88
89 6
        return $this;
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95 1
    public function merge(array $attributes)
96
    {
97 1
        $this->set((new MergeAttributes($this->all(), $attributes))->merge());
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 13
    public function build($attributes = [])
106
    {
107 13
        $attributes = (new MergeAttributes($this->all(), $attributes))->merge();
108 13
        ksort($attributes);
109
110 13
        $html = (count($attributes) > 0) ? ' ' : '';
111
112 13
        foreach ($attributes as $key => $value) {
113 11
            $html .= $key . '="' . $value . '" ';
114
        }
115
116 13
        return rtrim($html);
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 1
    public function __toString()
123
    {
124 1
        return $this->build();
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 4
    public function toArray()
131
    {
132 4
        $array = $this->all();
133 4
        ksort($array);
134
        
135 4
        return $array;
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141 1
    public function offsetExists($offset)
142
    {
143 1
        return $this->has($offset);
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149 1
    public function offsetGet($offset)
150
    {
151 1
        return $this->get($offset);
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157 1
    public function offsetSet($offset, $value)
158
    {
159 1
        $this->put($offset, $value);
160 1
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165 1
    public function offsetUnset($offset)
166
    {
167 1
        $this->forget($offset);
168
    }
169
}