1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\HtmlElement; |
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
|
|
|
|
27
|
|
|
if ($attribute === 'class') { |
28
|
|
|
$this->addClass($value); |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (is_int($attribute)) { |
33
|
|
|
$attribute = $value; |
34
|
|
|
$value = ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->setAttribute($attribute, $value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $attribute |
45
|
|
|
* @param string $value |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setAttribute(string $attribute, string $value = '') |
50
|
|
|
{ |
51
|
|
|
if ($attribute === 'class') { |
52
|
|
|
$this->addClass($value); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->attributes[$attribute] = $value; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string|array $class |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function addClass($class) |
68
|
|
|
{ |
69
|
|
|
if (!is_array($class)) { |
70
|
|
|
$class = [$class]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->classes = array_unique( |
74
|
|
|
array_merge($this->classes, $class) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isEmpty() : bool |
81
|
|
|
{ |
82
|
|
|
return empty($this->attributes) && empty($this->classes); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function toArray() : array |
86
|
|
|
{ |
87
|
|
|
if (empty($this->classes)) { |
88
|
|
|
return $this->attributes; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function toString() : string |
95
|
|
|
{ |
96
|
|
|
if ($this->isEmpty()) { |
97
|
|
|
return ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$attributeStrings = []; |
101
|
|
|
|
102
|
|
|
foreach ($this->toArray() as $attribute => $value) { |
103
|
|
|
if (empty($value)) { |
104
|
|
|
$attributeStrings[] = $attribute; |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$attributeStrings[] = "{$attribute}=\"{$value}\""; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return implode(' ', $attributeStrings); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __toString() : string |
115
|
|
|
{ |
116
|
|
|
return $this->toString(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|