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
|
|
|
* @deprecated |
38
|
|
|
* @param $name |
39
|
|
|
* @param $arguments |
40
|
|
|
* @return $this|null |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public function __call($name, $arguments) { |
44
|
|
|
trigger_error('Deprecated', E_USER_DEPRECATED); |
45
|
|
|
if (strpos($name, 'set') === 0 and isset($arguments[0])) { |
46
|
|
|
$name = strtolower(substr($name, 3)); |
47
|
|
|
$this->setAttribute($name, $arguments[0]); |
48
|
|
|
return $this; |
49
|
|
|
} elseif (strpos($name, 'get') === 0 and !isset($arguments[0])) { |
50
|
|
|
return $this->getAttribute(strtolower(substr($name, 3))); |
51
|
|
|
} else { |
52
|
|
|
throw new \Exception('Invalid method: ' . $name); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $className |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
2 |
|
public function addClass($className) { |
62
|
2 |
|
$currentClass = $this->getAttribute('class'); |
63
|
|
|
|
64
|
2 |
|
if (!empty($currentClass)) { |
65
|
2 |
|
$className = $currentClass . ' ' . $className; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$this->setAttribute('class', $className); |
69
|
|
|
|
70
|
2 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $attributes |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
9 |
|
public static function renderAttributes(array $attributes = []) { |
79
|
9 |
|
$attributesInline = ''; |
80
|
9 |
|
foreach ($attributes as $name => $value) { |
81
|
8 |
|
$attributesInline .= $name . '="' . addslashes($value) . '" '; |
82
|
|
|
} |
83
|
9 |
|
return $attributesInline; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @param string $tag |
90
|
|
|
* @param array $attributes |
91
|
|
|
* @param bool $content |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
7 |
|
public static function tag($tag, array $attributes, $content = null) { |
95
|
7 |
|
$html = '<' . $tag . ' ' . static::renderAttributes($attributes); |
96
|
|
|
|
97
|
7 |
|
if ($content !== null) { |
98
|
|
|
$html .= '>' . $content . '</' . $tag . '>'; |
99
|
|
|
} else { |
100
|
7 |
|
$html .= ' />'; |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
return $html; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
5 |
|
public function getAttributes() { |
111
|
5 |
|
return $this->attributes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $attributes |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
5 |
|
public function setAttributes(array $attributes) { |
120
|
5 |
|
foreach ($attributes as $name => $value) { |
121
|
4 |
|
$this->setAttribute($name, $value); |
122
|
|
|
} |
123
|
5 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add multiple attributes |
129
|
|
|
* |
130
|
|
|
* @param array $attributes |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
3 |
|
public function addAttributes(array $attributes) { |
134
|
3 |
|
$this->setAttributes($attributes); |
135
|
3 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $name |
141
|
|
|
* @param string $value |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
27 |
|
public function setAttribute($name, $value) { |
145
|
27 |
|
$this->attributes[$name] = $value; |
146
|
27 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $name |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
3 |
|
public function removeAttribute($name) { |
155
|
3 |
|
unset($this->attributes[$name]); |
156
|
3 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $name Attribute name |
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
23 |
|
public function getAttribute($name) { |
165
|
23 |
|
return !empty($this->attributes[$name]) ? $this->attributes[$name] : null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getAttributesAsString() { |
173
|
|
|
trigger_error('Deprecated. use Html::renderAttributes', E_USER_DEPRECATED); |
174
|
|
|
return static::renderAttributes($this->attributes); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
6 |
|
public function render() { |
182
|
6 |
|
return static::tag($this->tag, $this->attributes, $this->getContent()); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return null|string |
188
|
|
|
*/ |
189
|
6 |
|
public function getContent() { |
190
|
6 |
|
return $this->content; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getTag() { |
198
|
|
|
return $this->tag; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $tag |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
1 |
|
public function setTag($tag) { |
207
|
1 |
|
if (!is_string($tag)) { |
208
|
|
|
throw new \InvalidArgumentException('Expect tag to be a valid string'); |
209
|
|
|
} |
210
|
1 |
|
$this->tag = $tag; |
211
|
1 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.