Element.php
Go to the documentation of this file.
1<?php
11
13
20abstract class Element
21{
25 protected $name;
26
30 public $valid;
31
35 protected $validated = false;
36
40 protected $log;
41
45 protected $defaults = [];
46
56 protected $disabled;
57
68 public $dataAttr;
69
79 protected $class;
80
89 public function __construct($name, $parameters, $form)
90 {
91 $this->checkName($name);
92 $this->checkParameters($parameters);
93
94 $this->name = $name;
95
96 $this->setDefaults();
97 $parameters = array_change_key_case($parameters);
98 foreach ($this->defaults as $parameter => $default) {
99 $this->$parameter = isset($parameters[strtolower($parameter)]) ? $parameters[strtolower($parameter)] : $default;
100 }
101 }
102
112 protected function setDefaults()
113 {
114 $this->defaults['log'] = null;
115 $this->defaults['class'] = null;
116 $this->defaults['dataAttr'] = [];
117 $this->defaults['disabled'] = false;
118 }
119
132 public function __call($function, $arguments)
133 {
134 if (substr($function, 0, 4) === 'html') {
135 $attribute = str_replace('html', '', $function);
136 $attribute[0] = strtolower($attribute[0]);
137
138 $escapedAttribute = $attribute . "Html";
139
140 if (!empty($this->$escapedAttribute)) {
141 return $this->$escapedAttribute;
142 }
143
144 return $this->htmlEscape($this->$attribute);
145 } else {
146 trigger_error("Call to undefined method $function", E_USER_ERROR);
147 }
148 }
149
156 public function setDisabled($disabled = true)
157 {
158 $this->disabled = (bool) $disabled;
159
160 return $this;
161 }
162
169 public function getDisabled()
170 {
171 return $this->disabled;
172 }
173
183 public function clearValue()
184 {
185 }
186
192 public function getName()
193 {
194 return $this->name;
195 }
196
205 protected function checkParameters($parameters)
206 {
207 if ((isset($parameters)) && (!is_array($parameters))) {
208 throw new Exceptions\ElementParametersNoArrayException('Element "' . $this->getName() . '": parameters must be of type array.');
209 }
210 }
211
221 private function checkName($name)
222 {
223 if (
224 !is_string($name)
225 || trim($name) === ''
226 || preg_match('/[^a-zA-Z0-9_\-\[\]]/', $name)
227 ) {
228 throw new Exceptions\InvalidElementNameException('"' . $name . '" is not a valid element name.');
229 }
230 }
231
242 protected function log($argument, $type = null)
243 {
244 if (is_callable(array($this->log, 'log'))) {
245 $this->log->log($argument, $type);
246 } else {
247 if (gettype($argument) != 'string') {
248 ob_start();
249 print_r($argument);
250 $message = ob_get_contents();
251 ob_end_clean();
252 } else {
253 $message = $argument;
254 }
255 error_log($message);
256 }
257 }
258
267 protected function htmlEscape($options = array())
268 {
269 if (is_string($options)) {
270 $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
271 } elseif (is_array($options)) {
272 $htmlOptions = array();
273
274 foreach ($options as $index => $option) {
275 if (is_string($index)) $index = htmlspecialchars($index, ENT_QUOTES);
276 if (is_string($option)) $option = htmlspecialchars($option, ENT_QUOTES);
277
278 $htmlOptions[$index] = $option;
279 }
280 } else {
281 $htmlOptions = $options;
282 }
283
284 return $htmlOptions;
285 }
286
290 protected function htmlDataAttributes()
291 {
292 $attributes = "";
293 if (is_array($this->dataAttr)) {
294 foreach ($this->dataAttr as $key => $val) {
295 // @todo throw error when key is not plain string?
296 $attributes .= " data-$key=\"" . $this->htmlEscape($val) . "\"";
297 }
298 }
299
300 return $attributes;
301 }
302}
303
304/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
behold: the über-class
Definition Element.php:21
$valid
Contains element validation status/result.
Definition Element.php:30
setDisabled($disabled=true)
Sets the HTML disabled-attribute of the current input element.
Definition Element.php:156
$class
CSS class of the container element.
Definition Element.php:79
$defaults
holds default values for element attributes
Definition Element.php:45
__construct($name, $parameters, $form)
element class constructor
Definition Element.php:89
getName()
Returns the element name.
Definition Element.php:192
clearValue()
resets the value to null
Definition Element.php:183
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Element.php:290
$disabled
wether a input element will be disabled
Definition Element.php:56
__call($function, $arguments)
HTML escaping.
Definition Element.php:132
log($argument, $type=null)
error & warning logger
Definition Element.php:242
$log
Log object reference.
Definition Element.php:40
checkParameters($parameters)
checks element parameters
Definition Element.php:205
$validated
True if the element has been validated before.
Definition Element.php:35
setDefaults()
Collects initial values across subclasses.
Definition Element.php:112
getDisabled()
Gets if input is currently disabled.
Definition Element.php:169
$dataAttr
Extra information about the data that is saved inside the element.
Definition Element.php:68
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
thrown when element parameters aren't of type array on construction
thrown when element name is empty or contains invalid characters
Abstract element classes.
Definition Container.php:10