~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Validate/StringLength.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Zend Framework
 
5
 *
 
6
 * LICENSE
 
7
 *
 
8
 * This source file is subject to the new BSD license that is bundled
 
9
 * with this package in the file LICENSE.txt.
 
10
 * It is also available through the world-wide-web at this URL:
 
11
 * http://framework.zend.com/license/new-bsd
 
12
 * If you did not receive a copy of the license and are unable to
 
13
 * obtain it through the world-wide-web, please send an email
 
14
 * to license@zend.com so we can send you a copy immediately.
 
15
 *
 
16
 * @category   Zend
 
17
 * @package    Zend_Validate
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 * @version    $Id: StringLength.php 8064 2008-02-16 10:58:39Z thomas $
 
21
 */
 
22
 
 
23
 
 
24
/**
 
25
 * @see Zend_Validate_Abstract
 
26
 */
 
27
require_once 'Zend/Validate/Abstract.php';
 
28
 
 
29
 
 
30
/**
 
31
 * @category   Zend
 
32
 * @package    Zend_Validate
 
33
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
34
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
35
 */
 
36
class Zend_Validate_StringLength extends Zend_Validate_Abstract
 
37
{
 
38
 
 
39
    const TOO_SHORT = 'stringLengthTooShort';
 
40
    const TOO_LONG  = 'stringLengthTooLong';
 
41
 
 
42
    /**
 
43
     * @var array
 
44
     */
 
45
    protected $_messageTemplates = array(
 
46
        self::TOO_SHORT => "'%value%' is less than %min% characters long",
 
47
        self::TOO_LONG  => "'%value%' is greater than %max% characters long"
 
48
    );
 
49
 
 
50
    /**
 
51
     * @var array
 
52
     */
 
53
    protected $_messageVariables = array(
 
54
        'min' => '_min',
 
55
        'max' => '_max'
 
56
    );
 
57
 
 
58
    /**
 
59
     * Minimum length
 
60
     *
 
61
     * @var integer
 
62
     */
 
63
    protected $_min;
 
64
 
 
65
    /**
 
66
     * Maximum length
 
67
     *
 
68
     * If null, there is no maximum length
 
69
     *
 
70
     * @var integer|null
 
71
     */
 
72
    protected $_max;
 
73
 
 
74
    /**
 
75
     * Sets validator options
 
76
     *
 
77
     * @param  integer $min
 
78
     * @param  integer $max
 
79
     * @return void
 
80
     */
 
81
    public function __construct($min = 0, $max = null)
 
82
    {
 
83
        $this->setMin($min);
 
84
        $this->setMax($max);
 
85
    }
 
86
 
 
87
    /**
 
88
     * Returns the min option
 
89
     *
 
90
     * @return integer
 
91
     */
 
92
    public function getMin()
 
93
    {
 
94
        return $this->_min;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Sets the min option
 
99
     *
 
100
     * @param  integer $min
 
101
     * @throws Zend_Validate_Exception
 
102
     * @return Zend_Validate_StringLength Provides a fluent interface
 
103
     */
 
104
    public function setMin($min)
 
105
    {
 
106
        if (null !== $this->_max && $min > $this->_max) {
 
107
            /**
 
108
             * @see Zend_Validate_Exception
 
109
             */
 
110
            require_once 'Zend/Validate/Exception.php';
 
111
            throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
 
112
                                            . " $this->_max");
 
113
        }
 
114
        $this->_min = max(0, (integer) $min);
 
115
        return $this;
 
116
    }
 
117
 
 
118
    /**
 
119
     * Returns the max option
 
120
     *
 
121
     * @return integer|null
 
122
     */
 
123
    public function getMax()
 
124
    {
 
125
        return $this->_max;
 
126
    }
 
127
 
 
128
    /**
 
129
     * Sets the max option
 
130
     *
 
131
     * @param  integer|null $max
 
132
     * @throws Zend_Validate_Exception
 
133
     * @return Zend_Validate_StringLength Provides a fluent interface
 
134
     */
 
135
    public function setMax($max)
 
136
    {
 
137
        if (null === $max) {
 
138
            $this->_max = null;
 
139
        } else if ($max < $this->_min) {
 
140
            /**
 
141
             * @see Zend_Validate_Exception
 
142
             */
 
143
            require_once 'Zend/Validate/Exception.php';
 
144
            throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
 
145
                                            . "$max < $this->_min");
 
146
        } else {
 
147
            $this->_max = (integer) $max;
 
148
        }
 
149
 
 
150
        return $this;
 
151
    }
 
152
 
 
153
    /**
 
154
     * Defined by Zend_Validate_Interface
 
155
     *
 
156
     * Returns true if and only if the string length of $value is at least the min option and
 
157
     * no greater than the max option (when the max option is not null).
 
158
     *
 
159
     * @param  string $value
 
160
     * @return boolean
 
161
     */
 
162
    public function isValid($value)
 
163
    {
 
164
        $valueString = (string) $value;
 
165
        $this->_setValue($valueString);
 
166
        $length = iconv_strlen($valueString);
 
167
        if ($length < $this->_min) {
 
168
            $this->_error(self::TOO_SHORT);
 
169
        }
 
170
        if (null !== $this->_max && $this->_max < $length) {
 
171
            $this->_error(self::TOO_LONG);
 
172
        }
 
173
        if (count($this->_messages)) {
 
174
            return false;
 
175
        } else {
 
176
            return true;
 
177
        }
 
178
    }
 
179
 
 
180
}