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  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_Digits extends Zend_Validate_Abstract
0034 {
0035     const NOT_DIGITS   = 'notDigits';
0036     const STRING_EMPTY = 'digitsStringEmpty';
0037     const INVALID      = 'digitsInvalid';
0038 
0039     /**
0040      * Digits filter used for validation
0041      *
0042      * @var Zend_Filter_Digits
0043      */
0044     protected static $_filter = null;
0045 
0046     /**
0047      * Validation failure message template definitions
0048      *
0049      * @var array
0050      */
0051     protected $_messageTemplates = array(
0052         self::NOT_DIGITS   => "'%value%' must contain only digits",
0053         self::STRING_EMPTY => "'%value%' is an empty string",
0054         self::INVALID      => "Invalid type given. String, integer or float expected",
0055     );
0056 
0057     /**
0058      * Defined by Zend_Validate_Interface
0059      *
0060      * Returns true if and only if $value only contains digit characters
0061      *
0062      * @param  string $value
0063      * @return boolean
0064      */
0065     public function isValid($value)
0066     {
0067         if (!is_string($value) && !is_int($value) && !is_float($value)) {
0068             $this->_error(self::INVALID);
0069             return false;
0070         }
0071 
0072         $this->_setValue((string) $value);
0073 
0074         if ('' === $this->_value) {
0075             $this->_error(self::STRING_EMPTY);
0076             return false;
0077         }
0078 
0079         if (null === self::$_filter) {
0080             // require_once 'Zend/Filter/Digits.php';
0081             self::$_filter = new Zend_Filter_Digits();
0082         }
0083 
0084         if ($this->_value !== self::$_filter->filter($this->_value)) {
0085             $this->_error(self::NOT_DIGITS);
0086             return false;
0087         }
0088 
0089         return true;
0090     }
0091 }