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

Source for file Length.php

Documentation is available at Length.php

  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. {
  92.    /**
  93.     * Validates the element's value
  94.     * 
  95.     * @return   bool    whether length of the element's value is within allowed range
  96.     * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
  97.     *            was passed to constructor or bogus allowed length(s) were used
  98.     *            for rule configuration
  99.     * @throws   HTML_QuickForm2_Exception if rule configuration is missing
  100.     */
  101.     protected function checkValue($value)
  102.     {
  103.         if (!empty($this->registeredType)) {
  104.             $config HTML_QuickForm2_Factory::getRuleConfig($this->registeredType);
  105.         else {
  106.             $config = null;
  107.         }
  108.         $allowedLength $this->findAllowedLength($config);
  109.  
  110.         if (0 == ($valueLength strlen($value))) {
  111.             return true;
  112.         }
  113.         if (is_scalar($allowedLength)) {
  114.             return $valueLength == $allowedLength;
  115.         else {
  116.             return (!empty($allowedLength['min'])$valueLength >= $allowedLength['min']: true&&
  117.                    (!empty($allowedLength['max'])$valueLength <= $allowedLength['max']: true);
  118.         }
  119.     }
  120.  
  121.    /**
  122.     * Adds the 'min' and 'max' fields from one array to the other
  123.     *
  124.     * @param    array   Rule configuration, array with 'min' and 'max' keys
  125.     * @param    array   Additional configuration, fields will be added to
  126.     *                    $length if it doesn't contain such a key already
  127.     * @return   array 
  128.     */
  129.     protected function mergeMinMaxLength($length$config)
  130.     {
  131.         if (array_key_exists('min'$config|| array_key_exists('max'$config)) {
  132.             if (!array_key_exists('min'$length&& array_key_exists('min'$config)) {
  133.                 $length['min'$config['min'];
  134.             }
  135.             if (!array_key_exists('max'$length&& array_key_exists('max'$config)) {
  136.                 $length['max'$config['max'];
  137.             }
  138.         else {
  139.             if (!array_key_exists('min'$length)) {
  140.                 $length['min'reset($config);
  141.             }
  142.             if (!array_key_exists('max'$length)) {
  143.                 $length['max'end($config);
  144.             }
  145.         }
  146.         return $length;
  147.     
  148.  
  149.    /**
  150.     * Searches in global config and Rule's options for allowed length limits
  151.     *
  152.     * @param    mixed   config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
  153.     *                    if applicable
  154.     * @return   int|array
  155.     * @throws   HTML_QuickForm2_Exception   if length limits weren't found anywhere
  156.     * @throws   HTML_QuickForm2_InvalidArgumentException if bogus length limits
  157.     *            were provided
  158.     */
  159.     protected function findAllowedLength($globalConfig)
  160.     {
  161.         if (0 == count($globalConfig&& 0 == count($this->options)) {
  162.             throw new HTML_QuickForm2_Exception(
  163.                 'Length Rule requires an allowed length parameter'
  164.             );
  165.         }
  166.         if (!is_array($globalConfig)) {
  167.             $length $globalConfig;
  168.         else {
  169.             $length $this->mergeMinMaxLength(array()$globalConfig);
  170.         }
  171.  
  172.         if (is_array($this->options)) {
  173.             if (!isset($length)) {
  174.                 $length $this->mergeMinMaxLength(array()$this->options);
  175.             else {
  176.                 $length $this->mergeMinMaxLength($length$this->options);
  177.             }
  178.  
  179.         elseif (isset($this->options)) {
  180.             if (!isset($length)) {
  181.                 $length $this->options;
  182.             elseif (is_array($length)) {
  183.                 if (!array_key_exists('min'$length)) {
  184.                     $length['min'$this->options;
  185.                 else {
  186.                     $length['max'$this->options;
  187.                 }
  188.             }
  189.         }
  190.  
  191.         if (is_array($length)) {
  192.             $length += array('min' => 0'max' => 0);
  193.         }
  194.         if (is_array($length&& ($length['min'< 0 || $length['max'< 0||
  195.             !is_array($length&& $length < 0)
  196.         {
  197.             throw new HTML_QuickForm2_InvalidArgumentException(
  198.                 'Length Rule requires parameters to be nonnegative, ' .
  199.                 preg_replace('/\s+/'' 'var_export($lengthtrue)) ' given'
  200.             );
  201.         elseif (is_array($length&& $length['min'== 0 && $length['max'== 0 ||
  202.                   !is_array($length&& 0 == $length)
  203.         {
  204.             throw new HTML_QuickForm2_InvalidArgumentException(
  205.                 'Length Rule requires at least one non-zero parameter, ' .
  206.                 preg_replace('/\s+/'' 'var_export($lengthtrue)) ' given'
  207.             );
  208.         }
  209.  
  210.         if (!empty($length['min']&& !empty($length['max'])) {
  211.             if ($length['min'$length['max']{
  212.                 list($length['min']$length['max']= array($length['max']$length['min']);
  213.             elseif ($length['min'== $length['max']{
  214.                 $length $length['min'];
  215.             }
  216.         }
  217.         return $length;
  218.     }
  219. }
  220. ?>

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