This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Int.php
131 lines (112 loc) · 3.25 KB
/
Int.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/
namespace Zend\I18n\Validator;
use Locale;
use NumberFormatter;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* @category Zend
* @package Zend_I18n
* @subpackage Validator
*/
class Int extends AbstractValidator
{
const INVALID = 'intInvalid';
const NOT_INT = 'notInt';
/**
* @var array
*/
protected $messageTemplates = array(
self::INVALID => "Invalid type given. String or integer expected",
self::NOT_INT => "The input does not appear to be an integer",
);
/**
* Optional locale
*
* @var string|null
*/
protected $locale;
/**
* Constructor for the integer validator
*
* @param array|Traversable $options
*/
public function __construct($options = array())
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if (array_key_exists('locale', $options)) {
$this->setLocale($options['locale']);
}
parent::__construct($options);
}
/**
* Returns the set locale
*/
public function getLocale()
{
if (null === $this->locale) {
$this->locale = Locale::getDefault();
}
return $this->locale;
}
/**
* Sets the locale to use
*
* @param string $locale
* @return Int
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Returns true if and only if $value is a valid integer
*
* @param string|integer $value
* @return boolean
* @throws Exception\InvalidArgumentException
*/
public function isValid($value)
{
if (!is_string($value) && !is_int($value) && !is_float($value)) {
$this->error(self::INVALID);
return false;
}
if (is_int($value)) {
return true;
}
$this->setValue($value);
$locale = $this->getLocale();
$format = new NumberFormatter($locale, NumberFormatter::DECIMAL);
if (intl_is_failure($format->getErrorCode())) {
throw new Exception\InvalidArgumentException("Invalid locale string given");
}
$parsedInt = $format->parse($value, NumberFormatter::TYPE_INT64);
if (intl_is_failure($format->getErrorCode())) {
$this->error(self::NOT_INT);
return false;
}
$decimalSep = $format->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$groupingSep = $format->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
$valueFiltered = str_replace($groupingSep, '', $value);
$valueFiltered = str_replace($decimalSep, '.', $valueFiltered);
if (strval($parsedInt) !== $valueFiltered) {
$this->error(self::NOT_INT);
return false;
}
return true;
}
}