Source for file Length.php
Documentation is available at Length.php
* Rule checking the value's length
* Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
* Bertrand Mansion <golgote@mamasam.com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Length.php,v 1.1 2007/10/13 16:18:22 avb Exp $
* @link http://pear.php.net/package/HTML_QuickForm2
* Base class for HTML_QuickForm2 rules
require_once 'HTML/QuickForm2/Rule.php';
* Rule checking the value's length
* The rule needs an "allowed length" parameter for its work, it can be either
* - a scalar: the value will be valid if it is exactly this long
* - an array: the value will be valid if its length is between the given values
* (inclusive). If one of these evaluates to 0, then length will be compared
* only with the remaining one.
* Parameters can be passed to {@link HTML_QuickForm2_Rule::setOptions() setOptions()} in
* either of the following formats
* - scalar (if no parameters were registered with Factory then it is treated as
* an exact length, if 'min' or 'max' was already registered then it is treated
* as 'max' or 'min', respectively)
* - array(minlength, maxlength)
* - array(['min' => minlength, ]['max' => maxlength])
* and also may be passed to {@link HTML_QuickForm2_Factory::registerRule()} in
* either of the following formats
* - scalar (exact length)
* - array(minlength, maxlength)
* - array(['min' => minlength, ]['max' => maxlength])
* global config registered with the Factory overrides options set for the
* particular Rule instance.
* The Rule considers empty fields as valid and doesn't try to compare their
* lengths with provided limits.
* For convenience this Rule is also registered with the names 'minlength' and
* 'maxlength' (having, respectively, 'max' and 'min' parameters set to 0):
* $password->addRule('minlength', 'The password should be at least 6 characters long', 6);
* $message->addRule('maxlength', 'Your message is too verbose', 1000);
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
* Validates the element's value
* @return bool whether length of the element's value is within allowed range
* @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType
* was passed to constructor or bogus allowed length(s) were used
* @throws HTML_QuickForm2_Exception if rule configuration is missing
if (0 == ($valueLength = strlen($value))) {
return $valueLength == $allowedLength;
return (!empty ($allowedLength['min'])? $valueLength >= $allowedLength['min']: true ) &&
(!empty ($allowedLength['max'])? $valueLength <= $allowedLength['max']: true );
* Adds the 'min' and 'max' fields from one array to the other
* @param array Rule configuration, array with 'min' and 'max' keys
* @param array Additional configuration, fields will be added to
* $length if it doesn't contain such a key already
$length['min'] = $config['min'];
$length['max'] = $config['max'];
$length['min'] = reset($config);
$length['max'] = end($config);
* Searches in global config and Rule's options for allowed length limits
* @param mixed config returned by {@link HTML_QuickForm2_Factory::getRuleConfig()},
* @throws HTML_QuickForm2_Exception if length limits weren't found anywhere
* @throws HTML_QuickForm2_InvalidArgumentException if bogus length limits
'Length Rule requires an allowed length parameter'
$length += array ('min' => 0 , 'max' => 0 );
if (is_array($length) && ($length['min'] < 0 || $length['max'] < 0 ) ||
'Length Rule requires parameters to be nonnegative, ' .
} elseif (is_array($length) && $length['min'] == 0 && $length['max'] == 0 ||
'Length Rule requires at least one non-zero parameter, ' .
if (!empty ($length['min']) && !empty ($length['max'])) {
if ($length['min'] > $length['max']) {
list ($length['min'], $length['max']) = array ($length['max'], $length['min']);
} elseif ($length['min'] == $length['max']) {
$length = $length['min'];
Documentation generated on Mon, 22 Oct 2007 12:30:20 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|