File: Factory.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (233 lines) | stat: -rw-r--r-- 10,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * Static Factory class for HTML_QuickForm2 package
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>,
 *                          Bertrand Mansion <golgote@mamasam.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * The names of the authors may not be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   HTML
 * @package    HTML_QuickForm2
 * @author     Alexey Borzov <avb@php.net>
 * @author     Bertrand Mansion <golgote@mamasam.com>
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 * @version    SVN: $Id: Factory.php 299305 2010-05-12 20:15:28Z avb $
 * @link       http://pear.php.net/package/HTML_QuickForm2
 */

/**
 * Class with static methods for loading classes and files
 */
// require_once 'HTML/QuickForm2/Loader.php';

/**
 * Static factory class
 *
 * The class handles instantiation of Element and Rule objects as well as
 * registering of new Element and Rule classes.
 *
 * @category   HTML
 * @package    HTML_QuickForm2
 * @author     Alexey Borzov <avb@php.net>
 * @author     Bertrand Mansion <golgote@mamasam.com>
 * @version    Release: @package_version@
 */
class HTML_QuickForm2_Factory
{
   /**
    * List of element types known to Factory
    * @var array
    */
    protected static $elementTypes = array(
        'button'        => array('HTML_QuickForm2_Element_Button', null),
        'checkbox'      => array('HTML_QuickForm2_Element_InputCheckbox', null),
        'date'          => array('HTML_QuickForm2_Element_Date', null),
        'fieldset'      => array('HTML_QuickForm2_Container_Fieldset', null),
        'group'         => array('HTML_QuickForm2_Container_Group', null),
        'file'          => array('HTML_QuickForm2_Element_InputFile', null),
        'hidden'        => array('HTML_QuickForm2_Element_InputHidden', null),
        'image'         => array('HTML_QuickForm2_Element_InputImage', null),
        'inputbutton'   => array('HTML_QuickForm2_Element_InputButton', null),
        'password'      => array('HTML_QuickForm2_Element_InputPassword', null),
        'radio'         => array('HTML_QuickForm2_Element_InputRadio', null),
        'reset'         => array('HTML_QuickForm2_Element_InputReset', null),
        'select'        => array('HTML_QuickForm2_Element_Select', null),
        'submit'        => array('HTML_QuickForm2_Element_InputSubmit', null),
        'text'          => array('HTML_QuickForm2_Element_InputText', null),
        'textarea'      => array('HTML_QuickForm2_Element_Textarea', null)
    );

   /**
    * List of registered rules
    * @var array
    */
    protected static $registeredRules = array(
        'nonempty'      => array('HTML_QuickForm2_Rule_Nonempty', null),
        'empty'         => array('HTML_QuickForm2_Rule_Empty', null),
        'required'      => array('HTML_QuickForm2_Rule_Required', null),
        'compare'       => array('HTML_QuickForm2_Rule_Compare', null),
        'eq'            => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '===')),
        'neq'           => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '!==')),
        'lt'            => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '<')),
        'lte'           => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '<=')),
        'gt'            => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '>')),
        'gte'           => array('HTML_QuickForm2_Rule_Compare', null,
                                 array('operator' => '>=')),
        'regex'         => array('HTML_QuickForm2_Rule_Regex', null),
        'callback'      => array('HTML_QuickForm2_Rule_Callback', null),
        'length'        => array('HTML_QuickForm2_Rule_Length', null),
        'minlength'     => array('HTML_QuickForm2_Rule_Length', null,
                                 array('max' => 0)),
        'maxlength'     => array('HTML_QuickForm2_Rule_Length', null,
                                 array('min' => 0)),
        'maxfilesize'   => array('HTML_QuickForm2_Rule_MaxFileSize', null),
        'mimetype'      => array('HTML_QuickForm2_Rule_MimeType', null),
        'each'          => array('HTML_QuickForm2_Rule_Each', null),
        'notcallback'   => array('HTML_QuickForm2_Rule_NotCallback', null),
        'notregex'      => array('HTML_QuickForm2_Rule_NotRegex', null)
    );


   /**
    * Registers a new element type
    *
    * @param    string  Type name (treated case-insensitively)
    * @param    string  Class name
    * @param    string  File containing the class, leave empty if class already loaded
    */
    public static function registerElement($type, $className, $includeFile = null)
    {
        self::$elementTypes[strtolower($type)] = array($className, $includeFile);
    }


   /**
    * Checks whether an element type is known to factory
    *
    * @param    string  Type name (treated case-insensitively)
    * @return   bool
    */
    public static function isElementRegistered($type)
    {
        return isset(self::$elementTypes[strtolower($type)]);
    }


   /**
    * Creates a new element object of the given type
    *
    * @param    string  Type name (treated case-insensitively)
    * @param    mixed   Element name (passed to element's constructor)
    * @param    mixed   Element attributes (passed to element's constructor)
    * @param    array   Element-specific data (passed to element's constructor)
    * @return   HTML_QuickForm2_Node     A created element
    * @throws   HTML_QuickForm2_InvalidArgumentException If type name is unknown
    * @throws   HTML_QuickForm2_NotFoundException If class for the element can
    *           not be found and/or loaded from file
    */
    public static function createElement($type, $name = null, $attributes = null,
                                         array $data = array())
    {
        $type = strtolower($type);
        if (!isset(self::$elementTypes[$type])) {
            throw new HTML_QuickForm2_InvalidArgumentException("Element type '$type' is not known");
        }
        list($className, $includeFile) = self::$elementTypes[$type];
        if (!class_exists($className)) {
            HTML_QuickForm2_Loader::loadClass($className, $includeFile);
        }
        return new $className($name, $attributes, $data);
    }


   /**
    * Registers a new rule type
    *
    * @param    string  Rule type name (treated case-insensitively)
    * @param    string  Class name
    * @param    string  File containing the class, leave empty if class already loaded
    * @param    mixed   Configuration data for rules of the given type
    */
    public static function registerRule($type, $className, $includeFile = null,
                                        $config = null)
    {
        self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config);
    }


   /**
    * Checks whether a rule type is known to Factory
    *
    * @param    string  Rule type name (treated case-insensitively)
    * @return   bool
    */
    public static function isRuleRegistered($type)
    {
        return isset(self::$registeredRules[strtolower($type)]);
    }


   /**
    * Creates a new Rule of the given type
    *
    * @param    string                  Rule type name (treated case-insensitively)
    * @param    HTML_QuickForm2_Node    Element to validate by the rule
    * @param    string                  Message to display if validation fails
    * @param    mixed                   Configuration data for the rule
    * @return   HTML_QuickForm2_Rule    A created Rule
    * @throws   HTML_QuickForm2_InvalidArgumentException If rule type is unknown
    * @throws   HTML_QuickForm2_NotFoundException        If class for the rule
    *           can't be found and/or loaded from file
    */
    public static function createRule($type, HTML_QuickForm2_Node $owner,
                                      $message = '', $config = null)
    {
        $type = strtolower($type);
        if (!isset(self::$registeredRules[$type])) {
            throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known");
        }
        list($className, $includeFile) = self::$registeredRules[$type];
        if (!class_exists($className)) {
            HTML_QuickForm2_Loader::loadClass($className, $includeFile);
        }
        if (isset(self::$registeredRules[$type][2])) {
            $config = call_user_func(array($className, 'mergeConfig'), $config,
                                     self::$registeredRules[$type][2]);
        }
        return new $className($owner, $message, $config);
    }
}
?>