HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file Callback.php

Documentation is available at Callback.php

  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. {
  102.    /**
  103.     * Set to true if callback function was registered in Factory
  104.     * @var  bool 
  105.     */ 
  106.     protected $registeredCallback = false;
  107.  
  108.    /**
  109.     * Validates the element's value
  110.     * 
  111.     * @return   bool    the value returned by a callback function
  112.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  113.     *            was passed to constructor or a bogus callback was provided
  114.     * @throws   HTML_QuickForm2_Exception if the callback is missing
  115.     */
  116.     protected function checkValue($value)
  117.     {
  118.         if (!empty($this->registeredType)) {
  119.             $config HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  120.         else {
  121.             $config = null;
  122.         }
  123.         $callback  $this->findCallback($config);
  124.         $arguments $this->findArguments($config);
  125.         if (!is_callable($callbackfalse$callbackName)) {
  126.             throw new HTML_QuickForm2_InvalidArgumentException(
  127.                 'Callback Rule requires a valid callback, \'' $callbackName .
  128.                 '\' was given'
  129.             );
  130.         }
  131.         return call_user_func_array($callbackarray_merge(array($value)$arguments));
  132.     }
  133.  
  134.    /**
  135.     * Searches in global config and Rule's options for a callback function to use
  136.     *
  137.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  138.     *                    if applicable
  139.     * @return   callback 
  140.     * @throws   HTML_QuickForm2_Exception   if a callback wasn't found anywhere
  141.     */
  142.     protected function findCallback($globalConfig)
  143.     {
  144.         $this->registeredCallback = false;
  145.         if (!empty($globalConfig)) {
  146.             if (!is_array($globalConfig|| 
  147.                 !isset($globalConfig['callback']&& !isset($globalConfig['arguments']))
  148.             {
  149.                 $this->registeredCallback = true;
  150.                 return $globalConfig;
  151.             elseif (isset($globalConfig['callback'])) {
  152.                 $this->registeredCallback = true;
  153.                 return $globalConfig['callback'];
  154.             }
  155.         }
  156.         if (is_array($this->options&& isset($this->options['callback'])) {
  157.             return $this->options['callback'];
  158.         elseif (!empty($this->options)) {
  159.             return $this->options;
  160.         else {
  161.             throw new HTML_QuickForm2_Exception(
  162.                 'Callback Rule requires a callback to check value with'
  163.             );
  164.         }
  165.     }
  166.  
  167.    /**
  168.     * Searches in global config and Rule's options for callback's additional arguments
  169.     *
  170.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  171.     *                    if applicable
  172.     * @return   array   additional arguments to pass to a callback
  173.     */
  174.     protected function findArguments($globalConfig)
  175.     {
  176.         if (is_array($globalConfig&& isset($globalConfig['arguments'])) {
  177.             return $globalConfig['arguments'];
  178.         }
  179.         if (is_array($this->options&& isset($this->options['arguments'])) {
  180.             return $this->options['arguments'];
  181.         elseif ($this->registeredCallback && !empty($this->options)) {
  182.             return $this->options;
  183.         }
  184.         return array();
  185.     }
  186. }
  187. ?>

Documentation generated on Mon, 22 Oct 2007 12:30:10 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.