|
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: Float.php 22668 2010-07-25 14:50:46Z thomas $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Validate_Abstract |
|
24 */ |
|
25 require_once 'Zend/Validate/Abstract.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_Locale_Format |
|
29 */ |
|
30 require_once 'Zend/Locale/Format.php'; |
|
31 |
|
32 /** |
|
33 * @category Zend |
|
34 * @package Zend_Validate |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Validate_Float extends Zend_Validate_Abstract |
|
39 { |
|
40 const INVALID = 'floatInvalid'; |
|
41 const NOT_FLOAT = 'notFloat'; |
|
42 |
|
43 /** |
|
44 * @var array |
|
45 */ |
|
46 protected $_messageTemplates = array( |
|
47 self::INVALID => "Invalid type given. String, integer or float expected", |
|
48 self::NOT_FLOAT => "'%value%' does not appear to be a float", |
|
49 ); |
|
50 |
|
51 protected $_locale; |
|
52 |
|
53 /** |
|
54 * Constructor for the float validator |
|
55 * |
|
56 * @param string|Zend_Config|Zend_Locale $locale |
|
57 */ |
|
58 public function __construct($locale = null) |
|
59 { |
|
60 if ($locale instanceof Zend_Config) { |
|
61 $locale = $locale->toArray(); |
|
62 } |
|
63 |
|
64 if (is_array($locale)) { |
|
65 if (array_key_exists('locale', $locale)) { |
|
66 $locale = $locale['locale']; |
|
67 } else { |
|
68 $locale = null; |
|
69 } |
|
70 } |
|
71 |
|
72 if (empty($locale)) { |
|
73 require_once 'Zend/Registry.php'; |
|
74 if (Zend_Registry::isRegistered('Zend_Locale')) { |
|
75 $locale = Zend_Registry::get('Zend_Locale'); |
|
76 } |
|
77 } |
|
78 |
|
79 $this->setLocale($locale); |
|
80 } |
|
81 |
|
82 /** |
|
83 * Returns the set locale |
|
84 */ |
|
85 public function getLocale() |
|
86 { |
|
87 return $this->_locale; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Sets the locale to use |
|
92 * |
|
93 * @param string|Zend_Locale $locale |
|
94 */ |
|
95 public function setLocale($locale = null) |
|
96 { |
|
97 require_once 'Zend/Locale.php'; |
|
98 $this->_locale = Zend_Locale::findLocale($locale); |
|
99 return $this; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Defined by Zend_Validate_Interface |
|
104 * |
|
105 * Returns true if and only if $value is a floating-point value |
|
106 * |
|
107 * @param string $value |
|
108 * @return boolean |
|
109 */ |
|
110 public function isValid($value) |
|
111 { |
|
112 if (!is_string($value) && !is_int($value) && !is_float($value)) { |
|
113 $this->_error(self::INVALID); |
|
114 return false; |
|
115 } |
|
116 |
|
117 if (is_float($value)) { |
|
118 return true; |
|
119 } |
|
120 |
|
121 $this->_setValue($value); |
|
122 try { |
|
123 if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) { |
|
124 $this->_error(self::NOT_FLOAT); |
|
125 return false; |
|
126 } |
|
127 } catch (Zend_Locale_Exception $e) { |
|
128 $this->_error(self::NOT_FLOAT); |
|
129 return false; |
|
130 } |
|
131 |
|
132 return true; |
|
133 } |
|
134 } |