PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Renderer.php

https://bitbucket.org/spekkionu/passworddb
PHP | 395 lines | 128 code | 27 blank | 240 comment | 12 complexity | 38922cd3f7dd01c77729771d297eb65a MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Base class for HTML_QuickForm2 renderers
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: Renderer.php 325209 2012-04-15 15:01:57Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Class with static methods for loading classes and files
  47. */
  48. require_once 'HTML/QuickForm2/Loader.php';
  49. /**
  50. * Abstract base class for QuickForm2 renderers
  51. *
  52. * This class serves two main purposes:
  53. * <ul>
  54. * <li>Defines the API all renderers should implement (render*() methods);</li>
  55. * <li>Provides static methods for registering renderers and their plugins
  56. * and {@link factory()} method for creating renderer instances.</li>
  57. * </ul>
  58. *
  59. * Note that renderers should always be instantiated through factory(), in the
  60. * other case it will not be possible to add plugins.
  61. *
  62. * @category HTML
  63. * @package HTML_QuickForm2
  64. * @author Alexey Borzov <avb@php.net>
  65. * @author Bertrand Mansion <golgote@mamasam.com>
  66. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  67. * @version Release: 2.0.0
  68. * @link http://pear.php.net/package/HTML_QuickForm2
  69. */
  70. abstract class HTML_QuickForm2_Renderer
  71. {
  72. /**
  73. * List of registered renderer types
  74. * @var array
  75. */
  76. private static $_types = array(
  77. 'callback' => array('HTML_QuickForm2_Renderer_Callback', null),
  78. 'default' => array('HTML_QuickForm2_Renderer_Default', null),
  79. 'array' => array('HTML_QuickForm2_Renderer_Array', null),
  80. 'stub' => array('HTML_QuickForm2_Renderer_Stub', null)
  81. );
  82. /**
  83. * List of registered renderer plugins
  84. * @var array
  85. */
  86. private static $_pluginClasses = array(
  87. 'callback' => array(),
  88. 'default' => array(),
  89. 'array' => array(),
  90. 'stub' => array()
  91. );
  92. /**
  93. * Renderer options
  94. * @var array
  95. * @see setOption()
  96. */
  97. protected $options = array(
  98. 'group_hiddens' => true,
  99. 'required_note' => '<em>*</em> denotes required fields.',
  100. 'errors_prefix' => 'Invalid information entered:',
  101. 'errors_suffix' => 'Please correct these fields.',
  102. 'group_errors' => false
  103. );
  104. /**
  105. * Javascript builder object
  106. * @var HTML_QuickForm2_JavascriptBuilder
  107. */
  108. protected $jsBuilder;
  109. /**
  110. * Creates a new renderer instance of the given type
  111. *
  112. * A renderer is always wrapped by a Proxy, which handles calling its
  113. * "published" methods and methods of its plugins. Registered plugins are
  114. * added automagically to the existing renderer instances so that
  115. * <code>
  116. * $foo = HTML_QuickForm2_Renderer::factory('foo');
  117. * // Plugin implementing bar() method
  118. * HTML_QuickForm2_Renderer::registerPlugin('foo', 'Plugin_Foo_Bar');
  119. * $foo->bar();
  120. * </code>
  121. * will work.
  122. *
  123. * @param string $type Type name (treated case-insensitively)
  124. *
  125. * @return HTML_QuickForm2_Renderer_Proxy A renderer instance of the given
  126. * type wrapped by a Proxy
  127. * @throws HTML_QuickForm2_InvalidArgumentException If type name is unknown
  128. * @throws HTML_QuickForm2_NotFoundException If class for the renderer can
  129. * not be found and/or loaded from file
  130. */
  131. final public static function factory($type)
  132. {
  133. $type = strtolower($type);
  134. if (!isset(self::$_types[$type])) {
  135. throw new HTML_QuickForm2_InvalidArgumentException(
  136. "Renderer type '$type' is not known"
  137. );
  138. }
  139. list ($className, $includeFile) = self::$_types[$type];
  140. HTML_QuickForm2_Loader::loadClass($className, $includeFile);
  141. HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_Renderer_Proxy');
  142. return new HTML_QuickForm2_Renderer_Proxy(new $className, self::$_pluginClasses[$type]);
  143. }
  144. /**
  145. * Registers a new renderer type
  146. *
  147. * @param string $type Type name (treated case-insensitively)
  148. * @param string $className Class name
  149. * @param string $includeFile File containing the class, leave empty
  150. * if class already loaded
  151. *
  152. * @throws HTML_QuickForm2_InvalidArgumentException if type already registered
  153. */
  154. final public static function register($type, $className, $includeFile = null)
  155. {
  156. $type = strtolower($type);
  157. self::$_types[$type] = array($className, $includeFile);
  158. if (empty(self::$_pluginClasses[$type])) {
  159. self::$_pluginClasses[$type] = array();
  160. }
  161. }
  162. /**
  163. * Registers a plugin for a renderer type
  164. *
  165. * @param string $type Renderer type name (treated case-insensitively)
  166. * @param string $className Plugin class name
  167. * @param string $includeFile File containing the plugin class, leave empty if class already loaded
  168. *
  169. * @throws HTML_QuickForm2_InvalidArgumentException if plugin is already registered
  170. */
  171. final public static function registerPlugin($type, $className, $includeFile = null)
  172. {
  173. $type = strtolower($type);
  174. // We don't check self::$_types, since a plugin may be registered
  175. // before renderer itself if it goes with some custom element
  176. if (empty(self::$_pluginClasses[$type])) {
  177. self::$_pluginClasses[$type] = array(array($className, $includeFile));
  178. } else {
  179. foreach (self::$_pluginClasses[$type] as $plugin) {
  180. if (0 == strcasecmp($plugin[0], $className)) {
  181. throw new HTML_QuickForm2_InvalidArgumentException(
  182. "Plugin '$className' for renderer type '$type' is already registered"
  183. );
  184. }
  185. }
  186. self::$_pluginClasses[$type][] = array($className, $includeFile);
  187. }
  188. }
  189. /**
  190. * Constructor
  191. *
  192. * Renderer instances should not be created directly, use {@link factory()}
  193. */
  194. protected function __construct()
  195. {
  196. }
  197. /**
  198. * Returns an array of "published" method names that should be callable through proxy
  199. *
  200. * Methods defined in HTML_QuickForm2_Renderer are proxied automatically,
  201. * only additional methods should be returned.
  202. *
  203. * @return array
  204. */
  205. protected function exportMethods()
  206. {
  207. return array();
  208. }
  209. /**
  210. * Checks whether a method is available in this object
  211. *
  212. * @param string $name Method name
  213. *
  214. * @return bool
  215. */
  216. public function methodExists($name)
  217. {
  218. try {
  219. $method = new ReflectionMethod($this, $name);
  220. return $method->isPublic();
  221. } catch (ReflectionException $e) {
  222. return false;
  223. }
  224. }
  225. /**
  226. * Sets the option(s) affecting renderer behaviour
  227. *
  228. * The following options are available:
  229. * <ul>
  230. * <li>'group_hiddens' - whether to group hidden elements together or
  231. * render them where they were added (boolean)</li>
  232. * <li>'group_errors' - whether to group error messages or render them
  233. * alongside elements they apply to (boolean)</li>
  234. * <li>'errors_prefix' - leading message for grouped errors (string)</li>
  235. * <li>'errors_suffix' - trailing message for grouped errors (string)</li>
  236. * <li>'required_note' - note displayed if the form contains required
  237. * elements (string)</li>
  238. * </ul>
  239. *
  240. * @param string|array $nameOrOptions option name or array ('option name' => 'option value')
  241. * @param mixed $value parameter value if $nameOrConfig is not an array
  242. *
  243. * @return HTML_QuickForm2_Renderer
  244. * @throws HTML_QuickForm2_NotFoundException in case of unknown option
  245. */
  246. public function setOption($nameOrOptions, $value = null)
  247. {
  248. if (is_array($nameOrOptions)) {
  249. foreach ($nameOrOptions as $name => $value) {
  250. $this->setOption($name, $value);
  251. }
  252. } else {
  253. if (!array_key_exists($nameOrOptions, $this->options)) {
  254. throw new HTML_QuickForm2_NotFoundException(
  255. "Unknown option '{$nameOrOptions}'"
  256. );
  257. }
  258. $this->options[$nameOrOptions] = $value;
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Returns the value(s) of the renderer option(s)
  264. *
  265. * @param string $name parameter name
  266. *
  267. * @return mixed value of $name parameter, array of all configuration
  268. * parameters if $name is not given
  269. * @throws HTML_QuickForm2_NotFoundException in case of unknown option
  270. */
  271. public function getOption($name = null)
  272. {
  273. if (null === $name) {
  274. return $this->options;
  275. } elseif (!array_key_exists($name, $this->options)) {
  276. throw new HTML_QuickForm2_NotFoundException(
  277. "Unknown option '{$name}'"
  278. );
  279. }
  280. return $this->options[$name];
  281. }
  282. /**
  283. * Returns the javascript builder object
  284. *
  285. * @return HTML_QuickForm2_JavascriptBuilder
  286. */
  287. public function getJavascriptBuilder()
  288. {
  289. if (empty($this->jsBuilder)) {
  290. HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder');
  291. $this->jsBuilder = new HTML_QuickForm2_JavascriptBuilder();
  292. }
  293. return $this->jsBuilder;
  294. }
  295. /**
  296. * Sets the javascript builder object
  297. *
  298. * You may want to reuse the same builder object if outputting several
  299. * forms on one page.
  300. *
  301. * @param HTML_QuickForm2_JavascriptBuilder $builder
  302. *
  303. * @return HTML_QuickForm2_Renderer
  304. */
  305. public function setJavascriptBuilder(HTML_QuickForm2_JavascriptBuilder $builder = null)
  306. {
  307. $this->jsBuilder = $builder;
  308. return $this;
  309. }
  310. /**
  311. * Resets the accumulated data
  312. *
  313. * This method is called automatically by startForm() method, but should
  314. * be called manually before calling other rendering methods separately.
  315. *
  316. * @return HTML_QuickForm2_Renderer
  317. */
  318. abstract public function reset();
  319. /**
  320. * Renders a generic element
  321. *
  322. * @param HTML_QuickForm2_Node $element Element being rendered
  323. */
  324. abstract public function renderElement(HTML_QuickForm2_Node $element);
  325. /**
  326. * Renders a hidden element
  327. *
  328. * @param HTML_QuickForm2_Node $element Hidden element being rendered
  329. */
  330. abstract public function renderHidden(HTML_QuickForm2_Node $element);
  331. /**
  332. * Starts rendering a form, called before processing contained elements
  333. *
  334. * @param HTML_QuickForm2_Node $form Form being rendered
  335. */
  336. abstract public function startForm(HTML_QuickForm2_Node $form);
  337. /**
  338. * Finishes rendering a form, called after processing contained elements
  339. *
  340. * @param HTML_QuickForm2_Node $form Form being rendered
  341. */
  342. abstract public function finishForm(HTML_QuickForm2_Node $form);
  343. /**
  344. * Starts rendering a generic container, called before processing contained elements
  345. *
  346. * @param HTML_QuickForm2_Node $container Container being rendered
  347. */
  348. abstract public function startContainer(HTML_QuickForm2_Node $container);
  349. /**
  350. * Finishes rendering a generic container, called after processing contained elements
  351. *
  352. * @param HTML_QuickForm2_Node $container Container being rendered
  353. */
  354. abstract public function finishContainer(HTML_QuickForm2_Node $container);
  355. /**
  356. * Starts rendering a group, called before processing grouped elements
  357. *
  358. * @param HTML_QuickForm2_Node $group Group being rendered
  359. */
  360. abstract public function startGroup(HTML_QuickForm2_Node $group);
  361. /**
  362. * Finishes rendering a group, called after processing grouped elements
  363. *
  364. * @param HTML_QuickForm2_Node $group Group being rendered
  365. */
  366. abstract public function finishGroup(HTML_QuickForm2_Node $group);
  367. }
  368. ?>