element.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\abstracts;
11 
12 use depage\htmlform\exceptions;
13 
20 abstract class element
21 {
25  protected $name;
29  public $valid;
33  protected $validated = false;
37  protected $log;
38 
47  public function __construct($name, $parameters, $form)
48  {
49  $this->checkName($name);
50  $this->checkParameters($parameters);
51 
52  $this->name = $name;
53 
54  $this->setDefaults();
55  $parameters = array_change_key_case($parameters);
56  foreach ($this->defaults as $parameter => $default) {
57  $this->$parameter = isset($parameters[strtolower($parameter)]) ? $parameters[strtolower($parameter)] : $default;
58  }
59  }
60 
70  protected function setDefaults()
71  {
72  $this->defaults['log'] = null;
73  $this->defaults['class'] = null;
74  }
75 
88  public function __call($function, $arguments)
89  {
90  if (substr($function, 0, 4) === 'html') {
91  $attribute = str_replace('html', '', $function);
92  $attribute{0} = strtolower($attribute{0});
93 
94  return $this->htmlEscape($this->$attribute);
95  } else {
96  trigger_error("Call to undefined method $function", E_USER_ERROR);
97  }
98  }
99 
109  public function clearValue()
110  {
111  }
112 
118  public function getName()
119  {
120  return $this->name;
121  }
122 
131  protected function checkParameters($parameters)
132  {
133  if ((isset($parameters)) && (!is_array($parameters))) {
134  throw new exceptions\elementParametersNoArrayException('Element "' . $this->getName() . '": parameters must be of type array.');
135  }
136  }
137 
147  private function checkName($name)
148  {
149  if (
150  !is_string($name)
151  || trim($name) === ''
152  || preg_match('/[^a-zA-Z0-9_\-\[\]]/', $name)
153  ) {
154  throw new exceptions\invalidElementNameException('"' . $name . '" is not a valid element name.');
155  }
156  }
157 
168  protected function log($argument, $type = null)
169  {
170  if (is_callable(array($this->log, 'log'))) {
171  $this->log->log($argument, $type);
172  } else {
173  if (gettype($argument) != 'string') {
174  ob_start();
175  print_r($argument);
176  $message = ob_get_contents();
177  ob_end_clean();
178  } else {
179  $message = $argument;
180  }
181  error_log($message);
182  }
183  }
184 
193  protected function htmlEscape($options = array())
194  {
195  if (is_string($options)) {
196  $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
197  } elseif (is_array($options)) {
198  $htmlOptions = array();
199 
200  foreach ($options as $index => $option) {
201  if (is_string($index)) $index = htmlspecialchars($index, ENT_QUOTES);
202  if (is_string($option)) $option = htmlspecialchars($option, ENT_QUOTES);
203 
204  $htmlOptions[$index] = $option;
205  }
206  } else {
207  $htmlOptions = $options;
208  }
209 
210  return $htmlOptions;
211  }
212 }
213 
214 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
thrown when element parameters aren&#39;t of type array on construction
thrown when element name is empty or contains invalid characters
getName()
Returns the element name.
Definition: element.php:118
$log
Log object reference.
Definition: element.php:37
$validated
True if the element has been validated before.
Definition: element.php:33
log($argument, $type=null)
error &amp; warning logger
Definition: element.php:168
$valid
Contains element validation status/result.
Definition: element.php:29
clearValue()
resets the value to null
Definition: element.php:109
behold: the ├╝ber-class
Definition: element.php:20
setDefaults()
Collects initial values across subclasses.
Definition: element.php:70
__construct($name, $parameters, $form)
element class constructor
Definition: element.php:47
checkParameters($parameters)
checks element parameters
Definition: element.php:131
__call($function, $arguments)
HTML escaping.
Definition: element.php:88