PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/framework/HtmlForm/Abstracts/Element.php

https://github.com/depage/depage-cms
PHP | 300 lines | 117 code | 27 blank | 156 comment | 18 complexity | a01e1608aebe5e80ea0d55b15e9677e2 MD5 | raw file
  1. <?php
  2. /**
  3. * @file element.php
  4. * @brief element class
  5. *
  6. * @author Frank Hellenkamp <jonas@depage.net>
  7. * @author Sebastian Reinhold <sebastian@bitbernd.de>
  8. **/
  9. namespace Depage\HtmlForm\Abstracts;
  10. use Depage\HtmlForm\Exceptions;
  11. /**
  12. * @brief behold: the über-class
  13. *
  14. * The abstract element class contains the basic attributes and tools of
  15. * container and input elements.
  16. **/
  17. abstract class Element
  18. {
  19. // {{{ variables
  20. /**
  21. * @brief Element name.
  22. **/
  23. protected $name;
  24. /**
  25. * @brief Contains element validation status/result.
  26. **/
  27. public $valid;
  28. /**
  29. * @brief True if the element has been validated before.
  30. **/
  31. protected $validated = false;
  32. /**
  33. * @brief Log object reference
  34. **/
  35. protected $log;
  36. /**
  37. * @addtogroup htmlformInputDefaults
  38. *
  39. * @default variant $dataAttr
  40. * Optional associated Array of values that will be added as data-Attribute
  41. * to the container element
  42. **/
  43. /**
  44. * @brief Extra information about the data that is saved inside the element.
  45. **/
  46. public $dataAttr;
  47. // }}}
  48. // {{{ __construct()
  49. /**
  50. * @brief element class constructor
  51. *
  52. * @param string $name element name
  53. * @param array $parameters element parameters, HTML attributes
  54. * @param object $form parent form object reference
  55. * @return void
  56. **/
  57. public function __construct($name, $parameters, $form)
  58. {
  59. $this->checkName($name);
  60. $this->checkParameters($parameters);
  61. $this->name = $name;
  62. $this->setDefaults();
  63. $parameters = array_change_key_case($parameters);
  64. foreach ($this->defaults as $parameter => $default) {
  65. $this->$parameter = isset($parameters[strtolower($parameter)]) ? $parameters[strtolower($parameter)] : $default;
  66. }
  67. }
  68. // }}}
  69. // {{{ setDefaults()
  70. /**
  71. * @brief Collects initial values across subclasses.
  72. *
  73. * The constructor loops through these and creates settable class
  74. * attributes at runtime. It's a compact mechanism for initialising
  75. * a lot of variables.
  76. *
  77. * @return void
  78. **/
  79. protected function setDefaults()
  80. {
  81. $this->defaults['log'] = null;
  82. $this->defaults['class'] = null;
  83. $this->defaults['dataAttr'] = [];
  84. $this->defaults['disabled'] = false;
  85. }
  86. // }}}
  87. // {{{ __call()
  88. /**
  89. * @brief HTML escaping
  90. *
  91. * Returns respective HTML escaped attributes for element rendering. Has to
  92. * be called before printing user-entered data.
  93. *
  94. * @param string $function function name
  95. * @param array $arguments function arguments
  96. * @return mixed HTML escaped value
  97. *
  98. * @see htmlEscape()
  99. **/
  100. public function __call($function, $arguments)
  101. {
  102. if (substr($function, 0, 4) === 'html') {
  103. $attribute = str_replace('html', '', $function);
  104. $attribute[0] = strtolower($attribute[0]);
  105. $escapedAttribute = $attribute . "Html";
  106. if (!empty($this->$escapedAttribute)) {
  107. return $this->$escapedAttribute;
  108. }
  109. return $this->htmlEscape($this->$attribute);
  110. } else {
  111. trigger_error("Call to undefined method $function", E_USER_ERROR);
  112. }
  113. }
  114. // }}}
  115. // {{{ setDisabled()
  116. /**
  117. * @brief Sets the HTML disabled-attribute of the current input element.
  118. *
  119. * @param bool $disabled HTML disabled-attribute
  120. * @return void
  121. **/
  122. public function setDisabled($disabled = true)
  123. {
  124. $this->disabled = (bool) $disabled;
  125. return $this;
  126. }
  127. // }}}
  128. // {{{ getDisabled()
  129. /**
  130. * @brief Gets if input is currently disabled
  131. *
  132. * @param bool $disabled HTML disabled-attribute
  133. * @return void
  134. **/
  135. public function getDisabled()
  136. {
  137. return $this->disabled;
  138. }
  139. // }}}
  140. // {{{ clearValue()
  141. /**
  142. * @brief resets the value to null
  143. *
  144. * This needs to be refined in the subclasses which really provide an input.
  145. * It is defined on this abstract class since clearSession calls it for every element in the form
  146. * even if there is no value.
  147. *
  148. * @return void
  149. **/
  150. public function clearValue()
  151. {
  152. }
  153. // }}}
  154. // {{{ getName()
  155. /**
  156. * @brief Returns the element name.
  157. *
  158. * @return string $this->name element name
  159. **/
  160. public function getName()
  161. {
  162. return $this->name;
  163. }
  164. // }}}
  165. // {{{ checkParameters()
  166. /**
  167. * @brief checks element parameters
  168. *
  169. * Throws an exception if $parameters isn't of type array.
  170. *
  171. * @param array $parameters parameters for element constructor
  172. * @return void
  173. **/
  174. protected function checkParameters($parameters)
  175. {
  176. if ((isset($parameters)) && (!is_array($parameters))) {
  177. throw new Exceptions\ElementParametersNoArrayException('Element "' . $this->getName() . '": parameters must be of type array.');
  178. }
  179. }
  180. // }}}
  181. // {{{ checkName()
  182. /**
  183. * @brief checks for valid element name
  184. *
  185. * Checks that element name is of type string, not empty and doesn't
  186. * contain invalid characters. Otherwise throws an exception.
  187. *
  188. * @param string $name element name
  189. * @return void
  190. **/
  191. private function checkName($name)
  192. {
  193. if (
  194. !is_string($name)
  195. || trim($name) === ''
  196. || preg_match('/[^a-zA-Z0-9_\-\[\]]/', $name)
  197. ) {
  198. throw new Exceptions\InvalidElementNameException('"' . $name . '" is not a valid element name.');
  199. }
  200. }
  201. // }}}
  202. // {{{ log()
  203. /**
  204. * @brief error & warning logger
  205. *
  206. * If the element is constructed with a custom log object the logging
  207. * happens there, otherwise the PHP error_log function is used.
  208. *
  209. * @param string $argument message
  210. * @param string $type type of log message
  211. * @return void
  212. **/
  213. protected function log($argument, $type = null)
  214. {
  215. if (is_callable(array($this->log, 'log'))) {
  216. $this->log->log($argument, $type);
  217. } else {
  218. if (gettype($argument) != 'string') {
  219. ob_start();
  220. print_r($argument);
  221. $message = ob_get_contents();
  222. ob_end_clean();
  223. } else {
  224. $message = $argument;
  225. }
  226. error_log($message);
  227. }
  228. }
  229. // }}}
  230. // {{{ htmlEscape()
  231. /**
  232. * @brief Escapes HTML in strings and arrays of strings
  233. *
  234. * @param mixed $options value to be HTML-escaped
  235. * @return mixed $htmlOptions HTML escaped value
  236. *
  237. * @see __call()
  238. **/
  239. protected function htmlEscape($options = array())
  240. {
  241. if (is_string($options)) {
  242. $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
  243. } elseif (is_array($options)) {
  244. $htmlOptions = array();
  245. foreach ($options as $index => $option) {
  246. if (is_string($index)) $index = htmlspecialchars($index, ENT_QUOTES);
  247. if (is_string($option)) $option = htmlspecialchars($option, ENT_QUOTES);
  248. $htmlOptions[$index] = $option;
  249. }
  250. } else {
  251. $htmlOptions = $options;
  252. }
  253. return $htmlOptions;
  254. }
  255. // }}}
  256. // {{{ htmlDataAttributes()
  257. /**
  258. * @brief Returns dataAttr escaped as attribute string
  259. **/
  260. protected function htmlDataAttributes()
  261. {
  262. $attributes = "";
  263. if (is_array($this->dataAttr)) {
  264. foreach ($this->dataAttr as $key => $val) {
  265. // @todo throw error when key is not plain string?
  266. $attributes .= " data-$key=\"" . $this->htmlEscape($val) . "\"";
  267. }
  268. }
  269. return $attributes;
  270. }
  271. // }}}
  272. }
  273. /* vim:set ft=php sw=4 sts=4 fdm=marker et : */