Development

/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfNumberValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfNumberValidator.class.php

Revision 10270, 4.7 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfNumberValidator verifies a parameter is a number and allows you to apply
14  * size constraints.
15  *
16  * <b>Optional parameters:</b>
17  *
18  * # <b>max</b>        - [none]                  - Maximum number size.
19  * # <b>max_error</b>  - [Input is too large]    - An error message to use when
20  *                                                 input is too large.
21  * # <b>min</b>        - [none]                  - Minimum number size.
22  * # <b>min_error</b>  - [Input is too small]    - An error message to use when
23  *                                                 input is too small.
24  * # <b>nan_error</b>  - [Input is not a number] - Default error message when
25  *                                                 input is not a number.
26  * # <b>type</b>       - [Any]                   - Type of number (Any, Float).
27  * # <b>type_error</b> - [Input is not a number] - An error message to use when
28  *                                                 input is not a number.
29  *
30  * WARNING: This class is deprecated and will be removed in symfony 1.2.
31  *
32  * @package    symfony
33  * @subpackage validator
34  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
35  * @author     Sean Kerr <sean@code-box.org>
36  * @version    SVN: $Id$
37  * @deprecated Deprecated since symfony 1.1
38  */
39 class sfNumberValidator extends sfValidator
40 {
41   /**
42    * Executes this validator.
43    *
44    * @param mixed A file or parameter value/array
45    * @param error An error message reference
46    *
47    * @return bool true, if this validator executes successfully, otherwise false
48    */
49   public function execute(&$value, &$error)
50   {
51     if (!preg_match('/^-?\d+(\.\d+)?$/', $value))
52     {
53       // it's NaN, what nerve!
54       $error = $this->getParameterHolder()->get('nan_error');
55
56       return false;
57     }
58
59     $type = strtolower($this->getParameterHolder()->get('type'));
60
61     switch ($type)
62     {
63       case "decimal":
64       case "float":
65       {
66         if (substr_count($value, '.') != 1)
67         {
68           // value isn't a float, shazbot!
69           $error = $this->getParameterHolder()->get('type_error');
70           return false;
71         }
72
73         // cast our value to a float
74         $value = (float) $value;
75
76         break;
77       }
78
79       case "int":
80       case "integer":
81       {
82         // Note: (Both 3 AND 3.0 are BOTH considered integers and 3.1 is not)
83         if ((float) $value != (int) $value)
84         {
85           // is not an integer.
86           $error = $this->getParameterHolder()->get('type_error');
87           return false;
88         }
89
90         // cast our value to an integer
91         $value = (int) $value;
92
93         break;
94       }
95
96     }
97
98     $min = $this->getParameterHolder()->get('min');
99
100     if ($min !== null && $value < $min)
101     {
102       // too small
103       $error = $this->getParameterHolder()->get('min_error');
104
105       return false;
106     }
107
108     $max = $this->getParameterHolder()->get('max');
109
110     if ($max !== null && $value > $max)
111     {
112       // too large
113       $error = $this->getParameterHolder()->get('max_error');
114
115       return false;
116     }
117
118     return true;
119   }
120
121   /**
122    * Initializes this validator.
123    *
124    * @param sfContext The current application context
125    * @param array   An associative array of initialization parameters
126    *
127    * @return bool true, if initialization completes successfully, otherwise false
128    */
129   public function initialize($context, $parameters = null)
130   {
131     // initialize parent
132     parent::initialize($context);
133
134     // set defaults
135     $this->getParameterHolder()->set('max',        null);
136     $this->getParameterHolder()->set('max_error''Input is too large');
137     $this->getParameterHolder()->set('min',        null);
138     $this->getParameterHolder()->set('min_error''Input is too small');
139     $this->getParameterHolder()->set('nan_error''Input is not a number');
140     $this->getParameterHolder()->set('type',       'any');
141     $this->getParameterHolder()->set('type_error', 'Input is not a number');
142
143     $this->getParameterHolder()->add($parameters);
144
145     // check user-specified parameters
146     $type = strtolower($this->getParameterHolder()->get('type'));
147
148     // array of allowed types
149     $allowed_types = array('any', 'decimal', 'float', 'int', 'integer');
150
151     if (!in_array(strtolower($type), $allowed_types))
152     {
153       // unknown type
154       throw new sfValidatorException(sprintf('Unknown number type "%s" in NumberValidator.', $this->getParameterHolder()->get('type')));
155     }
156
157     return true;
158   }
159 }
160
Note: See TracBrowser for help on using the browser.