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
|
|
|
} |