-
Notifications
You must be signed in to change notification settings - Fork 811
Expand file tree
/
Copy pathAttributesHTML.php
More file actions
138 lines (114 loc) · 3.37 KB
/
Copy pathAttributesHTML.php
File metadata and controls
138 lines (114 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace SilverStripe\View;
use SilverStripe\Core\Convert;
/**
* This trait can be applied to a ViewableData class to add the logic to render attributes in an SS template.
*
* When applying this trait to a class, you also need to add the following casting configuration.
* ```
* private static $casting = [
* 'AttributesHTML' => 'HTMLFragment',
* 'getAttributesHTML' => 'HTMLFragment',
* ];
* ```
*/
trait AttributesHTML
{
/**
* List of attributes to render on the frontend
* @var array
*/
protected $attributes = [];
/**
* Set an HTML attribute
* @param $name
* @param $value
* @return $this
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
* Retrieve the value of an HTML attribute
* @param string $name
* @return mixed|null
*/
public function getAttribute($name)
{
$attributes = $this->getAttributes();
if (isset($attributes[$name])) {
return $attributes[$name];
}
return null;
}
/**
* Get the default attributes when rendering this object.
*
* Called by `getAttributes()`
*
* @return array
*/
abstract protected function getDefaultAttributes(): array;
/**
* Allows customization through an 'updateAttributes' hook on the base class.
* Existing attributes are passed in as the first argument and can be manipulated,
* but any attributes added through a subclass implementation won't be included.
*
* @return array
*/
public function getAttributes()
{
$defaultAttributes = $this->getDefaultAttributes();
$attributes = array_merge($defaultAttributes, $this->attributes);
if (method_exists($this, 'extend')) {
$this->extend('updateAttributes', $attributes);
}
return $attributes;
}
/**
* Custom attributes to process. Falls back to {@link getAttributes()}.
*
* If at least one argument is passed as a string, all arguments act as excludes, by name.
*
* @param array $attributes
*
* @return string
*/
public function getAttributesHTML($attributes = null)
{
$exclude = null;
if (is_string($attributes)) {
$exclude = func_get_args();
}
if (!$attributes || is_string($attributes)) {
$attributes = $this->getAttributes();
}
$attributes = (array) $attributes;
$attributes = array_filter($attributes ?? [], function ($v, $k) {
return ($k === 'alt' || $v || $v === 0 || $v === '0');
}, ARRAY_FILTER_USE_BOTH);
if ($exclude) {
$attributes = array_diff_key(
$attributes ?? [],
array_flip($exclude ?? [])
);
}
// Create markup
$parts = [];
foreach ($attributes as $name => $value) {
if ($value === true) {
$value = $name;
} else {
if (is_scalar($value)) {
$value = (string) $value;
} else {
$value = json_encode($value);
}
}
$parts[] = sprintf('%s="%s"', Convert::raw2att($name), Convert::raw2att($value));
}
return implode(' ', $parts);
}
}