-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIntegerValidator.php
51 lines (45 loc) · 1.07 KB
/
IntegerValidator.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
<?php declare(strict_types=1);
namespace MyENA\RGW\Validator;
use MyENA\RGW\Validator;
/**
* TODO: Currently does not support scientific notation
*
* Class IntegerValidator
* @package MyENA\RGW\Validator
*/
class IntegerValidator implements Validator
{
public const NAME = 'integer';
public const EXPECTS = 'integer or string containing only numbers with optional sign prefix';
/**
* @return string
*/
public function name(): string
{
return self::NAME;
}
/**
* @param mixed $value
* @return bool
*/
public function test($value): bool
{
if (null === $value) {
return false;
} elseif (is_int($value)) {
return true;
} elseif (!is_string($value)) {
return false;
} elseif ('-' === $value[0] || '+' === $value[0]) {
return ctype_digit(substr($value, 1));
}
return ctype_digit($value);
}
/**
* @return string
*/
public function expectedStatement(): string
{
return self::EXPECTS;
}
}