Container
6 years ago
Controller
6 years ago
DataSource
6 years ago
Element
6 years ago
Renderer
6 years ago
Rule
6 years ago
Container.php
6 years ago
Controller.php
6 years ago
DataSource.php
6 years ago
Element.php
6 years ago
Exception.php
6 years ago
Factory.php
6 years ago
JavascriptBuilder.php
6 years ago
Loader.php
6 years ago
Node.php
6 years ago
Renderer.php
6 years ago
Rule.php
6 years ago
Factory.php
234 lines
| 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 | /** |
| 47 | * Class with static methods for loading classes and files |
| 48 | */ |
| 49 | // require_once 'HTML/QuickForm2/Loader.php'; |
| 50 | |
| 51 | /** |
| 52 | * Static factory class |
| 53 | * |
| 54 | * The class handles instantiation of Element and Rule objects as well as |
| 55 | * registering of new Element and Rule classes. |
| 56 | * |
| 57 | * @category HTML |
| 58 | * @package HTML_QuickForm2 |
| 59 | * @author Alexey Borzov <avb@php.net> |
| 60 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 61 | * @version Release: @package_version@ |
| 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 | 'image' => array('HTML_QuickForm2_Element_InputImage', null), |
| 78 | 'inputbutton' => array('HTML_QuickForm2_Element_InputButton', null), |
| 79 | 'password' => array('HTML_QuickForm2_Element_InputPassword', null), |
| 80 | 'radio' => array('HTML_QuickForm2_Element_InputRadio', null), |
| 81 | 'reset' => array('HTML_QuickForm2_Element_InputReset', null), |
| 82 | 'select' => array('HTML_QuickForm2_Element_Select', null), |
| 83 | 'submit' => array('HTML_QuickForm2_Element_InputSubmit', null), |
| 84 | 'text' => array('HTML_QuickForm2_Element_InputText', null), |
| 85 | 'textarea' => array('HTML_QuickForm2_Element_Textarea', null) |
| 86 | ); |
| 87 | |
| 88 | /** |
| 89 | * List of registered rules |
| 90 | * @var array |
| 91 | */ |
| 92 | protected static $registeredRules = array( |
| 93 | 'nonempty' => array('HTML_QuickForm2_Rule_Nonempty', null), |
| 94 | 'empty' => array('HTML_QuickForm2_Rule_Empty', null), |
| 95 | 'required' => array('HTML_QuickForm2_Rule_Required', null), |
| 96 | 'compare' => array('HTML_QuickForm2_Rule_Compare', null), |
| 97 | 'eq' => array('HTML_QuickForm2_Rule_Compare', null, |
| 98 | array('operator' => '===')), |
| 99 | 'neq' => array('HTML_QuickForm2_Rule_Compare', null, |
| 100 | array('operator' => '!==')), |
| 101 | 'lt' => array('HTML_QuickForm2_Rule_Compare', null, |
| 102 | array('operator' => '<')), |
| 103 | 'lte' => array('HTML_QuickForm2_Rule_Compare', null, |
| 104 | array('operator' => '<=')), |
| 105 | 'gt' => array('HTML_QuickForm2_Rule_Compare', null, |
| 106 | array('operator' => '>')), |
| 107 | 'gte' => array('HTML_QuickForm2_Rule_Compare', null, |
| 108 | array('operator' => '>=')), |
| 109 | 'regex' => array('HTML_QuickForm2_Rule_Regex', null), |
| 110 | 'callback' => array('HTML_QuickForm2_Rule_Callback', null), |
| 111 | 'length' => array('HTML_QuickForm2_Rule_Length', null), |
| 112 | 'minlength' => array('HTML_QuickForm2_Rule_Length', null, |
| 113 | array('max' => 0)), |
| 114 | 'maxlength' => array('HTML_QuickForm2_Rule_Length', null, |
| 115 | array('min' => 0)), |
| 116 | 'maxfilesize' => array('HTML_QuickForm2_Rule_MaxFileSize', null), |
| 117 | 'mimetype' => array('HTML_QuickForm2_Rule_MimeType', null), |
| 118 | 'each' => array('HTML_QuickForm2_Rule_Each', null), |
| 119 | 'notcallback' => array('HTML_QuickForm2_Rule_NotCallback', null), |
| 120 | 'notregex' => array('HTML_QuickForm2_Rule_NotRegex', null) |
| 121 | ); |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Registers a new element type |
| 126 | * |
| 127 | * @param string Type name (treated case-insensitively) |
| 128 | * @param string Class name |
| 129 | * @param string File containing the class, leave empty if class already loaded |
| 130 | */ |
| 131 | public static function registerElement($type, $className, $includeFile = null) |
| 132 | { |
| 133 | self::$elementTypes[strtolower($type)] = array($className, $includeFile); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Checks whether an element type is known to factory |
| 139 | * |
| 140 | * @param string Type name (treated case-insensitively) |
| 141 | * @return bool |
| 142 | */ |
| 143 | public static function isElementRegistered($type) |
| 144 | { |
| 145 | return isset(self::$elementTypes[strtolower($type)]); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Creates a new element object of the given type |
| 151 | * |
| 152 | * @param string Type name (treated case-insensitively) |
| 153 | * @param mixed Element name (passed to element's constructor) |
| 154 | * @param mixed Element attributes (passed to element's constructor) |
| 155 | * @param array Element-specific data (passed to element's constructor) |
| 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($type, $name = null, $attributes = null, |
| 162 | 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 | if (!class_exists($className)) { |
| 170 | HTML_QuickForm2_Loader::loadClass($className, $includeFile); |
| 171 | } |
| 172 | return new $className($name, $attributes, $data); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Registers a new rule type |
| 178 | * |
| 179 | * @param string Rule type name (treated case-insensitively) |
| 180 | * @param string Class name |
| 181 | * @param string File containing the class, leave empty if class already loaded |
| 182 | * @param mixed Configuration data for rules of the given type |
| 183 | */ |
| 184 | public static function registerRule($type, $className, $includeFile = null, |
| 185 | $config = null) |
| 186 | { |
| 187 | self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Checks whether a rule type is known to Factory |
| 193 | * |
| 194 | * @param string Rule type name (treated case-insensitively) |
| 195 | * @return bool |
| 196 | */ |
| 197 | public static function isRuleRegistered($type) |
| 198 | { |
| 199 | return isset(self::$registeredRules[strtolower($type)]); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Creates a new Rule of the given type |
| 205 | * |
| 206 | * @param string Rule type name (treated case-insensitively) |
| 207 | * @param HTML_QuickForm2_Node Element to validate by the rule |
| 208 | * @param string Message to display if validation fails |
| 209 | * @param mixed Configuration data for the rule |
| 210 | * @return HTML_QuickForm2_Rule A created Rule |
| 211 | * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown |
| 212 | * @throws HTML_QuickForm2_NotFoundException If class for the rule |
| 213 | * can't be found and/or loaded from file |
| 214 | */ |
| 215 | public static function createRule($type, HTML_QuickForm2_Node $owner, |
| 216 | $message = '', $config = null) |
| 217 | { |
| 218 | $type = strtolower($type); |
| 219 | if (!isset(self::$registeredRules[$type])) { |
| 220 | throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known"); |
| 221 | } |
| 222 | list($className, $includeFile) = self::$registeredRules[$type]; |
| 223 | if (!class_exists($className)) { |
| 224 | HTML_QuickForm2_Loader::loadClass($className, $includeFile); |
| 225 | } |
| 226 | if (isset(self::$registeredRules[$type][2])) { |
| 227 | $config = call_user_func(array($className, 'mergeConfig'), $config, |
| 228 | self::$registeredRules[$type][2]); |
| 229 | } |
| 230 | return new $className($owner, $message, $config); |
| 231 | } |
| 232 | } |
| 233 | ?> |
| 234 |