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 $name |
38
|
|
|
* @param $arguments |
39
|
|
|
* @return $this|null |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
14 |
|
public function __call($name, $arguments) { |
43
|
14 |
|
if (strpos($name, 'set') === 0 and isset($arguments[0])) { |
44
|
14 |
|
$this->attributes[strtolower(substr($name, 3))] = $arguments[0]; |
45
|
14 |
|
return $this; |
46
|
12 |
|
} elseif (strpos($name, 'get') === 0 and !isset($arguments[0])) { |
47
|
12 |
|
$key = strtolower(substr($name, 3)); |
48
|
12 |
|
return isset($this->attributes[$key]) ? $this->attributes[$key] : null; |
49
|
|
|
} else { |
50
|
1 |
|
throw new \Exception('Invalid method: ' . $name); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $className |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
1 |
|
public function addClass($className) { |
60
|
1 |
|
if (empty($this->attributes['class'])) { |
61
|
1 |
|
$this->attributes['class'] = $className; |
62
|
|
|
} else { |
63
|
1 |
|
$this->attributes['class'] = $this->attributes['class'] . ' ' . $className; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $attributes |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
4 |
|
public static function renderAttributes($attributes = array()) { |
75
|
4 |
|
$attributesInline = ''; |
76
|
4 |
|
foreach ($attributes as $name => $value) { |
77
|
3 |
|
$attributesInline .= $name . '="' . addslashes($value) . '" '; |
78
|
|
|
} |
79
|
4 |
|
return $attributesInline; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
* @param $tag |
86
|
|
|
* @param $attributes |
87
|
|
|
* @param bool $content |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public static function tag($tag, $attributes, $content = null) { |
91
|
2 |
|
$html = '<' . $tag . ' ' . static::renderAttributes($attributes); |
92
|
|
|
|
93
|
2 |
|
if ($content !== null) { |
94
|
|
|
$html .= '>' . $content . '</' . $tag . '>'; |
95
|
|
|
} else { |
96
|
2 |
|
$html .= ' />'; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return $html; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $attributes |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
1 |
|
public function setAttributes(array $attributes) { |
108
|
1 |
|
$this->attributes = $attributes; |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add multiple attributes |
115
|
|
|
* |
116
|
|
|
* @param array $attributes |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function addAttributes(array $attributes) { |
120
|
|
|
foreach ($attributes as $name => $value) { |
121
|
|
|
$this->attributes[$name] = $value; |
122
|
|
|
} |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $name |
129
|
|
|
* @param string $value |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
9 |
|
public function setAttribute($name, $value) { |
133
|
9 |
|
$this->attributes[$name] = $value; |
134
|
9 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $name |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
1 |
|
public function removeAttribute($name) { |
143
|
1 |
|
unset($this->attributes[$name]); |
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name Attribute name |
150
|
|
|
* @return string|null |
151
|
|
|
*/ |
152
|
2 |
|
public function getAttribute($name) { |
153
|
2 |
|
return !empty($this->attributes[$name]) ? $this->attributes[$name] : null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
1 |
|
public function getAttributesAsString() { |
161
|
1 |
|
return static::renderAttributes($this->attributes); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
1 |
|
public function render() { |
169
|
1 |
|
return static::tag($this->tag, $this->attributes, $this->getContent()); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return null|string |
175
|
|
|
*/ |
176
|
1 |
|
public function getContent() { |
177
|
1 |
|
return $this->content; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
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.