File indexing completed on 2024-12-22 05:37:12

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_Int extends Zend_Validate_Abstract
0039 {
0040     const INVALID = 'intInvalid';
0041     const NOT_INT = 'notInt';
0042 
0043     /**
0044      * @var array
0045      */
0046     protected $_messageTemplates = array(
0047         self::INVALID => "Invalid type given. String or integer expected",
0048         self::NOT_INT => "'%value%' does not appear to be an integer",
0049     );
0050 
0051     protected $_locale;
0052 
0053     /**
0054      * Constructor for the integer 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         if ($locale !== null) {
0080             $this->setLocale($locale);
0081         }
0082     }
0083 
0084     /**
0085      * Returns the set locale
0086      */
0087     public function getLocale()
0088     {
0089         return $this->_locale;
0090     }
0091 
0092     /**
0093      * Sets the locale to use
0094      *
0095      * @param string|Zend_Locale $locale
0096      * @return $this
0097      */
0098     public function setLocale($locale = null)
0099     {
0100         // require_once 'Zend/Locale.php';
0101         $this->_locale = Zend_Locale::findLocale($locale);
0102         return $this;
0103     }
0104 
0105     /**
0106      * Defined by Zend_Validate_Interface
0107      *
0108      * Returns true if and only if $value is a valid integer
0109      *
0110      * @param  string|integer $value
0111      * @return boolean
0112      */
0113     public function isValid($value)
0114     {
0115         if (!is_string($value) && !is_int($value) && !is_float($value)) {
0116             $this->_error(self::INVALID);
0117             return false;
0118         }
0119 
0120         if (is_int($value)) {
0121             return true;
0122         }
0123 
0124         $this->_setValue($value);
0125         if ($this->_locale === null) {
0126             $locale        = localeconv();
0127             $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
0128             $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
0129 
0130             if (strval(intval($valueFiltered)) != $valueFiltered) {
0131                 $this->_error(self::NOT_INT);
0132                 return false;
0133             }
0134 
0135         } else {
0136             try {
0137                 if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) {
0138                     $this->_error(self::NOT_INT);
0139                     return false;
0140                 }
0141             } catch (Zend_Locale_Exception $e) {
0142                 $this->_error(self::NOT_INT);
0143                 return false;
0144             }
0145         }
0146 
0147         return true;
0148     }
0149 }