1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oddvalue\LinkBuilder; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
7
|
|
|
|
8
|
|
|
class HtmlAttributes implements ArrayAccess, Htmlable |
9
|
|
|
{ |
10
|
|
|
protected $attributes = []; |
11
|
|
|
protected $classes = []; |
12
|
|
|
|
13
|
51 |
|
public function __construct($attributes = []) |
14
|
|
|
{ |
15
|
51 |
|
$this->set($attributes); |
16
|
51 |
|
} |
17
|
|
|
|
18
|
36 |
|
public static function make($attributes = []) |
19
|
|
|
{ |
20
|
36 |
|
return new static($attributes); |
21
|
|
|
} |
22
|
|
|
|
23
|
9 |
|
public function get(string $attribute = null) |
24
|
|
|
{ |
25
|
9 |
|
if (is_null($attribute)) { |
26
|
3 |
|
return $this->toArray(); |
27
|
|
|
} |
28
|
9 |
|
if ($attribute === 'class') { |
29
|
3 |
|
return $this->getClass(); |
30
|
|
|
} |
31
|
9 |
|
return $this->attributes[$attribute]; |
32
|
|
|
} |
33
|
|
|
|
34
|
51 |
|
public function set($attributes, string $value = null) |
35
|
|
|
{ |
36
|
51 |
|
if (! is_array($attributes) || $attributes instanceof ArrayAccess) { |
37
|
9 |
|
$attributes = [$attributes => $value]; |
38
|
|
|
} |
39
|
|
|
|
40
|
51 |
|
foreach ($attributes as $name => $value) { |
41
|
30 |
|
if ($name === 'class') { |
42
|
15 |
|
$this->setClass($value); |
43
|
15 |
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
24 |
|
if (is_int($name)) { |
47
|
3 |
|
$name = $value; |
48
|
3 |
|
$value = null; |
49
|
|
|
} |
50
|
|
|
|
51
|
24 |
|
$this->attributes[$name] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
51 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function remove($attributes) |
58
|
|
|
{ |
59
|
3 |
|
foreach ($this->wrapArray($attributes) as $name) { |
60
|
3 |
|
if ($name === 'class') { |
61
|
3 |
|
$this->clearClass(); |
62
|
3 |
|
continue; |
63
|
|
|
} |
64
|
3 |
|
unset($this->attributes[$name]); |
65
|
|
|
} |
66
|
3 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function has($attribute) : bool |
70
|
|
|
{ |
71
|
6 |
|
if ($attribute === 'class') { |
72
|
3 |
|
return $this->hasClass(); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return array_key_exists($attribute, $this->attributes); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
public function addClass($classes) |
79
|
|
|
{ |
80
|
6 |
|
$this->classes = array_unique(array_merge($this->classes, $this->splitClass($classes))); |
81
|
6 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
18 |
|
public function setClass($class) |
85
|
|
|
{ |
86
|
18 |
|
$this->classes = $this->splitClass($class); |
87
|
18 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
18 |
|
public function getClass() |
91
|
|
|
{ |
92
|
18 |
|
return implode(' ', $this->classes); |
93
|
|
|
} |
94
|
|
|
|
95
|
27 |
|
public function hasClass(string $class = null) |
96
|
|
|
{ |
97
|
27 |
|
if (! $class) { |
98
|
27 |
|
return count($this->classes) > 0; |
99
|
|
|
} |
100
|
3 |
|
return in_array($class, $this->classes); |
101
|
|
|
} |
102
|
|
|
|
103
|
24 |
|
private function splitClass($classString) : array |
104
|
|
|
{ |
105
|
|
|
return call_user_func_array('array_merge', array_map(function ($class) { |
106
|
24 |
|
return explode(' ', $class); |
107
|
24 |
|
}, $this->wrapArray($classString))); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
public function clearClass() |
111
|
|
|
{ |
112
|
3 |
|
$this->classes = []; |
113
|
3 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
public function removeClass($class) |
117
|
|
|
{ |
118
|
3 |
|
if (($key = array_search($class, $this->classes)) !== false) { |
119
|
3 |
|
unset($this->classes[$key]); |
120
|
|
|
} |
121
|
3 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
24 |
|
private function wrapArray($value) |
125
|
|
|
{ |
126
|
24 |
|
if (is_null($value)) { |
127
|
3 |
|
return []; |
128
|
|
|
} |
129
|
|
|
|
130
|
24 |
|
return is_array($value) ? $value : [$value]; |
131
|
|
|
} |
132
|
|
|
|
133
|
33 |
|
public function isEmpty() : bool |
134
|
|
|
{ |
135
|
33 |
|
return empty($this->attributes) && empty($this->classes); |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
public function offsetExists($offset) : bool |
139
|
|
|
{ |
140
|
3 |
|
return $this->has($offset); |
141
|
|
|
} |
142
|
|
|
|
143
|
6 |
|
public function offsetGet($offset) |
144
|
|
|
{ |
145
|
6 |
|
return $this->get($offset); |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
public function offsetSet($offset, $value) : void |
149
|
|
|
{ |
150
|
3 |
|
$this->set($offset, $value); |
151
|
3 |
|
} |
152
|
|
|
|
153
|
3 |
|
public function offsetUnset($offset) : void |
154
|
|
|
{ |
155
|
3 |
|
$this->remove($offset); |
156
|
3 |
|
} |
157
|
|
|
|
158
|
24 |
|
public function toArray() |
159
|
|
|
{ |
160
|
24 |
|
return array_merge($this->attributes, $this->hasClass() ? ['class' => $this->getClass()] : []); |
161
|
|
|
} |
162
|
|
|
|
163
|
33 |
|
public function toHtml() |
164
|
|
|
{ |
165
|
33 |
|
if ($this->isEmpty()) { |
166
|
15 |
|
return ''; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return ' ' . implode(' ', array_map(function ($attribute, $value) { |
170
|
21 |
|
if (is_null($value) || $value === '') { |
171
|
9 |
|
return $attribute; |
172
|
|
|
} |
173
|
|
|
|
174
|
18 |
|
return "{$attribute}=\"{$value}\""; |
175
|
21 |
|
}, array_keys($this->toArray()), $this->toArray())); |
176
|
|
|
} |
177
|
|
|
|
178
|
30 |
|
public function __toString() |
179
|
|
|
{ |
180
|
30 |
|
return $this->toHtml(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|