PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/spekkionu/passworddb
PHP | 243 lines | 104 code | 15 blank | 124 comment | 3 complexity | a3ba7218d1fea0175f4c4da5eb5de48c MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Static Factory class for HTML_QuickForm2 package
  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: Factory.php 325159 2012-04-13 21:42:23Z 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. * Static factory class
  51. *
  52. * The class handles instantiation of Element and Rule objects as well as
  53. * registering of new Element and Rule classes.
  54. *
  55. * @category HTML
  56. * @package HTML_QuickForm2
  57. * @author Alexey Borzov <avb@php.net>
  58. * @author Bertrand Mansion <golgote@mamasam.com>
  59. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  60. * @version Release: 2.0.0
  61. * @link http://pear.php.net/package/HTML_QuickForm2
  62. */
  63. class HTML_QuickForm2_Factory
  64. {
  65. /**
  66. * List of element types known to Factory
  67. * @var array
  68. */
  69. protected static $elementTypes = array(
  70. 'button' => array('HTML_QuickForm2_Element_Button', null),
  71. 'checkbox' => array('HTML_QuickForm2_Element_InputCheckbox', null),
  72. 'date' => array('HTML_QuickForm2_Element_Date', null),
  73. 'fieldset' => array('HTML_QuickForm2_Container_Fieldset', null),
  74. 'group' => array('HTML_QuickForm2_Container_Group', null),
  75. 'file' => array('HTML_QuickForm2_Element_InputFile', null),
  76. 'hidden' => array('HTML_QuickForm2_Element_InputHidden', null),
  77. 'hierselect' => array('HTML_QuickForm2_Element_Hierselect', null),
  78. 'image' => array('HTML_QuickForm2_Element_InputImage', null),
  79. 'inputbutton' => array('HTML_QuickForm2_Element_InputButton', null),
  80. 'password' => array('HTML_QuickForm2_Element_InputPassword', null),
  81. 'radio' => array('HTML_QuickForm2_Element_InputRadio', null),
  82. 'repeat' => array('HTML_QuickForm2_Container_Repeat', null),
  83. 'reset' => array('HTML_QuickForm2_Element_InputReset', null),
  84. 'script' => array('HTML_QuickForm2_Element_Script', null),
  85. 'select' => array('HTML_QuickForm2_Element_Select', null),
  86. 'static' => array('HTML_QuickForm2_Element_Static', null),
  87. 'submit' => array('HTML_QuickForm2_Element_InputSubmit', null),
  88. 'text' => array('HTML_QuickForm2_Element_InputText', null),
  89. 'textarea' => array('HTML_QuickForm2_Element_Textarea', null)
  90. );
  91. /**
  92. * List of registered rules
  93. * @var array
  94. */
  95. protected static $registeredRules = array(
  96. 'nonempty' => array('HTML_QuickForm2_Rule_Nonempty', null),
  97. 'empty' => array('HTML_QuickForm2_Rule_Empty', null),
  98. 'required' => array('HTML_QuickForm2_Rule_Required', null),
  99. 'compare' => array('HTML_QuickForm2_Rule_Compare', null),
  100. 'eq' => array('HTML_QuickForm2_Rule_Compare', null,
  101. array('operator' => '===')),
  102. 'neq' => array('HTML_QuickForm2_Rule_Compare', null,
  103. array('operator' => '!==')),
  104. 'lt' => array('HTML_QuickForm2_Rule_Compare', null,
  105. array('operator' => '<')),
  106. 'lte' => array('HTML_QuickForm2_Rule_Compare', null,
  107. array('operator' => '<=')),
  108. 'gt' => array('HTML_QuickForm2_Rule_Compare', null,
  109. array('operator' => '>')),
  110. 'gte' => array('HTML_QuickForm2_Rule_Compare', null,
  111. array('operator' => '>=')),
  112. 'regex' => array('HTML_QuickForm2_Rule_Regex', null),
  113. 'callback' => array('HTML_QuickForm2_Rule_Callback', null),
  114. 'length' => array('HTML_QuickForm2_Rule_Length', null),
  115. 'minlength' => array('HTML_QuickForm2_Rule_Length', null,
  116. array('max' => 0)),
  117. 'maxlength' => array('HTML_QuickForm2_Rule_Length', null,
  118. array('min' => 0)),
  119. 'maxfilesize' => array('HTML_QuickForm2_Rule_MaxFileSize', null),
  120. 'mimetype' => array('HTML_QuickForm2_Rule_MimeType', null),
  121. 'each' => array('HTML_QuickForm2_Rule_Each', null),
  122. 'notcallback' => array('HTML_QuickForm2_Rule_NotCallback', null),
  123. 'notregex' => array('HTML_QuickForm2_Rule_NotRegex', null),
  124. 'email' => array('HTML_QuickForm2_Rule_Email', null)
  125. );
  126. /**
  127. * Registers a new element type
  128. *
  129. * @param string $type Type name (treated case-insensitively)
  130. * @param string $className Class name
  131. * @param string $includeFile File containing the class, leave empty if class already loaded
  132. */
  133. public static function registerElement($type, $className, $includeFile = null)
  134. {
  135. self::$elementTypes[strtolower($type)] = array($className, $includeFile);
  136. }
  137. /**
  138. * Checks whether an element type is known to factory
  139. *
  140. * @param string $type Type name (treated case-insensitively)
  141. *
  142. * @return bool
  143. */
  144. public static function isElementRegistered($type)
  145. {
  146. return isset(self::$elementTypes[strtolower($type)]);
  147. }
  148. /**
  149. * Creates a new element object of the given type
  150. *
  151. * @param string $type Type name (treated case-insensitively)
  152. * @param string $name Element name (passed to element's constructor)
  153. * @param string|array $attributes Element attributes (passed to element's constructor)
  154. * @param array $data Element-specific data (passed to element's constructor)
  155. *
  156. * @return HTML_QuickForm2_Node A created element
  157. * @throws HTML_QuickForm2_InvalidArgumentException If type name is unknown
  158. * @throws HTML_QuickForm2_NotFoundException If class for the element can
  159. * not be found and/or loaded from file
  160. */
  161. public static function createElement(
  162. $type, $name = null, $attributes = null, array $data = array()
  163. ) {
  164. $type = strtolower($type);
  165. if (!isset(self::$elementTypes[$type])) {
  166. throw new HTML_QuickForm2_InvalidArgumentException("Element type '$type' is not known");
  167. }
  168. list($className, $includeFile) = self::$elementTypes[$type];
  169. HTML_QuickForm2_Loader::loadClass($className, $includeFile);
  170. return new $className($name, $attributes, $data);
  171. }
  172. /**
  173. * Registers a new rule type
  174. *
  175. * @param string $type Rule type name (treated case-insensitively)
  176. * @param string $className Class name
  177. * @param string $includeFile File containing the class,
  178. * leave empty if class already loaded
  179. * @param mixed $config Configuration data for rules of the given type
  180. */
  181. public static function registerRule(
  182. $type, $className, $includeFile = null, $config = null
  183. ) {
  184. self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config);
  185. }
  186. /**
  187. * Checks whether a rule type is known to Factory
  188. *
  189. * @param string $type Rule type name (treated case-insensitively)
  190. *
  191. * @return bool
  192. */
  193. public static function isRuleRegistered($type)
  194. {
  195. return isset(self::$registeredRules[strtolower($type)]);
  196. }
  197. /**
  198. * Creates a new Rule of the given type
  199. *
  200. * @param string $type Rule type name (treated case-insensitively)
  201. * @param HTML_QuickForm2_Node $owner Element to validate by the rule
  202. * @param string $message Message to display if validation fails
  203. * @param mixed $config Configuration data for the rule
  204. *
  205. * @return HTML_QuickForm2_Rule A created Rule
  206. * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown
  207. * @throws HTML_QuickForm2_NotFoundException If class for the rule
  208. * can't be found and/or loaded from file
  209. */
  210. public static function createRule(
  211. $type, HTML_QuickForm2_Node $owner, $message = '', $config = null
  212. ) {
  213. $type = strtolower($type);
  214. if (!isset(self::$registeredRules[$type])) {
  215. throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known");
  216. }
  217. list($className, $includeFile) = self::$registeredRules[$type];
  218. HTML_QuickForm2_Loader::loadClass($className, $includeFile);
  219. if (isset(self::$registeredRules[$type][2])) {
  220. $config = call_user_func(
  221. array($className, 'mergeConfig'),
  222. $config, self::$registeredRules[$type][2]
  223. );
  224. }
  225. return new $className($owner, $message, $config);
  226. }
  227. }
  228. ?>