PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/widget/sfWidget.class.php

http://openirudi.googlecode.com/
PHP | 350 lines | 131 code | 36 blank | 183 comment | 7 complexity | 7021981107a1d8c7d490f9ca08ac1f88 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidget is the base class for all widgets.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidget.class.php 10985 2008-08-20 07:26:11Z dwhittle $
  16. */
  17. abstract class sfWidget
  18. {
  19. protected
  20. $requiredOptions = array(),
  21. $attributes = array(),
  22. $options = array();
  23. protected static
  24. $xhtml = true,
  25. $charset = 'UTF-8';
  26. /**
  27. * Constructor.
  28. *
  29. * @param array $options An array of options
  30. * @param array $attributes An array of default HTML attributes
  31. */
  32. public function __construct($options = array(), $attributes = array())
  33. {
  34. $this->configure($options, $attributes);
  35. // check option names
  36. if ($diff = array_diff(array_keys($options), array_merge(array_keys($this->options), $this->requiredOptions)))
  37. {
  38. throw new InvalidArgumentException(sprintf('%s does not support the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  39. }
  40. // check required options
  41. if ($diff = array_diff($this->requiredOptions, array_merge(array_keys($this->options), array_keys($options))))
  42. {
  43. throw new RuntimeException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  44. }
  45. $this->options = array_merge($this->options, $options);
  46. $this->attributes = array_merge($this->attributes, $attributes);
  47. }
  48. /**
  49. * Configures the current widget.
  50. *
  51. * This method allows each widget to add options or HTML attributes
  52. * during widget creation.
  53. *
  54. * If some options and HTML attributes are given in the sfWidget constructor
  55. * they will take precedence over the options and HTML attributes you configure
  56. * in this method.
  57. *
  58. * @param array $options An array of options
  59. * @param array $attributes An array of HTML attributes
  60. *
  61. * @see __construct()
  62. */
  63. protected function configure($options = array(), $attributes = array())
  64. {
  65. }
  66. /**
  67. * Renders the widget as HTML.
  68. *
  69. * All subclasses must implement this method.
  70. *
  71. * @param string $name The name of the HTML widget
  72. * @param mixed $value The value of the widget
  73. * @param array $attributes An array of HTML attributes
  74. * @param array $errors An array of errors
  75. *
  76. * @return string A HTML representation of the widget
  77. */
  78. abstract public function render($name, $value = null, $attributes = array(), $errors = array());
  79. /**
  80. * Adds a required option.
  81. *
  82. * @param string $name The option name
  83. */
  84. public function addRequiredOption($name)
  85. {
  86. $this->requiredOptions[] = $name;
  87. }
  88. /**
  89. * Returns all required option names.
  90. *
  91. * @param array An array of required option names
  92. */
  93. public function getRequiredOptions()
  94. {
  95. return $this->requiredOptions;
  96. }
  97. /**
  98. * Adds a new option value with a default value.
  99. *
  100. * @param string $name The option name
  101. * @param mixed $value The default value
  102. */
  103. public function addOption($name, $value = null)
  104. {
  105. $this->options[$name] = $value;
  106. }
  107. /**
  108. * Changes an option value.
  109. *
  110. * @param string $name The option name
  111. * @param mixed $value The value
  112. */
  113. public function setOption($name, $value)
  114. {
  115. if (!in_array($name, array_merge(array_keys($this->options), $this->requiredOptions)))
  116. {
  117. throw new InvalidArgumentException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name));
  118. }
  119. $this->options[$name] = $value;
  120. }
  121. /**
  122. * Gets an option value.
  123. *
  124. * @param string The option name
  125. *
  126. * @return mixed The option value
  127. */
  128. public function getOption($name)
  129. {
  130. return isset($this->options[$name]) ? $this->options[$name] : null;
  131. }
  132. /**
  133. * Returns true if the option exists.
  134. *
  135. * @param string $name The option name
  136. *
  137. * @return bool true if the option exists, false otherwise
  138. */
  139. public function hasOption($name)
  140. {
  141. return array_key_exists($name, $this->options);
  142. }
  143. /**
  144. * Gets all options.
  145. *
  146. * @return array An array of named options
  147. */
  148. public function getOptions()
  149. {
  150. return $this->options;
  151. }
  152. /**
  153. * Sets the options.
  154. *
  155. * @param array $options An array of options
  156. */
  157. public function setOptions($options)
  158. {
  159. $this->options = $options;
  160. }
  161. /**
  162. * Returns the default HTML attributes.
  163. *
  164. * @param array An array of HTML attributes
  165. */
  166. public function getAttributes()
  167. {
  168. return $this->attributes;
  169. }
  170. /**
  171. * Sets a default HTML attribute.
  172. *
  173. * @param string $name The attribute name
  174. * @param string $value The attribute value
  175. */
  176. public function setAttribute($name, $value)
  177. {
  178. $this->attributes[$name] = $value;
  179. }
  180. /**
  181. * Returns the HTML attribute value for a given attribute name.
  182. *
  183. * @param string $name The attribute name.
  184. *
  185. * @return string The attribute value, or null if the attribute does not exist
  186. */
  187. public function getAttribute($name)
  188. {
  189. return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
  190. }
  191. /**
  192. * Sets the HTML attributes.
  193. *
  194. * @param array $attributes An array of HTML attributes
  195. */
  196. public function setAttributes($attributes)
  197. {
  198. $this->attributes = $attributes;
  199. }
  200. /**
  201. * Sets the charset to use when rendering widgets.
  202. *
  203. * @param string $charset The charset
  204. */
  205. static public function setCharset($charset)
  206. {
  207. self::$charset = $charset;
  208. }
  209. /**
  210. * Returns the charset to use when rendering widgets.
  211. *
  212. * @return string The charset (defaults to UTF-8)
  213. */
  214. static public function getCharset()
  215. {
  216. return self::$charset;
  217. }
  218. /**
  219. * Sets the XHTML generation flag.
  220. *
  221. * @param bool $boolean true if widgets must be generated as XHTML, false otherwise
  222. */
  223. static public function setXhtml($boolean)
  224. {
  225. self::$xhtml = (boolean) $boolean;
  226. }
  227. /**
  228. * Returns whether to generate XHTML tags or not.
  229. *
  230. * @return bool true if widgets must be generated as XHTML, false otherwise
  231. */
  232. static public function isXhtml()
  233. {
  234. return self::$xhtml;
  235. }
  236. /**
  237. * Renders a HTML tag.
  238. *
  239. * @param string $tag The tag name
  240. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  241. *
  242. * @param string An HTML tag string
  243. */
  244. public function renderTag($tag, $attributes = array())
  245. {
  246. if (empty($tag))
  247. {
  248. return '';
  249. }
  250. return sprintf('<%s%s%s', $tag, $this->attributesToHtml($attributes), self::$xhtml ? ' />' : ('input' != $tag ? sprintf('></%s>', $tag) : '>'));
  251. }
  252. /**
  253. * Renders a HTML content tag.
  254. *
  255. * @param string $tag The tag name
  256. * @param string $content The content of the tag
  257. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  258. *
  259. * @param string An HTML tag string
  260. */
  261. public function renderContentTag($tag, $content = null, $attributes = array())
  262. {
  263. if (empty($tag))
  264. {
  265. return '';
  266. }
  267. return sprintf('<%s%s>%s</%s>', $tag, $this->attributesToHtml($attributes), $content, $tag);
  268. }
  269. /**
  270. * Escapes a string.
  271. *
  272. * @param string $value string to escape
  273. * @return string escaped string
  274. */
  275. static public function escapeOnce($value)
  276. {
  277. $value = is_object($value) ? $value->__toString() : (string) $value;
  278. return self::fixDoubleEscape(htmlspecialchars($value, ENT_QUOTES, self::getCharset()));
  279. }
  280. /**
  281. * Fixes double escaped strings.
  282. *
  283. * @param string $escaped string to fix
  284. * @return string single escaped string
  285. */
  286. static public function fixDoubleEscape($escaped)
  287. {
  288. return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
  289. }
  290. /**
  291. * Converts an array of attributes to its HTML representation.
  292. *
  293. * @param array $attributes An array of attributes
  294. *
  295. * @return string The HTML representation of the HTML attribute array.
  296. */
  297. public function attributesToHtml($attributes)
  298. {
  299. $attributes = array_merge($this->attributes, $attributes);
  300. return implode('', array_map(array($this, 'attributesToHtmlCallback'), array_keys($attributes), array_values($attributes)));
  301. }
  302. /**
  303. * Prepares an attribute key and value for HTML representation.
  304. *
  305. * @param string $k The attribute key
  306. * @param string $v The attribute value
  307. *
  308. * @return string The HTML representation of the HTML key attribute pair.
  309. */
  310. protected function attributesToHtmlCallback($k, $v)
  311. {
  312. return is_null($v) || '' === $v ? '' : sprintf(' %s="%s"', $k, $this->escapeOnce($v));
  313. }
  314. }