|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
* @author Ivan Shcherbak <[email protected]> |
|
8
|
|
|
* @package Fiv\Form\Html |
|
9
|
|
|
*/ |
|
10
|
|
|
class Html { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $tag = ''; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var null|string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $content = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $attributes = []; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function __toString() { |
|
32
|
2 |
|
return $this->render(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $className |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function addClass($className) { |
|
41
|
2 |
|
$currentClass = $this->getAttribute('class'); |
|
42
|
|
|
|
|
43
|
2 |
|
if (!empty($currentClass)) { |
|
44
|
2 |
|
$className = $currentClass . ' ' . $className; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
$this->setAttribute('class', $className); |
|
48
|
|
|
|
|
49
|
2 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $attributes |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
13 |
|
public static function renderAttributes(array $attributes = []) { |
|
58
|
13 |
|
$attributesInline = ''; |
|
59
|
13 |
|
foreach ($attributes as $name => $value) { |
|
60
|
12 |
|
$attributesInline .= $name . '="' . addslashes($value) . '" '; |
|
61
|
|
|
} |
|
62
|
13 |
|
return $attributesInline; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $tag |
|
69
|
|
|
* @param array $attributes |
|
70
|
|
|
* @param bool $content |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
10 |
|
public static function tag($tag, array $attributes, $content = null) { |
|
74
|
10 |
|
$html = '<' . $tag . ' ' . static::renderAttributes($attributes); |
|
75
|
|
|
|
|
76
|
10 |
|
if ($content !== null) { |
|
77
|
|
|
$html .= '>' . $content . '</' . $tag . '>'; |
|
78
|
|
|
} else { |
|
79
|
10 |
|
$html .= ' />'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
10 |
|
return $html; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function getAttributes() { |
|
90
|
6 |
|
return $this->attributes; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $attributes |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
5 |
|
public function setAttributes(array $attributes) { |
|
99
|
5 |
|
foreach ($attributes as $name => $value) { |
|
100
|
4 |
|
$this->setAttribute($name, $value); |
|
101
|
|
|
} |
|
102
|
5 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add multiple attributes |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $attributes |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function addAttributes(array $attributes) { |
|
113
|
3 |
|
$this->setAttributes($attributes); |
|
114
|
3 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
* @param string $value |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
47 |
|
public function setAttribute($name, $value) { |
|
124
|
47 |
|
$this->attributes[$name] = $value; |
|
125
|
47 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $name |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
8 |
|
public function removeAttribute($name) { |
|
134
|
8 |
|
unset($this->attributes[$name]); |
|
135
|
8 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $name Attribute name |
|
141
|
|
|
* @return string|null |
|
142
|
|
|
*/ |
|
143
|
40 |
|
public function getAttribute($name) { |
|
144
|
40 |
|
return !empty($this->attributes[$name]) ? $this->attributes[$name] : null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
7 |
|
public function render() { |
|
152
|
7 |
|
return static::tag($this->tag, $this->attributes, $this->getContent()); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return null|string |
|
158
|
|
|
*/ |
|
159
|
7 |
|
public function getContent() { |
|
160
|
7 |
|
return $this->content; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getTag() { |
|
168
|
|
|
return $this->tag; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $tag |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
1 |
|
public function setTag($tag) { |
|
177
|
1 |
|
if (!is_string($tag)) { |
|
178
|
|
|
throw new \InvalidArgumentException('Expect tag to be a valid string'); |
|
179
|
|
|
} |
|
180
|
1 |
|
$this->tag = $tag; |
|
181
|
1 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |