PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/widget/sfWidget.class.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 406 lines | 146 code | 44 blank | 216 comment | 10 complexity | e0fbdf61b5fa295c5860c60decaee38b MD5 | raw file
  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 22933 2009-10-11 22:42:56Z Kris.Wallsmith $
  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. * @throws InvalidArgumentException when a option is not supported
  33. * @throws RuntimeException when a required option is not given
  34. */
  35. public function __construct($options = array(), $attributes = array())
  36. {
  37. $this->configure($options, $attributes);
  38. $currentOptionKeys = array_keys($this->options);
  39. $optionKeys = array_keys($options);
  40. // check option names
  41. if ($diff = array_diff($optionKeys, array_merge($currentOptionKeys, $this->requiredOptions)))
  42. {
  43. throw new InvalidArgumentException(sprintf('%s does not support the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  44. }
  45. // check required options
  46. if ($diff = array_diff($this->requiredOptions, array_merge($currentOptionKeys, $optionKeys)))
  47. {
  48. throw new RuntimeException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff)));
  49. }
  50. $this->options = array_merge($this->options, $options);
  51. $this->attributes = array_merge($this->attributes, $attributes);
  52. }
  53. /**
  54. * Configures the current widget.
  55. *
  56. * This method allows each widget to add options or HTML attributes
  57. * during widget creation.
  58. *
  59. * If some options and HTML attributes are given in the sfWidget constructor
  60. * they will take precedence over the options and HTML attributes you configure
  61. * in this method.
  62. *
  63. * @param array $options An array of options
  64. * @param array $attributes An array of HTML attributes
  65. *
  66. * @see __construct()
  67. */
  68. protected function configure($options = array(), $attributes = array())
  69. {
  70. }
  71. /**
  72. * Renders the widget as HTML.
  73. *
  74. * All subclasses must implement this method.
  75. *
  76. * @param string $name The name of the HTML widget
  77. * @param mixed $value The value of the widget
  78. * @param array $attributes An array of HTML attributes
  79. * @param array $errors An array of errors
  80. *
  81. * @return string A HTML representation of the widget
  82. */
  83. abstract public function render($name, $value = null, $attributes = array(), $errors = array());
  84. /**
  85. * Adds a required option.
  86. *
  87. * @param string $name The option name
  88. *
  89. * @return sfWidget The current widget instance
  90. */
  91. public function addRequiredOption($name)
  92. {
  93. $this->requiredOptions[] = $name;
  94. return $this;
  95. }
  96. /**
  97. * Returns all required option names.
  98. *
  99. * @return array An array of required option names
  100. */
  101. public function getRequiredOptions()
  102. {
  103. return $this->requiredOptions;
  104. }
  105. /**
  106. * Adds a new option value with a default value.
  107. *
  108. * @param string $name The option name
  109. * @param mixed $value The default value
  110. *
  111. * @return sfWidget The current widget instance
  112. */
  113. public function addOption($name, $value = null)
  114. {
  115. $this->options[$name] = $value;
  116. return $this;
  117. }
  118. /**
  119. * Changes an option value.
  120. *
  121. * @param string $name The option name
  122. * @param mixed $value The value
  123. *
  124. * @return sfWidget The current widget instance
  125. *
  126. * @throws InvalidArgumentException when a option is not supported
  127. */
  128. public function setOption($name, $value)
  129. {
  130. if (!in_array($name, array_merge(array_keys($this->options), $this->requiredOptions)))
  131. {
  132. throw new InvalidArgumentException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name));
  133. }
  134. $this->options[$name] = $value;
  135. return $this;
  136. }
  137. /**
  138. * Gets an option value.
  139. *
  140. * @param string $name The option name
  141. *
  142. * @return mixed The option value
  143. */
  144. public function getOption($name)
  145. {
  146. return isset($this->options[$name]) ? $this->options[$name] : null;
  147. }
  148. /**
  149. * Returns true if the option exists.
  150. *
  151. * @param string $name The option name
  152. *
  153. * @return bool true if the option exists, false otherwise
  154. */
  155. public function hasOption($name)
  156. {
  157. return array_key_exists($name, $this->options);
  158. }
  159. /**
  160. * Gets all options.
  161. *
  162. * @return array An array of named options
  163. */
  164. public function getOptions()
  165. {
  166. return $this->options;
  167. }
  168. /**
  169. * Sets the options.
  170. *
  171. * @param array $options An array of options
  172. *
  173. * @return sfWidget The current widget instance
  174. */
  175. public function setOptions($options)
  176. {
  177. $this->options = $options;
  178. return $this;
  179. }
  180. /**
  181. * Returns the default HTML attributes.
  182. *
  183. * @param array An array of HTML attributes
  184. */
  185. public function getAttributes()
  186. {
  187. return $this->attributes;
  188. }
  189. /**
  190. * Sets a default HTML attribute.
  191. *
  192. * @param string $name The attribute name
  193. * @param string $value The attribute value
  194. *
  195. * @return sfWidget The current widget instance
  196. */
  197. public function setAttribute($name, $value)
  198. {
  199. $this->attributes[$name] = $value;
  200. return $this;
  201. }
  202. /**
  203. * Returns the HTML attribute value for a given attribute name.
  204. *
  205. * @param string $name The attribute name.
  206. *
  207. * @return string The attribute value, or null if the attribute does not exist
  208. */
  209. public function getAttribute($name)
  210. {
  211. return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
  212. }
  213. /**
  214. * Sets the HTML attributes.
  215. *
  216. * @param array $attributes An array of HTML attributes
  217. *
  218. * @return sfWidget The current widget instance
  219. */
  220. public function setAttributes($attributes)
  221. {
  222. $this->attributes = $attributes;
  223. return $this;
  224. }
  225. /**
  226. * Gets the stylesheet paths associated with the widget.
  227. *
  228. * The array keys are files and values are the media names (separated by a ,):
  229. *
  230. * array('/path/to/file.css' => 'all', '/another/file.css' => 'screen,print')
  231. *
  232. * @return array An array of stylesheet paths
  233. */
  234. public function getStylesheets()
  235. {
  236. return array();
  237. }
  238. /**
  239. * Gets the JavaScript paths associated with the widget.
  240. *
  241. * @return array An array of JavaScript paths
  242. */
  243. public function getJavaScripts()
  244. {
  245. return array();
  246. }
  247. /**
  248. * Sets the charset to use when rendering widgets.
  249. *
  250. * @param string $charset The charset
  251. */
  252. static public function setCharset($charset)
  253. {
  254. self::$charset = $charset;
  255. }
  256. /**
  257. * Returns the charset to use when rendering widgets.
  258. *
  259. * @return string The charset (defaults to UTF-8)
  260. */
  261. static public function getCharset()
  262. {
  263. return self::$charset;
  264. }
  265. /**
  266. * Sets the XHTML generation flag.
  267. *
  268. * @param bool $boolean true if widgets must be generated as XHTML, false otherwise
  269. */
  270. static public function setXhtml($boolean)
  271. {
  272. self::$xhtml = (boolean) $boolean;
  273. }
  274. /**
  275. * Returns whether to generate XHTML tags or not.
  276. *
  277. * @return bool true if widgets must be generated as XHTML, false otherwise
  278. */
  279. static public function isXhtml()
  280. {
  281. return self::$xhtml;
  282. }
  283. /**
  284. * Renders a HTML tag.
  285. *
  286. * @param string $tag The tag name
  287. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  288. *
  289. * @param string An HTML tag string
  290. */
  291. public function renderTag($tag, $attributes = array())
  292. {
  293. if (empty($tag))
  294. {
  295. return '';
  296. }
  297. return sprintf('<%s%s%s', $tag, $this->attributesToHtml($attributes), self::$xhtml ? ' />' : (strtolower($tag) == 'input' ? '>' : sprintf('></%s>', $tag)));
  298. }
  299. /**
  300. * Renders a HTML content tag.
  301. *
  302. * @param string $tag The tag name
  303. * @param string $content The content of the tag
  304. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  305. *
  306. * @param string An HTML tag string
  307. */
  308. public function renderContentTag($tag, $content = null, $attributes = array())
  309. {
  310. if (empty($tag))
  311. {
  312. return '';
  313. }
  314. return sprintf('<%s%s>%s</%s>', $tag, $this->attributesToHtml($attributes), $content, $tag);
  315. }
  316. /**
  317. * Escapes a string.
  318. *
  319. * @param string $value string to escape
  320. * @return string escaped string
  321. */
  322. static public function escapeOnce($value)
  323. {
  324. return self::fixDoubleEscape(htmlspecialchars((string) $value, ENT_QUOTES, self::getCharset()));
  325. }
  326. /**
  327. * Fixes double escaped strings.
  328. *
  329. * @param string $escaped string to fix
  330. * @return string single escaped string
  331. */
  332. static public function fixDoubleEscape($escaped)
  333. {
  334. return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
  335. }
  336. /**
  337. * Converts an array of attributes to its HTML representation.
  338. *
  339. * @param array $attributes An array of attributes
  340. *
  341. * @return string The HTML representation of the HTML attribute array.
  342. */
  343. public function attributesToHtml($attributes)
  344. {
  345. $attributes = array_merge($this->attributes, $attributes);
  346. return implode('', array_map(array($this, 'attributesToHtmlCallback'), array_keys($attributes), array_values($attributes)));
  347. }
  348. /**
  349. * Prepares an attribute key and value for HTML representation.
  350. *
  351. * It removes empty attributes, except for the value one.
  352. *
  353. * @param string $k The attribute key
  354. * @param string $v The attribute value
  355. *
  356. * @return string The HTML representation of the HTML key attribute pair.
  357. */
  358. protected function attributesToHtmlCallback($k, $v)
  359. {
  360. return false === $v || null === $v || ('' === $v && 'value' != $k) ? '' : sprintf(' %s="%s"', $k, $this->escapeOnce($v));
  361. }
  362. }