-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayOfInts.php
45 lines (36 loc) · 1.13 KB
/
ArrayOfInts.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
<?php
namespace App\Validator;
use Zend\Validator\AbstractValidator;
use Zend\I18n\Validator\IsInt;
/**
* Validates that a given value is an array of Integers.
*/
class ArrayOfInts extends AbstractValidator
{
const NOT_INTS = 'notInts';
const INVALID = 'arrayInvalid';
protected $messageTemplates = array(
self::NOT_INTS => "The array must contain only integers",
self::INVALID => "Invalid type given. Array of integers expected",
);
public function isValid($value)
{
$this->setValue($value);
if (!is_array($value)) {
$this->error(self::INVALID);
return false;
}
$isIntOptions = array();
if (isset($this->abstractOptions) && array_key_exists('locale', $this->abstractOptions)) {
$isIntOptions = array('locale' => $this->abstractOptions['locale']);
}
$validatorIsInt = new IsInt($isIntOptions);
foreach ($value as $oneValue) {
if (!$validatorIsInt->isValid($oneValue)) {
$this->error(self::NOT_INTS);
return false;
}
}
return true;
}
}