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

Source for file Compare.php

Documentation is available at Compare.php

  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. {
  91.    /**
  92.     * Possible comparison operators
  93.     * @var array 
  94.     */
  95.     protected $operators = array('==''!=''===''!==''<''<=''>''>=');
  96.  
  97.  
  98.    /**
  99.     * Validates the element's value
  100.     * 
  101.     * @return   bool    whether (element_value operator operand) expression is true
  102.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  103.     *            was passed to constructor or a bogus comparison operator is used
  104.     *            for configuration
  105.     * @throws   HTML_QuickForm2_Exception if an operand to compare with is missing
  106.     */
  107.     protected function checkValue($value)
  108.     {
  109.         if (!empty($this->registeredType)) {
  110.             $config HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  111.         else {
  112.             $config = null;
  113.         }
  114.         $operator $this->findOperator($config);
  115.         $operand  $this->findOperand($config);
  116.         if (!in_array($operatorarray('===''!=='))) {
  117.             $compareFn create_function('$a, $b''return floatval($a) ' $operator ' floatval($b);');
  118.         else {
  119.             $compareFn create_function('$a, $b''return strval($a) ' $operator ' strval($b);');
  120.         }
  121.         return $compareFn($value$operand instanceof HTML_QuickForm2_Node
  122.                                   $operand->getValue()$operand);
  123.     }
  124.  
  125.  
  126.    /**
  127.     * Finds a comparison operator to use in global config and Rule's options
  128.     *
  129.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  130.     *                    if applicable
  131.     * @return   string  operator to use, defaults to '==='
  132.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus comparison
  133.     *            operator is used for configuration
  134.     */
  135.     protected function findOperator($globalConfig)
  136.     {
  137.         if (!empty($globalConfig)) {
  138.             if (!is_array($globalConfig)) {
  139.                 $operator $globalConfig;
  140.             elseif (isset($globalConfig['operator'])) {
  141.                 $operator $globalConfig['operator'];
  142.             else {
  143.                 $operator array_shift($globalConfig);
  144.             }
  145.         }
  146.         if (empty($operator)) {
  147.             if (is_array($this->options&& isset($this->options['operator'])) {
  148.                 $operator $this->options['operator'];
  149.             elseif (!is_array($this->options|| count($this->options< 2{
  150.                 return '===';
  151.             else {
  152.                 reset($this->options);
  153.                 $operator current($this->options);
  154.             }
  155.         }
  156.         if (!in_array($operator$this->operators)) {
  157.             throw new HTML_QuickForm2_InvalidArgumentException(
  158.                 'Compare Rule requires a valid comparison operator, ' .
  159.                 preg_replace('/\s+/'' 'var_export($operatortrue)) ' given'
  160.             );
  161.         }
  162.         if (in_array($operatorarray('==''!='))) {
  163.             return $operator '=';
  164.         }
  165.         return $operator;
  166.     }
  167.  
  168.  
  169.    /**
  170.     * Finds an operand to compare element's value with in global config and Rule's options
  171.     *
  172.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  173.     *                    if applicable
  174.     * @return   mixed   an operand to compare with
  175.     * @throws   HTML_QuickForm2_Exception if an operand is missing
  176.     */
  177.     protected function findOperand($globalConfig)
  178.     {
  179.         if (count($globalConfig> 1{
  180.             if (isset($globalConfig['operand'])) {
  181.                 return $globalConfig['operand'];
  182.             else {
  183.                 return end($globalConfig);
  184.             }
  185.         }
  186.         if (0 == count($this->options)) {
  187.             throw new HTML_QuickForm2_Exception(
  188.                 'Compare Rule requires an argument to compare with'
  189.             );
  190.         elseif (!is_array($this->options)) {
  191.             return $this->options;
  192.         elseif (isset($this->options['operand'])) {
  193.             return $this->options['operand'];
  194.         else {
  195.             return end($this->options);
  196.         }
  197.     }
  198. }
  199. ?>

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