home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm2 / Factory.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  13.8 KB  |  319 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, 2007, 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    CVS: $Id: Factory.php,v 1.23 2007/10/14 18:58:31 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Exception classes for HTML_QuickForm2
  48.  */
  49. require_once 'HTML/QuickForm2/Exception.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: 0.2.0
  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',
  71.                                  'HTML/QuickForm2/Element/Button.php'),
  72.         'checkbox'      => array('HTML_QuickForm2_Element_InputCheckbox',
  73.                                  'HTML/QuickForm2/Element/InputCheckbox.php'),
  74.         'fieldset'      => array('HTML_QuickForm2_Container_Fieldset',
  75.                                  'HTML/QuickForm2/Container/Fieldset.php'),
  76.         'file'          => array('HTML_QuickForm2_Element_InputFile',
  77.                                  'HTML/QuickForm2/Element/InputFile.php'),
  78.         'hidden'        => array('HTML_QuickForm2_Element_InputHidden',
  79.                                  'HTML/QuickForm2/Element/InputHidden.php'),
  80.         'image'         => array('HTML_QuickForm2_Element_InputImage',
  81.                                  'HTML/QuickForm2/Element/InputImage.php'),
  82.         'inputbutton'   => array('HTML_QuickForm2_Element_InputButton',
  83.                                  'HTML/QuickForm2/Element/InputButton.php'),
  84.         'password'      => array('HTML_QuickForm2_Element_InputPassword',
  85.                                  'HTML/QuickForm2/Element/InputPassword.php'),
  86.         'radio'         => array('HTML_QuickForm2_Element_InputRadio',
  87.                                  'HTML/QuickForm2/Element/InputRadio.php'),
  88.         'reset'         => array('HTML_QuickForm2_Element_InputReset',
  89.                                  'HTML/QuickForm2/Element/InputReset.php'),
  90.         'select'        => array('HTML_QuickForm2_Element_Select',
  91.                                  'HTML/QuickForm2/Element/Select.php'),
  92.         'submit'        => array('HTML_QuickForm2_Element_InputSubmit',
  93.                                  'HTML/QuickForm2/Element/InputSubmit.php'),
  94.         'text'          => array('HTML_QuickForm2_Element_InputText',
  95.                                  'HTML/QuickForm2/Element/InputText.php'),
  96.         'textarea'      => array('HTML_QuickForm2_Element_Textarea',
  97.                                  'HTML/QuickForm2/Element/Textarea.php')
  98.     );
  99.  
  100.    /**
  101.     * List of registered rules
  102.     * @var array
  103.     */
  104.     protected static $registeredRules = array(
  105.         'nonempty'      => array('HTML_QuickForm2_Rule_Nonempty',
  106.                                  'HTML/QuickForm2/Rule/Nonempty.php'),
  107.         'empty'         => array('HTML_QuickForm2_Rule_Empty',
  108.                                  'HTML/QuickForm2/Rule/Empty.php'),
  109.         'required'      => array('HTML_QuickForm2_Rule_Required',
  110.                                  'HTML/QuickForm2/Rule/Required.php'),
  111.         'compare'       => array('HTML_QuickForm2_Rule_Compare',
  112.                                  'HTML/QuickForm2/Rule/Compare.php'),
  113.         'eq'            => array('HTML_QuickForm2_Rule_Compare',
  114.                                  'HTML/QuickForm2/Rule/Compare.php',
  115.                                  array('operator' => '===')),
  116.         'neq'           => array('HTML_QuickForm2_Rule_Compare',
  117.                                  'HTML/QuickForm2/Rule/Compare.php',
  118.                                  array('operator' => '!==')),
  119.         'lt'            => array('HTML_QuickForm2_Rule_Compare',
  120.                                  'HTML/QuickForm2/Rule/Compare.php',
  121.                                  array('operator' => '<')),
  122.         'lte'           => array('HTML_QuickForm2_Rule_Compare',
  123.                                  'HTML/QuickForm2/Rule/Compare.php',
  124.                                  array('operator' => '<=')),
  125.         'gt'            => array('HTML_QuickForm2_Rule_Compare',
  126.                                  'HTML/QuickForm2/Rule/Compare.php',
  127.                                  array('operator' => '>')),
  128.         'gte'           => array('HTML_QuickForm2_Rule_Compare',
  129.                                  'HTML/QuickForm2/Rule/Compare.php',
  130.                                  array('operator' => '>=')),
  131.         'regex'         => array('HTML_QuickForm2_Rule_Regex',
  132.                                  'HTML/QuickForm2/Rule/Regex.php'),
  133.         'callback'      => array('HTML_QuickForm2_Rule_Callback',
  134.                                  'HTML/QuickForm2/Rule/Callback.php'),
  135.         'length'        => array('HTML_QuickForm2_Rule_Length',
  136.                                  'HTML/QuickForm2/Rule/Length.php'),
  137.         'minlength'     => array('HTML_QuickForm2_Rule_Length',
  138.                                  'HTML/QuickForm2/Rule/Length.php',
  139.                                  array('max' => 0)),
  140.         'maxlength'     => array('HTML_QuickForm2_Rule_Length',
  141.                                  'HTML/QuickForm2/Rule/Length.php',
  142.                                  array('min' => 0)),
  143.         'maxfilesize'   => array('HTML_QuickForm2_Rule_MaxFileSize',
  144.                                  'HTML/QuickForm2/Rule/MaxFileSize.php'),
  145.         'mimetype'      => array('HTML_QuickForm2_Rule_MimeType',
  146.                                  'HTML/QuickForm2/Rule/MimeType.php')
  147.     );
  148.  
  149.  
  150.    /**
  151.     * Checks whether the file exists in the include path
  152.     *
  153.     * @param    string  file name
  154.     * @return   bool
  155.     */
  156.     protected static function fileExists($fileName)
  157.     {
  158.         $fp = @fopen($fileName, 'r', true);
  159.         if (is_resource($fp)) {
  160.             fclose($fp);
  161.             return true;
  162.         }
  163.         return false;
  164.     }
  165.  
  166.    /**
  167.     * Tries to load a given class from a given file
  168.     *
  169.     * @param    string  Class name to load
  170.     * @param    string  Name of the file (supposedly) containing the given class
  171.     * @throws   HTML_QuickForm2_NotFoundException   If the file either can't be
  172.     *               loaded or doesn't contain the given class
  173.     */
  174.     protected static function loadClass($className, $includeFile)
  175.     {
  176.         if (empty($includeFile)) {
  177.             throw new HTML_QuickForm2_NotFoundException(
  178.                 "Class '$className' does not exist and no file to load"
  179.             );
  180.         } elseif (!self::fileExists($includeFile)) {
  181.             throw new HTML_QuickForm2_NotFoundException("File '$includeFile' was not found");
  182.         }
  183.         // Do not silence the errors with @, parse errors will not be seen
  184.         include_once $includeFile;
  185.         // Still no class?
  186.         if (!class_exists($className, false)) {
  187.             throw new HTML_QuickForm2_NotFoundException(
  188.                 "Class '$className' was not found within file '$includeFile'"
  189.             );
  190.         }
  191.     }
  192.  
  193.    /**
  194.     * Registers a new element type
  195.     *
  196.     * @param    string  Type name (treated case-insensitively)
  197.     * @param    string  Class name
  198.     * @param    string  File containing the class, leave empty if class already loaded
  199.     */
  200.     public static function registerElement($type, $className, $includeFile = null)
  201.     {
  202.         self::$elementTypes[strtolower($type)] = array($className, $includeFile);
  203.     }
  204.  
  205.  
  206.    /**
  207.     * Checks whether an element type is known to factory
  208.     *
  209.     * @param    string  Type name (treated case-insensitively)
  210.     * @return   bool
  211.     */
  212.     public static function isElementRegistered($type)
  213.     {
  214.         return isset(self::$elementTypes[strtolower($type)]);
  215.     }
  216.  
  217.  
  218.    /**
  219.     * Creates a new element object of the given type
  220.     *
  221.     * @param    string  Type name (treated case-insensitively)
  222.     * @param    mixed   Element name (passed to element's constructor)
  223.     * @param    mixed   Element attributes (passed to element's constructor)
  224.     * @param    array   Element-specific data (passed to element's constructor)
  225.     * @return   HTML_QuickForm2_Node     A created element
  226.     * @throws   HTML_QuickForm2_InvalidArgumentException If type name is unknown
  227.     * @throws   HTML_QuickForm2_NotFoundException If class for the element can
  228.     *           not be found and/or loaded from file
  229.     */
  230.     public static function createElement($type, $name = null, $attributes = null,
  231.                                          array $data = array())
  232.     {
  233.         $type = strtolower($type);
  234.         if (!isset(self::$elementTypes[$type])) {
  235.             throw new HTML_QuickForm2_InvalidArgumentException("Element type '$type' is not known");
  236.         }
  237.         list($className, $includeFile) = self::$elementTypes[$type];
  238.         if (!class_exists($className, false)) {
  239.             self::loadClass($className, $includeFile);
  240.         }
  241.         return new $className($name, $attributes, $data);
  242.     }
  243.  
  244.  
  245.    /**
  246.     * Registers a new rule type
  247.     *
  248.     * @param    string  Rule type name (treated case-insensitively)
  249.     * @param    string  Class name
  250.     * @param    string  File containing the class, leave empty if class already loaded
  251.     * @param    mixed   Configuration data for rules of the given type
  252.     */
  253.     public static function registerRule($type, $className, $includeFile = null,
  254.                                         $config = null)
  255.     {
  256.         self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config);
  257.     }
  258.  
  259.  
  260.    /**
  261.     * Returns configuration data for rules of the given type
  262.     *
  263.     * @param    string  Rule type name (treated case-insensitively)
  264.     * @return   mixed   Configuration data (set when registering the rule)
  265.     * @throws   HTML_QuickForm2_InvalidArgumentException If rule type is unknown
  266.     */
  267.     public static function getRuleConfig($type)
  268.     {
  269.         $type = strtolower($type);
  270.         if (!isset(self::$registeredRules[$type])) {
  271.             throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known");
  272.         } elseif (isset(self::$registeredRules[$type][2])) {
  273.             return self::$registeredRules[$type][2];
  274.         } else {
  275.             return null;
  276.         }
  277.     }
  278.  
  279.  
  280.    /**
  281.     * Checks whether a rule type is known to Factory
  282.     *
  283.     * @param    string  Rule type name (treated case-insensitively)
  284.     * @return   bool
  285.     */
  286.     public static function isRuleRegistered($type)
  287.     {
  288.         return isset(self::$registeredRules[strtolower($type)]);
  289.     }
  290.  
  291.  
  292.    /**
  293.     * Creates a new Rule of the given type
  294.     *
  295.     * @param    string                  Rule type name (treated case-insensitively)
  296.     * @param    HTML_QuickForm2_Node    Element to validate by the rule
  297.     * @param    string                  Message to display if validation fails
  298.     * @param    mixed                   Additional data for the rule
  299.     * @return   HTML_QuickForm2_Rule    A created Rule
  300.     * @throws   HTML_QuickForm2_InvalidArgumentException If rule type is unknown
  301.     * @throws   HTML_QuickForm2_NotFoundException        If class for the rule
  302.     *           can't be found and/or loaded from file
  303.     */
  304.     public static function createRule($type, HTML_QuickForm2_Node $owner,
  305.                                       $message = '', $options = null)
  306.     {
  307.         $type = strtolower($type);
  308.         if (!isset(self::$registeredRules[$type])) {
  309.             throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known");
  310.         }
  311.         list($className, $includeFile) = self::$registeredRules[$type];
  312.         if (!class_exists($className, false)) {
  313.             self::loadClass($className, $includeFile);
  314.         }
  315.         return new $className($owner, $message, $options, $type);
  316.     }
  317. }
  318. ?>
  319.