File indexing completed on 2024-05-12 06:03:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @see Zend_Locale_Format
0029  */
0030 // require_once 'Zend/Locale/Format.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Validate
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Validate_Float extends Zend_Validate_Abstract
0039 {
0040     const INVALID   = 'floatInvalid';
0041     const NOT_FLOAT = 'notFloat';
0042 
0043     /**
0044      * @var array
0045      */
0046     protected $_messageTemplates = array(
0047         self::INVALID   => "Invalid type given. String, integer or float expected",
0048         self::NOT_FLOAT => "'%value%' does not appear to be a float",
0049     );
0050 
0051     protected $_locale;
0052 
0053     /**
0054      * Constructor for the float validator
0055      *
0056      * @param string|Zend_Config|Zend_Locale $locale
0057      */
0058     public function __construct($locale = null)
0059     {
0060         if ($locale instanceof Zend_Config) {
0061             $locale = $locale->toArray();
0062         }
0063 
0064         if (is_array($locale)) {
0065             if (array_key_exists('locale', $locale)) {
0066                 $locale = $locale['locale'];
0067             } else {
0068                 $locale = null;
0069             }
0070         }
0071 
0072         if (empty($locale)) {
0073             // require_once 'Zend/Registry.php';
0074             if (Zend_Registry::isRegistered('Zend_Locale')) {
0075                 $locale = Zend_Registry::get('Zend_Locale');
0076             }
0077         }
0078 
0079         $this->setLocale($locale);
0080     }
0081 
0082     /**
0083      * Returns the set locale
0084      */
0085     public function getLocale()
0086     {
0087         return $this->_locale;
0088     }
0089 
0090     /**
0091      * Sets the locale to use
0092      *
0093      * @param string|Zend_Locale $locale
0094      * @return $this
0095      */
0096     public function setLocale($locale = null)
0097     {
0098         // require_once 'Zend/Locale.php';
0099         $this->_locale = Zend_Locale::findLocale($locale);
0100         return $this;
0101     }
0102 
0103     /**
0104      * Defined by Zend_Validate_Interface
0105      *
0106      * Returns true if and only if $value is a floating-point value
0107      *
0108      * @param  string $value
0109      * @return boolean
0110      */
0111     public function isValid($value)
0112     {
0113         if (!is_string($value) && !is_int($value) && !is_float($value)) {
0114             $this->_error(self::INVALID);
0115             return false;
0116         }
0117 
0118         if (is_float($value)) {
0119             return true;
0120         }
0121 
0122         $this->_setValue($value);
0123         try {
0124             if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
0125                 $this->_error(self::NOT_FLOAT);
0126                 return false;
0127             }
0128         } catch (Zend_Locale_Exception $e) {
0129             $this->_error(self::NOT_FLOAT);
0130             return false;
0131         }
0132 
0133         return true;
0134     }
0135 }