1<?php 2/** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10namespace Zend\Validator; 11 12use Zend\Filter\Digits as DigitsFilter; 13 14class Digits extends AbstractValidator 15{ 16 const NOT_DIGITS = 'notDigits'; 17 const STRING_EMPTY = 'digitsStringEmpty'; 18 const INVALID = 'digitsInvalid'; 19 20 /** 21 * Digits filter used for validation 22 * 23 * @var \Zend\Filter\Digits 24 */ 25 protected static $filter = null; 26 27 /** 28 * Validation failure message template definitions 29 * 30 * @var array 31 */ 32 protected $messageTemplates = array( 33 self::NOT_DIGITS => "The input must contain only digits", 34 self::STRING_EMPTY => "The input is an empty string", 35 self::INVALID => "Invalid type given. String, integer or float expected", 36 ); 37 38 /** 39 * Returns true if and only if $value only contains digit characters 40 * 41 * @param string $value 42 * @return bool 43 */ 44 public function isValid($value) 45 { 46 if (!is_string($value) && !is_int($value) && !is_float($value)) { 47 $this->error(self::INVALID); 48 return false; 49 } 50 51 $this->setValue((string) $value); 52 53 if ('' === $this->getValue()) { 54 $this->error(self::STRING_EMPTY); 55 return false; 56 } 57 58 if (null === static::$filter) { 59 static::$filter = new DigitsFilter(); 60 } 61 62 if ($this->getValue() !== static::$filter->filter($this->getValue())) { 63 $this->error(self::NOT_DIGITS); 64 return false; 65 } 66 67 return true; 68 } 69} 70