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
|
39 |
|
public function __construct($attributes = []) |
14
|
|
|
{ |
15
|
39 |
|
$this->set($attributes); |
16
|
39 |
|
} |
17
|
|
|
|
18
|
24 |
|
public static function make($attributes = []) |
19
|
|
|
{ |
20
|
24 |
|
return new static($attributes); |
21
|
|
|
} |
22
|
|
|
|
23
|
3 |
|
public function get(string $attribute = null) |
24
|
|
|
{ |
25
|
3 |
|
if (is_null($attribute)) { |
26
|
|
|
return $this->toArray(); |
27
|
|
|
} |
28
|
3 |
|
if ($attribute === 'class') { |
29
|
|
|
return $this->getClass(); |
30
|
|
|
} |
31
|
3 |
|
return $this->attributes[$attribute]; |
32
|
|
|
} |
33
|
|
|
|
34
|
39 |
|
public function set($attributes, string $value = null) |
35
|
|
|
{ |
36
|
39 |
|
if (! is_array($attributes) || $attributes instanceof ArrayAccess) { |
37
|
9 |
|
$attributes = [$attributes => $value]; |
38
|
|
|
} |
39
|
|
|
|
40
|
39 |
|
foreach ($attributes as $name => $value) { |
41
|
18 |
|
if ($name === 'class') { |
42
|
9 |
|
$this->setClass($value); |
43
|
9 |
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
if (is_int($name)) { |
47
|
3 |
|
$name = $value; |
48
|
3 |
|
$value = null; |
49
|
|
|
} |
50
|
|
|
|
51
|
15 |
|
$this->attributes[$name] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
39 |
|
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
|
|
|
$this->clearClass(); |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
3 |
|
unset($this->attributes[$name]); |
65
|
|
|
} |
66
|
3 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function has($attribute) : bool |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if ($offset === 'class') { |
|
|
|
|
72
|
|
|
return $this->hasClass(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return array_key_exists($offset, $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
|
9 |
|
public function setClass($class) |
85
|
|
|
{ |
86
|
9 |
|
$this->classes = $this->splitClass($class); |
87
|
9 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
public function getClass() |
91
|
|
|
{ |
92
|
9 |
|
return implode(' ', $this->classes); |
93
|
|
|
} |
94
|
|
|
|
95
|
15 |
|
public function hasClass(string $class = null) |
96
|
|
|
{ |
97
|
15 |
|
if (! $class) { |
98
|
12 |
|
return count($this->classes) > 0; |
99
|
|
|
} |
100
|
3 |
|
return in_array($class, $this->classes); |
101
|
|
|
} |
102
|
|
|
|
103
|
15 |
|
private function splitClass($classString) : array |
104
|
|
|
{ |
105
|
|
|
return call_user_func_array('array_merge', array_map(function ($class) { |
106
|
15 |
|
return explode(' ', $class); |
107
|
15 |
|
}, $this->wrapArray($classString))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function clearClass() |
111
|
|
|
{ |
112
|
|
|
return $this->setClass([]); |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function removeClass($class) |
116
|
|
|
{ |
117
|
3 |
|
if (($key = array_search($class, $this->classes)) !== false) { |
118
|
3 |
|
unset($this->classes[$key]); |
119
|
|
|
} |
120
|
3 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
18 |
|
private function wrapArray($value) |
124
|
|
|
{ |
125
|
18 |
|
if (is_null($value)) { |
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
|
129
|
18 |
|
return is_array($value) ? $value : [$value]; |
130
|
|
|
} |
131
|
|
|
|
132
|
27 |
|
public function isEmpty() : bool |
133
|
|
|
{ |
134
|
27 |
|
return empty($this->attributes) && empty($this->classes); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function offsetExists($offset) : bool |
138
|
|
|
{ |
139
|
|
|
return $this->has($offset); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
public function offsetGet($offset) |
143
|
|
|
{ |
144
|
3 |
|
return $this->get($offset); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
public function offsetSet($offset, $value) : void |
148
|
|
|
{ |
149
|
3 |
|
$this->set($offset, $value); |
150
|
3 |
|
} |
151
|
|
|
|
152
|
|
|
public function offsetUnset($offset) : void |
153
|
|
|
{ |
154
|
|
|
$this->remove($offset); |
155
|
|
|
} |
156
|
|
|
|
157
|
12 |
|
public function toArray() |
158
|
|
|
{ |
159
|
12 |
|
return array_merge($this->attributes, $this->hasClass() ? ['class' => $this->getClass()] : []); |
160
|
|
|
} |
161
|
|
|
|
162
|
27 |
|
public function toHtml() |
163
|
|
|
{ |
164
|
27 |
|
if ($this->isEmpty()) { |
165
|
15 |
|
return ''; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return ' ' . implode(' ', array_map(function ($attribute, $value) { |
169
|
12 |
|
if (is_null($value) || $value === '') { |
170
|
6 |
|
return $attribute; |
171
|
|
|
} |
172
|
|
|
|
173
|
9 |
|
return "{$attribute}=\"{$value}\""; |
174
|
12 |
|
}, array_keys($this->toArray()), $this->toArray())); |
175
|
|
|
} |
176
|
|
|
|
177
|
24 |
|
public function __toString() |
178
|
|
|
{ |
179
|
24 |
|
return $this->toHtml(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.