PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/HTML/QuickForm2/Factory.php

https://github.com/CodeYellowBV/piwik
PHP | 233 lines | 100 code | 15 blank | 118 comment | 5 complexity | e6570f82222a308a5e3292555dac07db MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-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-2010, 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 299305 2010-05-12 20:15:28Z 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. * @version Release: @package_version@
  60. */
  61. class HTML_QuickForm2_Factory
  62. {
  63. /**
  64. * List of element types known to Factory
  65. * @var array
  66. */
  67. protected static $elementTypes = array(
  68. 'button' => array('HTML_QuickForm2_Element_Button', null),
  69. 'checkbox' => array('HTML_QuickForm2_Element_InputCheckbox', null),
  70. 'date' => array('HTML_QuickForm2_Element_Date', null),
  71. 'fieldset' => array('HTML_QuickForm2_Container_Fieldset', null),
  72. 'group' => array('HTML_QuickForm2_Container_Group', null),
  73. 'file' => array('HTML_QuickForm2_Element_InputFile', null),
  74. 'hidden' => array('HTML_QuickForm2_Element_InputHidden', null),
  75. 'image' => array('HTML_QuickForm2_Element_InputImage', null),
  76. 'inputbutton' => array('HTML_QuickForm2_Element_InputButton', null),
  77. 'password' => array('HTML_QuickForm2_Element_InputPassword', null),
  78. 'radio' => array('HTML_QuickForm2_Element_InputRadio', null),
  79. 'reset' => array('HTML_QuickForm2_Element_InputReset', null),
  80. 'select' => array('HTML_QuickForm2_Element_Select', null),
  81. 'submit' => array('HTML_QuickForm2_Element_InputSubmit', null),
  82. 'text' => array('HTML_QuickForm2_Element_InputText', null),
  83. 'textarea' => array('HTML_QuickForm2_Element_Textarea', null)
  84. );
  85. /**
  86. * List of registered rules
  87. * @var array
  88. */
  89. protected static $registeredRules = array(
  90. 'nonempty' => array('HTML_QuickForm2_Rule_Nonempty', null),
  91. 'empty' => array('HTML_QuickForm2_Rule_Empty', null),
  92. 'required' => array('HTML_QuickForm2_Rule_Required', null),
  93. 'compare' => array('HTML_QuickForm2_Rule_Compare', null),
  94. 'eq' => array('HTML_QuickForm2_Rule_Compare', null,
  95. array('operator' => '===')),
  96. 'neq' => array('HTML_QuickForm2_Rule_Compare', null,
  97. array('operator' => '!==')),
  98. 'lt' => array('HTML_QuickForm2_Rule_Compare', null,
  99. array('operator' => '<')),
  100. 'lte' => array('HTML_QuickForm2_Rule_Compare', null,
  101. array('operator' => '<=')),
  102. 'gt' => array('HTML_QuickForm2_Rule_Compare', null,
  103. array('operator' => '>')),
  104. 'gte' => array('HTML_QuickForm2_Rule_Compare', null,
  105. array('operator' => '>=')),
  106. 'regex' => array('HTML_QuickForm2_Rule_Regex', null),
  107. 'callback' => array('HTML_QuickForm2_Rule_Callback', null),
  108. 'length' => array('HTML_QuickForm2_Rule_Length', null),
  109. 'minlength' => array('HTML_QuickForm2_Rule_Length', null,
  110. array('max' => 0)),
  111. 'maxlength' => array('HTML_QuickForm2_Rule_Length', null,
  112. array('min' => 0)),
  113. 'maxfilesize' => array('HTML_QuickForm2_Rule_MaxFileSize', null),
  114. 'mimetype' => array('HTML_QuickForm2_Rule_MimeType', null),
  115. 'each' => array('HTML_QuickForm2_Rule_Each', null),
  116. 'notcallback' => array('HTML_QuickForm2_Rule_NotCallback', null),
  117. 'notregex' => array('HTML_QuickForm2_Rule_NotRegex', null)
  118. );
  119. /**
  120. * Registers a new element type
  121. *
  122. * @param string Type name (treated case-insensitively)
  123. * @param string Class name
  124. * @param string File containing the class, leave empty if class already loaded
  125. */
  126. public static function registerElement($type, $className, $includeFile = null)
  127. {
  128. self::$elementTypes[strtolower($type)] = array($className, $includeFile);
  129. }
  130. /**
  131. * Checks whether an element type is known to factory
  132. *
  133. * @param string Type name (treated case-insensitively)
  134. * @return bool
  135. */
  136. public static function isElementRegistered($type)
  137. {
  138. return isset(self::$elementTypes[strtolower($type)]);
  139. }
  140. /**
  141. * Creates a new element object of the given type
  142. *
  143. * @param string Type name (treated case-insensitively)
  144. * @param mixed Element name (passed to element's constructor)
  145. * @param mixed Element attributes (passed to element's constructor)
  146. * @param array Element-specific data (passed to element's constructor)
  147. * @return HTML_QuickForm2_Node A created element
  148. * @throws HTML_QuickForm2_InvalidArgumentException If type name is unknown
  149. * @throws HTML_QuickForm2_NotFoundException If class for the element can
  150. * not be found and/or loaded from file
  151. */
  152. public static function createElement($type, $name = null, $attributes = null,
  153. array $data = array())
  154. {
  155. $type = strtolower($type);
  156. if (!isset(self::$elementTypes[$type])) {
  157. throw new HTML_QuickForm2_InvalidArgumentException("Element type '$type' is not known");
  158. }
  159. list($className, $includeFile) = self::$elementTypes[$type];
  160. if (!class_exists($className)) {
  161. HTML_QuickForm2_Loader::loadClass($className, $includeFile);
  162. }
  163. return new $className($name, $attributes, $data);
  164. }
  165. /**
  166. * Registers a new rule type
  167. *
  168. * @param string Rule type name (treated case-insensitively)
  169. * @param string Class name
  170. * @param string File containing the class, leave empty if class already loaded
  171. * @param mixed Configuration data for rules of the given type
  172. */
  173. public static function registerRule($type, $className, $includeFile = null,
  174. $config = null)
  175. {
  176. self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config);
  177. }
  178. /**
  179. * Checks whether a rule type is known to Factory
  180. *
  181. * @param string Rule type name (treated case-insensitively)
  182. * @return bool
  183. */
  184. public static function isRuleRegistered($type)
  185. {
  186. return isset(self::$registeredRules[strtolower($type)]);
  187. }
  188. /**
  189. * Creates a new Rule of the given type
  190. *
  191. * @param string Rule type name (treated case-insensitively)
  192. * @param HTML_QuickForm2_Node Element to validate by the rule
  193. * @param string Message to display if validation fails
  194. * @param mixed Configuration data for the rule
  195. * @return HTML_QuickForm2_Rule A created Rule
  196. * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown
  197. * @throws HTML_QuickForm2_NotFoundException If class for the rule
  198. * can't be found and/or loaded from file
  199. */
  200. public static function createRule($type, HTML_QuickForm2_Node $owner,
  201. $message = '', $config = null)
  202. {
  203. $type = strtolower($type);
  204. if (!isset(self::$registeredRules[$type])) {
  205. throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known");
  206. }
  207. list($className, $includeFile) = self::$registeredRules[$type];
  208. if (!class_exists($className)) {
  209. HTML_QuickForm2_Loader::loadClass($className, $includeFile);
  210. }
  211. if (isset(self::$registeredRules[$type][2])) {
  212. $config = call_user_func(array($className, 'mergeConfig'), $config,
  213. self::$registeredRules[$type][2]);
  214. }
  215. return new $className($owner, $message, $config);
  216. }
  217. }
  218. ?>