1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Menu\Html; |
4
|
|
|
|
5
|
|
|
class Attributes |
6
|
|
|
{ |
7
|
|
|
/** @var array */ |
8
|
|
|
protected $attributes = []; |
9
|
|
|
|
10
|
|
|
/** @var array */ |
11
|
|
|
protected $classes = []; |
12
|
|
|
|
13
|
|
|
public function __construct(array $attributes = []) |
14
|
|
|
{ |
15
|
|
|
$this->setAttributes($attributes); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $attributes |
20
|
|
|
* |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public function setAttributes(array $attributes) |
24
|
|
|
{ |
25
|
|
|
foreach ($attributes as $attribute => $value) { |
26
|
|
|
if ($attribute === 'class') { |
27
|
|
|
$this->addClass($value); |
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (is_int($attribute)) { |
32
|
|
|
$attribute = $value; |
33
|
|
|
$value = ''; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->setAttribute($attribute, $value); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $attribute |
44
|
|
|
* @param string $value |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setAttribute(string $attribute, string $value = '') |
49
|
|
|
{ |
50
|
|
|
if ($attribute === 'class') { |
51
|
|
|
$this->addClass($value); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->attributes[$attribute] = $value; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string|array $class |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function addClass($class) |
67
|
|
|
{ |
68
|
|
|
if (! is_array($class)) { |
69
|
|
|
$class = [$class]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->classes = array_unique( |
73
|
|
|
array_merge($this->classes, $class) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param \Spatie\Menu\Html\Attributes $attributes |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function mergeWith(self $attributes) |
85
|
|
|
{ |
86
|
|
|
$this->attributes = array_merge($this->attributes, $attributes->attributes); |
87
|
|
|
$this->classes = array_merge($this->classes, $attributes->classes); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function isEmpty(): bool |
93
|
|
|
{ |
94
|
|
|
return empty($this->attributes) && empty($this->classes); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function toArray(): array |
98
|
|
|
{ |
99
|
|
|
if (empty($this->classes)) { |
100
|
|
|
return $this->attributes; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function toString(): string |
107
|
|
|
{ |
108
|
|
|
if ($this->isEmpty()) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$attributeStrings = []; |
113
|
|
|
|
114
|
|
|
foreach ($this->toArray() as $attribute => $value) { |
115
|
|
|
if (is_null($value) || $value === '') { |
116
|
|
|
$attributeStrings[] = $attribute; |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$attributeStrings[] = "{$attribute}=\"{$value}\""; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return implode(' ', $attributeStrings); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function __toString(): string |
127
|
|
|
{ |
128
|
|
|
return $this->toString(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|