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 / Compare.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.9 KB  |  201 lines

  1. <?php
  2. /**
  3.  * Rule comparing the value of the field with some other value
  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: Compare.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 comparing the value of the field with some other value
  53.  *
  54.  * The Rule needs two configuration parameters for its work
  55.  *  - comparison operator (defaults to equality)
  56.  *  - operand to compare with; this can be either a constant or another form 
  57.  *    element (its value will be used)
  58.  * 
  59.  * Parameters can be passed to {@link HTML_QuickForm2_Rule::setOptions() setOptions()} in
  60.  * either of the following formats
  61.  *  - operand
  62.  *  - array([operator, ]operand)
  63.  *  - array(['operator' => operator, ]['operand' => operand])
  64.  * and also may be passed to {@link HTML_QuickForm2_Factory::registerRule()} in
  65.  * either of the following formats
  66.  *  - operator
  67.  *  - array(operator[, operand])
  68.  *  - array(['operator' => operator, ]['operand' => operand])
  69.  * global config registered with the Factory overrides options set for the 
  70.  * particular Rule instance.
  71.  * 
  72.  * Note that 'less than [or equal]' and 'greater than [or equal]' operators
  73.  * compare the operands numerically, since this is considered as more useful
  74.  * approach by the authors.
  75.  * 
  76.  * For convenience, this Rule is already registered in the Factory with the 
  77.  * names 'eq', 'neq', 'lt', 'gt', 'lte', 'gte' corresponding to the relevant
  78.  * operators:
  79.  * <code>
  80.  * $password->addRule('eq', 'Passwords do not match', $passwordRepeat);
  81.  * $orderQty->addRule('lte', 'Should not order more than 10 of these', 10);
  82.  * </code>
  83.  *
  84.  * @category   HTML
  85.  * @package    HTML_QuickForm2
  86.  * @author     Alexey Borzov <avb@php.net>
  87.  * @author     Bertrand Mansion <golgote@mamasam.com>
  88.  * @version    Release: 0.2.0
  89.  */
  90. class HTML_QuickForm2_Rule_Compare extends HTML_QuickForm2_Rule
  91. {
  92.    /**
  93.     * Possible comparison operators
  94.     * @var array
  95.     */
  96.     protected $operators = array('==', '!=', '===', '!==', '<', '<=', '>', '>=');
  97.  
  98.  
  99.    /**
  100.     * Validates the element's value
  101.     * 
  102.     * @return   bool    whether (element_value operator operand) expression is true
  103.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  104.     *           was passed to constructor or a bogus comparison operator is used
  105.     *           for configuration
  106.     * @throws   HTML_QuickForm2_Exception if an operand to compare with is missing
  107.     */
  108.     protected function checkValue($value)
  109.     {
  110.         if (!empty($this->registeredType)) {
  111.             $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  112.         } else {
  113.             $config = null;
  114.         }
  115.         $operator = $this->findOperator($config);
  116.         $operand  = $this->findOperand($config);
  117.         if (!in_array($operator, array('===', '!=='))) {
  118.             $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
  119.         } else {
  120.             $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);');
  121.         }
  122.         return $compareFn($value, $operand instanceof HTML_QuickForm2_Node? 
  123.                                   $operand->getValue(): $operand);
  124.     }
  125.  
  126.  
  127.    /**
  128.     * Finds a comparison operator to use in global config and Rule's options
  129.     *
  130.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  131.     *                   if applicable 
  132.     * @return   string  operator to use, defaults to '==='
  133.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus comparison 
  134.     *           operator is used for configuration
  135.     */
  136.     protected function findOperator($globalConfig)
  137.     {
  138.         if (!empty($globalConfig)) {
  139.             if (!is_array($globalConfig)) {
  140.                 $operator = $globalConfig;
  141.             } elseif (isset($globalConfig['operator'])) {
  142.                 $operator = $globalConfig['operator'];
  143.             } else {
  144.                 $operator = array_shift($globalConfig);
  145.             }
  146.         }
  147.         if (empty($operator)) {
  148.             if (is_array($this->options) && isset($this->options['operator'])) {
  149.                 $operator = $this->options['operator'];
  150.             } elseif (!is_array($this->options) || count($this->options) < 2) {
  151.                 return '===';
  152.             } else {
  153.                 reset($this->options);
  154.                 $operator = current($this->options);
  155.             }
  156.         }
  157.         if (!in_array($operator, $this->operators)) {
  158.             throw new HTML_QuickForm2_InvalidArgumentException(
  159.                 'Compare Rule requires a valid comparison operator, ' .
  160.                 preg_replace('/\s+/', ' ', var_export($operator, true)) . ' given'
  161.             );
  162.         }
  163.         if (in_array($operator, array('==', '!='))) {
  164.             return $operator . '=';
  165.         }
  166.         return $operator;
  167.     }
  168.  
  169.  
  170.    /**
  171.     * Finds an operand to compare element's value with in global config and Rule's options
  172.     *
  173.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  174.     *                   if applicable 
  175.     * @return   mixed   an operand to compare with
  176.     * @throws   HTML_QuickForm2_Exception if an operand is missing
  177.     */
  178.     protected function findOperand($globalConfig)
  179.     {
  180.         if (count($globalConfig) > 1) {
  181.             if (isset($globalConfig['operand'])) {
  182.                 return $globalConfig['operand'];
  183.             } else {
  184.                 return end($globalConfig);
  185.             }
  186.         }
  187.         if (0 == count($this->options)) {
  188.             throw new HTML_QuickForm2_Exception(
  189.                 'Compare Rule requires an argument to compare with'
  190.             );
  191.         } elseif (!is_array($this->options)) {
  192.             return $this->options;
  193.         } elseif (isset($this->options['operand'])) {
  194.             return $this->options['operand'];
  195.         } else {
  196.             return end($this->options);
  197.         }
  198.     }
  199. }
  200. ?>
  201.