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

  1. <?php
  2. /**
  3.  * Rule checking the value via a callback function (method)
  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: Callback.php,v 1.2 2007/10/13 16:18:22 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Base class for HTML_QuickForm2 rules
  48.  */
  49. require_once 'HTML/QuickForm2/Rule.php';
  50.  
  51. /**
  52.  * Rule checking the value via a callback function (method)
  53.  *
  54.  * The Rule needs a valid callback as a configuration parameter for its work, it
  55.  * may also be given additional arguments to pass to the callback alongside the
  56.  * element's value.
  57.  *
  58.  * Parameters can be passed to {@link HTML_QuickForm2_Rule::setOptions() setOptions()} in
  59.  * either of the following formats
  60.  *  - callback or arguments (the semantics depend on whether the Rule was 
  61.  *    registered in the {@link HTML_QuickForm2_Factory Factory} with the
  62.  *    callback already given)
  63.  *  - array(['callback' => callback, ]['arguments' => array(...)])
  64.  * and also may be passed to {@link HTML_QuickForm2_Factory::registerRule()} in
  65.  * either of the following formats
  66.  *  - callback
  67.  *  - array(['callback' => callback, ]['arguments' => array(...)])
  68.  * global config registered with the Factory overrides options set for the 
  69.  * particular Rule instance. In any case you are advised to use the associative
  70.  * array format to prevent ambiguity.
  71.  *
  72.  * The callback will be called with element's value as the first argument, if
  73.  * additional arguments were provided they'll be passed as well. It is expected
  74.  * to return false if the value is invalid and true if it is valid.
  75.  *
  76.  * Checking that the value is not empty:
  77.  * <code>
  78.  * $str->addRule('callback', 'The field should not be empty', 'strlen'); 
  79.  * </code>
  80.  * Checking that the value is in the given array:
  81.  * <code>
  82.  * $meta->addRule('callback', 'Unknown variable name',
  83.  *                array('callback' => 'in_array',
  84.  *                      'arguments' => array(array('foo', 'bar', 'baz'))));
  85.  * </code>
  86.  * The same, but with rule registering first:
  87.  * <code>
  88.  * HTML_QuickForm2_Factory::registerRule(
  89.  *     'in_array', 'HTML_QuickForm2_Rule_Callback',
  90.  *     'HTML/QuickForm2/Rule/Callback.php', 'in_array'
  91.  * );
  92.  * $meta->addRule('in_array', 'Unknown variable name', array(array('foo', 'bar', 'baz')));
  93.  * </code>
  94.  *
  95.  * @category   HTML
  96.  * @package    HTML_QuickForm2
  97.  * @author     Alexey Borzov <avb@php.net>
  98.  * @author     Bertrand Mansion <golgote@mamasam.com>
  99.  * @version    Release: 0.2.0
  100.  */
  101. class HTML_QuickForm2_Rule_Callback extends HTML_QuickForm2_Rule
  102. {
  103.    /**
  104.     * Set to true if callback function was registered in Factory
  105.     * @var  bool
  106.     */ 
  107.     protected $registeredCallback = false;
  108.  
  109.    /**
  110.     * Validates the element's value
  111.     * 
  112.     * @return   bool    the value returned by a callback function
  113.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  114.     *           was passed to constructor or a bogus callback was provided
  115.     * @throws   HTML_QuickForm2_Exception if the callback is missing
  116.     */
  117.     protected function checkValue($value)
  118.     {
  119.         if (!empty($this->registeredType)) {
  120.             $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  121.         } else {
  122.             $config = null;
  123.         }
  124.         $callback  = $this->findCallback($config);
  125.         $arguments = $this->findArguments($config);
  126.         if (!is_callable($callback, false, $callbackName)) {
  127.             throw new HTML_QuickForm2_InvalidArgumentException(
  128.                 'Callback Rule requires a valid callback, \'' . $callbackName .
  129.                 '\' was given'
  130.             );
  131.         }
  132.         return call_user_func_array($callback, array_merge(array($value), $arguments));
  133.     }
  134.  
  135.    /**
  136.     * Searches in global config and Rule's options for a callback function to use 
  137.     *
  138.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  139.     *                   if applicable
  140.     * @return   callback
  141.     * @throws   HTML_QuickForm2_Exception   if a callback wasn't found anywhere
  142.     */
  143.     protected function findCallback($globalConfig)
  144.     {
  145.         $this->registeredCallback = false;
  146.         if (!empty($globalConfig)) {
  147.             if (!is_array($globalConfig) || 
  148.                 !isset($globalConfig['callback']) && !isset($globalConfig['arguments']))
  149.             {
  150.                 $this->registeredCallback = true;
  151.                 return $globalConfig;
  152.             } elseif (isset($globalConfig['callback'])) {
  153.                 $this->registeredCallback = true;
  154.                 return $globalConfig['callback'];
  155.             }
  156.         }
  157.         if (is_array($this->options) && isset($this->options['callback'])) {
  158.             return $this->options['callback'];
  159.         } elseif (!empty($this->options)) {
  160.             return $this->options;
  161.         } else {
  162.             throw new HTML_QuickForm2_Exception(
  163.                 'Callback Rule requires a callback to check value with'
  164.             );
  165.         }
  166.     }
  167.  
  168.    /**
  169.     * Searches in global config and Rule's options for callback's additional arguments  
  170.     *
  171.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  172.     *                   if applicable 
  173.     * @return   array   additional arguments to pass to a callback
  174.     */
  175.     protected function findArguments($globalConfig)
  176.     {
  177.         if (is_array($globalConfig) && isset($globalConfig['arguments'])) {
  178.             return $globalConfig['arguments'];
  179.         }
  180.         if (is_array($this->options) && isset($this->options['arguments'])) {
  181.             return $this->options['arguments'];
  182.         } elseif ($this->registeredCallback && !empty($this->options)) {
  183.             return $this->options;
  184.         }
  185.         return array();
  186.     }
  187. }
  188. ?>
  189.