web/lib/Zend/Validate/Int.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Int.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_Int extends Zend_Validate_Abstract
       
    39 {
       
    40     const INVALID = 'intInvalid';
       
    41     const NOT_INT = 'notInt';
       
    42 
       
    43     /**
       
    44      * @var array
       
    45      */
       
    46     protected $_messageTemplates = array(
       
    47         self::INVALID => "Invalid type given. String or integer expected",
       
    48         self::NOT_INT => "'%value%' does not appear to be an integer",
       
    49     );
       
    50 
       
    51     protected $_locale;
       
    52 
       
    53     /**
       
    54      * Constructor for the integer 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         if ($locale !== null) {
       
    80             $this->setLocale($locale);
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * Returns the set locale
       
    86      */
       
    87     public function getLocale()
       
    88     {
       
    89         return $this->_locale;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Sets the locale to use
       
    94      *
       
    95      * @param string|Zend_Locale $locale
       
    96      */
       
    97     public function setLocale($locale = null)
       
    98     {
       
    99         require_once 'Zend/Locale.php';
       
   100         $this->_locale = Zend_Locale::findLocale($locale);
       
   101         return $this;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Defined by Zend_Validate_Interface
       
   106      *
       
   107      * Returns true if and only if $value is a valid integer
       
   108      *
       
   109      * @param  string|integer $value
       
   110      * @return boolean
       
   111      */
       
   112     public function isValid($value)
       
   113     {
       
   114         if (!is_string($value) && !is_int($value) && !is_float($value)) {
       
   115             $this->_error(self::INVALID);
       
   116             return false;
       
   117         }
       
   118 
       
   119         if (is_int($value)) {
       
   120             return true;
       
   121         }
       
   122 
       
   123         $this->_setValue($value);
       
   124         if ($this->_locale === null) {
       
   125             $locale        = localeconv();
       
   126             $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
       
   127             $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
       
   128 
       
   129             if (strval(intval($valueFiltered)) != $valueFiltered) {
       
   130                 $this->_error(self::NOT_INT);
       
   131                 return false;
       
   132             }
       
   133 
       
   134         } else {
       
   135             try {
       
   136                 if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) {
       
   137                     $this->_error(self::NOT_INT);
       
   138                     return false;
       
   139                 }
       
   140             } catch (Zend_Locale_Exception $e) {
       
   141                 $this->_error(self::NOT_INT);
       
   142                 return false;
       
   143             }
       
   144         }
       
   145 
       
   146         return true;
       
   147     }
       
   148 }