source: trunk/library/Zend/Validate/Between.php @ 5146

Revision 5146, 5.6 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Library: adicionando arquivos.

Line 
1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category   Zend
16 * @package    Zend_Validate
17 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19 * @version    $Id: Between.php 20096 2010-01-06 02:05:09Z bkarwin $
20 */
21
22/**
23 * @see Zend_Validate_Abstract
24 */
25require_once 'Zend/Validate/Abstract.php';
26
27/**
28 * @category   Zend
29 * @package    Zend_Validate
30 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
32 */
33class Zend_Validate_Between extends Zend_Validate_Abstract
34{
35    /**
36     * Validation failure message key for when the value is not between the min and max, inclusively
37     */
38    const NOT_BETWEEN        = 'notBetween';
39
40    /**
41     * Validation failure message key for when the value is not strictly between the min and max
42     */
43    const NOT_BETWEEN_STRICT = 'notBetweenStrict';
44
45    /**
46     * Validation failure message template definitions
47     *
48     * @var array
49     */
50    protected $_messageTemplates = array(
51        self::NOT_BETWEEN        => "'%value%' is not between '%min%' and '%max%', inclusively",
52        self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'"
53    );
54
55    /**
56     * Additional variables available for validation failure messages
57     *
58     * @var array
59     */
60    protected $_messageVariables = array(
61        'min' => '_min',
62        'max' => '_max'
63    );
64
65    /**
66     * Minimum value
67     *
68     * @var mixed
69     */
70    protected $_min;
71
72    /**
73     * Maximum value
74     *
75     * @var mixed
76     */
77    protected $_max;
78
79    /**
80     * Whether to do inclusive comparisons, allowing equivalence to min and/or max
81     *
82     * If false, then strict comparisons are done, and the value may equal neither
83     * the min nor max options
84     *
85     * @var boolean
86     */
87    protected $_inclusive;
88
89    /**
90     * Sets validator options
91     * Accepts the following option keys:
92     *   'min' => scalar, minimum border
93     *   'max' => scalar, maximum border
94     *   'inclusive' => boolean, inclusive border values
95     *
96     * @param  array|Zend_Config $options
97     * @return void
98     */
99    public function __construct($options)
100    {
101        if ($options instanceof Zend_Config) {
102            $options = $options->toArray();
103        } else if (!is_array($options)) {
104            $options = func_get_args();
105            $temp['min'] = array_shift($options);
106            if (!empty($options)) {
107                $temp['max'] = array_shift($options);
108            }
109
110            if (!empty($options)) {
111                $temp['inclusive'] = array_shift($options);
112            }
113
114            $options = $temp;
115        }
116
117        if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) {
118            require_once 'Zend/Validate/Exception.php';
119            throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given");
120        }
121
122        if (!array_key_exists('inclusive', $options)) {
123            $options['inclusive'] = true;
124        }
125
126        $this->setMin($options['min'])
127             ->setMax($options['max'])
128             ->setInclusive($options['inclusive']);
129    }
130
131    /**
132     * Returns the min option
133     *
134     * @return mixed
135     */
136    public function getMin()
137    {
138        return $this->_min;
139    }
140
141    /**
142     * Sets the min option
143     *
144     * @param  mixed $min
145     * @return Zend_Validate_Between Provides a fluent interface
146     */
147    public function setMin($min)
148    {
149        $this->_min = $min;
150        return $this;
151    }
152
153    /**
154     * Returns the max option
155     *
156     * @return mixed
157     */
158    public function getMax()
159    {
160        return $this->_max;
161    }
162
163    /**
164     * Sets the max option
165     *
166     * @param  mixed $max
167     * @return Zend_Validate_Between Provides a fluent interface
168     */
169    public function setMax($max)
170    {
171        $this->_max = $max;
172        return $this;
173    }
174
175    /**
176     * Returns the inclusive option
177     *
178     * @return boolean
179     */
180    public function getInclusive()
181    {
182        return $this->_inclusive;
183    }
184
185    /**
186     * Sets the inclusive option
187     *
188     * @param  boolean $inclusive
189     * @return Zend_Validate_Between Provides a fluent interface
190     */
191    public function setInclusive($inclusive)
192    {
193        $this->_inclusive = $inclusive;
194        return $this;
195    }
196
197    /**
198     * Defined by Zend_Validate_Interface
199     *
200     * Returns true if and only if $value is between min and max options, inclusively
201     * if inclusive option is true.
202     *
203     * @param  mixed $value
204     * @return boolean
205     */
206    public function isValid($value)
207    {
208        $this->_setValue($value);
209
210        if ($this->_inclusive) {
211            if ($this->_min > $value || $value > $this->_max) {
212                $this->_error(self::NOT_BETWEEN);
213                return false;
214            }
215        } else {
216            if ($this->_min >= $value || $value >= $this->_max) {
217                $this->_error(self::NOT_BETWEEN_STRICT);
218                return false;
219            }
220        }
221        return true;
222    }
223
224}
Note: See TracBrowser for help on using the repository browser.