Completed
Push — master ( e84808...5a9d2e )
by Oleg
07:58
created

Attributes::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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