3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Validator
;
12 use Zend\Filter\Digits
as DigitsFilter
;
14 class Digits
extends AbstractValidator
16 const NOT_DIGITS
= 'notDigits';
17 const STRING_EMPTY
= 'digitsStringEmpty';
18 const INVALID
= 'digitsInvalid';
21 * Digits filter used for validation
23 * @var \Zend\Filter\Digits
25 protected static $filter = null;
28 * Validation failure message template definitions
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",
39 * Returns true if and only if $value only contains digit characters
41 * @param string $value
44 public function isValid($value)
46 if (!is_string($value) && !is_int($value) && !is_float($value)) {
47 $this->error(self
::INVALID
);
51 $this->setValue((string) $value);
53 if ('' === $this->getValue()) {
54 $this->error(self
::STRING_EMPTY
);
58 if (null === static::$filter) {
59 static::$filter = new DigitsFilter();
62 if ($this->getValue() !== static::$filter->filter($this->getValue())) {
63 $this->error(self
::NOT_DIGITS
);