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 / QuickForm / Rule / Callback.php next >
Encoding:
PHP Script  |  2008-07-02  |  4.2 KB  |  124 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Validates values using callback functions or methods
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category    HTML
  16.  * @package     HTML_QuickForm
  17.  * @author      Bertrand Mansion <bmansion@mamasam.com>
  18.  * @copyright   2001-2007 The PHP Group
  19.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20.  * @version     CVS: $Id: Callback.php,v 1.8 2007/05/29 18:34:36 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_QuickForm
  22.  */
  23.  
  24. /**
  25.  * Abstract base class for QuickForm validation rules 
  26.  */
  27. require_once 'HTML/QuickForm/Rule.php';
  28.  
  29. /**
  30.  * Validates values using callback functions or methods
  31.  *
  32.  * @category    HTML
  33.  * @package     HTML_QuickForm
  34.  * @author      Bertrand Mansion <bmansion@mamasam.com>
  35.  * @version     Release: 3.2.10
  36.  * @since       3.2
  37.  */
  38. class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
  39. {
  40.     /**
  41.      * Array of callbacks
  42.      *
  43.      * Array is in the format:
  44.      * $_data['rulename'] = array('functionname', 'classname');
  45.      * If the callback is not a method, then the class name is not set.
  46.      *
  47.      * @var     array
  48.      * @access  private
  49.      */
  50.     var $_data = array();
  51.  
  52.    /**
  53.     * Whether to use BC mode for specific rules
  54.     * 
  55.     * Previous versions of QF passed element's name as a first parameter
  56.     * to validation functions, but not to validation methods. This behaviour
  57.     * is emulated if you are using 'function' as rule type when registering.
  58.     * 
  59.     * @var array
  60.     * @access private
  61.     */
  62.     var $_BCMode = array();
  63.  
  64.     /**
  65.      * Validates a value using a callback
  66.      *
  67.      * @param     string    $value      Value to be checked
  68.      * @param     mixed     $options    Options for callback
  69.      * @access    public
  70.      * @return    boolean   true if value is valid
  71.      */
  72.     function validate($value, $options = null)
  73.     {
  74.         if (isset($this->_data[$this->name])) {
  75.             $callback = $this->_data[$this->name];
  76.             if (isset($callback[1])) {
  77.                 return call_user_func(array($callback[1], $callback[0]), $value, $options);
  78.             } elseif ($this->_BCMode[$this->name]) {
  79.                 return $callback[0]('', $value, $options);
  80.             } else {
  81.                 return $callback[0]($value, $options);
  82.             }
  83.         } elseif (is_callable($options)) {
  84.             return call_user_func($options, $value);
  85.         } else {
  86.             return true;
  87.         }
  88.     } // end func validate
  89.  
  90.     /**
  91.      * Adds new callbacks to the callbacks list
  92.      *
  93.      * @param     string    $name       Name of rule
  94.      * @param     string    $callback   Name of function or method
  95.      * @param     string    $class      Name of class containing the method
  96.      * @param     bool      $BCMode     Backwards compatibility mode 
  97.      * @access    public
  98.      */
  99.     function addData($name, $callback, $class = null, $BCMode = false)
  100.     {
  101.         if (!empty($class)) {
  102.             $this->_data[$name] = array($callback, $class);
  103.         } else {
  104.             $this->_data[$name] = array($callback);
  105.         }
  106.         $this->_BCMode[$name] = $BCMode;
  107.     } // end func addData
  108.  
  109.  
  110.     function getValidationScript($options = null)
  111.     {
  112.         if (isset($this->_data[$this->name])) {
  113.             $callback = $this->_data[$this->name][0];
  114.             $params   = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
  115.                         (isset($options)? ", '{$options}'": '');
  116.         } else {
  117.             $callback = is_array($options)? $options[1]: $options;
  118.             $params   = '{jsVar}';
  119.         }
  120.         return array('', "{jsVar} != '' && !{$callback}({$params})");
  121.     } // end func getValidationScript
  122.  
  123. } // end class HTML_QuickForm_Rule_Callback
  124. ?>