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 / Length.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.0 KB  |  222 lines

  1. <?php
  2. /**
  3.  * Rule checking the value's length
  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: Length.php,v 1.1 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's length
  53.  *
  54.  * The rule needs an "allowed length" parameter for its work, it can be either 
  55.  *  - a scalar: the value will be valid if it is exactly this long
  56.  *  - an array: the value will be valid if its length is between the given values
  57.  *    (inclusive). If one of these evaluates to 0, then length will be compared
  58.  *    only with the remaining one. 
  59.  *
  60.  * Parameters can be passed to {@link HTML_QuickForm2_Rule::setOptions() setOptions()} in
  61.  * either of the following formats
  62.  *  - scalar (if no parameters were registered with Factory then it is treated as
  63.  *    an exact length, if 'min' or 'max' was already registered then it is treated 
  64.  *    as 'max' or 'min', respectively)
  65.  *  - array(minlength, maxlength)
  66.  *  - array(['min' => minlength, ]['max' => maxlength])
  67.  * and also may be passed to {@link HTML_QuickForm2_Factory::registerRule()} in
  68.  * either of the following formats
  69.  *  - scalar (exact length)
  70.  *  - array(minlength, maxlength)
  71.  *  - array(['min' => minlength, ]['max' => maxlength])
  72.  * global config registered with the Factory overrides options set for the 
  73.  * particular Rule instance.
  74.  * 
  75.  * The Rule considers empty fields as valid and doesn't try to compare their
  76.  * lengths with provided limits.
  77.  *
  78.  * For convenience this Rule is also registered with the names 'minlength' and
  79.  * 'maxlength' (having, respectively, 'max' and 'min' parameters set to 0):
  80.  * <code>
  81.  * $password->addRule('minlength', 'The password should be at least 6 characters long', 6);
  82.  * $message->addRule('maxlength', 'Your message is too verbose', 1000);
  83.  * </code>
  84.  * 
  85.  * @category   HTML
  86.  * @package    HTML_QuickForm2
  87.  * @author     Alexey Borzov <avb@php.net>
  88.  * @author     Bertrand Mansion <golgote@mamasam.com>
  89.  * @version    Release: 0.2.0
  90.  */
  91. class HTML_QuickForm2_Rule_Length extends HTML_QuickForm2_Rule
  92. {
  93.    /**
  94.     * Validates the element's value
  95.     * 
  96.     * @return   bool    whether length of the element's value is within allowed range
  97.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  98.     *           was passed to constructor or bogus allowed length(s) were used
  99.     *           for rule configuration
  100.     * @throws   HTML_QuickForm2_Exception if rule configuration is missing
  101.     */
  102.     protected function checkValue($value)
  103.     {
  104.         if (!empty($this->registeredType)) {
  105.             $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  106.         } else {
  107.             $config = null;
  108.         }
  109.         $allowedLength = $this->findAllowedLength($config);
  110.  
  111.         if (0 == ($valueLength = strlen($value))) {
  112.             return true;
  113.         }
  114.         if (is_scalar($allowedLength)) {
  115.             return $valueLength == $allowedLength;
  116.         } else {
  117.             return (!empty($allowedLength['min'])? $valueLength >= $allowedLength['min']: true) &&
  118.                    (!empty($allowedLength['max'])? $valueLength <= $allowedLength['max']: true);
  119.         }
  120.     }
  121.  
  122.    /**
  123.     * Adds the 'min' and 'max' fields from one array to the other 
  124.     *
  125.     * @param    array   Rule configuration, array with 'min' and 'max' keys
  126.     * @param    array   Additional configuration, fields will be added to
  127.     *                   $length if it doesn't contain such a key already
  128.     * @return   array
  129.     */
  130.     protected function mergeMinMaxLength($length, $config)
  131.     {
  132.         if (array_key_exists('min', $config) || array_key_exists('max', $config)) {
  133.             if (!array_key_exists('min', $length) && array_key_exists('min', $config)) {
  134.                 $length['min'] = $config['min'];
  135.             }
  136.             if (!array_key_exists('max', $length) && array_key_exists('max', $config)) {
  137.                 $length['max'] = $config['max'];
  138.             }
  139.         } else {
  140.             if (!array_key_exists('min', $length)) {
  141.                 $length['min'] = reset($config);
  142.             }
  143.             if (!array_key_exists('max', $length)) {
  144.                 $length['max'] = end($config);
  145.             }
  146.         }
  147.         return $length;
  148.     } 
  149.  
  150.    /**
  151.     * Searches in global config and Rule's options for allowed length limits 
  152.     *
  153.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  154.     *                   if applicable
  155.     * @return   int|array
  156.     * @throws   HTML_QuickForm2_Exception   if length limits weren't found anywhere
  157.     * @throws   HTML_QuickForm2_InvalidArgumentException if bogus length limits
  158.     *           were provided
  159.     */
  160.     protected function findAllowedLength($globalConfig)
  161.     {
  162.         if (0 == count($globalConfig) && 0 == count($this->options)) {
  163.             throw new HTML_QuickForm2_Exception(
  164.                 'Length Rule requires an allowed length parameter'
  165.             );
  166.         }
  167.         if (!is_array($globalConfig)) {
  168.             $length = $globalConfig;
  169.         } else {
  170.             $length = $this->mergeMinMaxLength(array(), $globalConfig);
  171.         }
  172.  
  173.         if (is_array($this->options)) {
  174.             if (!isset($length)) {
  175.                 $length = $this->mergeMinMaxLength(array(), $this->options);
  176.             } else {
  177.                 $length = $this->mergeMinMaxLength($length, $this->options);
  178.             }
  179.  
  180.         } elseif (isset($this->options)) {
  181.             if (!isset($length)) {
  182.                 $length = $this->options;
  183.             } elseif (is_array($length)) {
  184.                 if (!array_key_exists('min', $length)) {
  185.                     $length['min'] = $this->options;
  186.                 } else {
  187.                     $length['max'] = $this->options;
  188.                 }
  189.             }
  190.         }
  191.  
  192.         if (is_array($length)) {
  193.             $length += array('min' => 0, 'max' => 0);
  194.         }
  195.         if (is_array($length) && ($length['min'] < 0 || $length['max'] < 0) ||
  196.             !is_array($length) && $length < 0)
  197.         {
  198.             throw new HTML_QuickForm2_InvalidArgumentException(
  199.                 'Length Rule requires parameters to be nonnegative, ' .
  200.                 preg_replace('/\s+/', ' ', var_export($length, true)) . ' given'
  201.             );
  202.         } elseif (is_array($length) && $length['min'] == 0 && $length['max'] == 0 ||
  203.                   !is_array($length) && 0 == $length)
  204.         {
  205.             throw new HTML_QuickForm2_InvalidArgumentException(
  206.                 'Length Rule requires at least one non-zero parameter, ' .
  207.                 preg_replace('/\s+/', ' ', var_export($length, true)) . ' given'
  208.             );
  209.         }
  210.  
  211.         if (!empty($length['min']) && !empty($length['max'])) {
  212.             if ($length['min'] > $length['max']) {
  213.                 list($length['min'], $length['max']) = array($length['max'], $length['min']);
  214.             } elseif ($length['min'] == $length['max']) {
  215.                 $length = $length['min'];
  216.             }
  217.         }
  218.         return $length;
  219.     }
  220. }
  221. ?>
  222.