-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathArrayLength.php
More file actions
executable file
·37 lines (31 loc) · 1.09 KB
/
ArrayLength.php
File metadata and controls
executable file
·37 lines (31 loc) · 1.09 KB
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
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule;
class ArrayLength extends AbstractRule
{
const OPTION_MIN = 'min';
const OPTION_MAX = 'max';
const MESSAGE = 'This input should contain between {min} and {max} items';
const LABELED_MESSAGE = '{label} should contain between {min} and {max} items';
protected array $optionsIndexMap = [
0 => self::OPTION_MIN,
1 => self::OPTION_MAX
];
public function validate(mixed $value, ?string $valueIdentifier = null): bool
{
$this->value = $value;
$maxValidator = new ArrayMaxLength();
if (isset($this->options['max'])) {
$maxValidator->setOption('max', $this->options['max']);
}
$minValidator = new ArrayMinLength();
if (isset($this->options['min'])) {
$minValidator->setOption('min', $this->options['min']);
}
$this->success = $minValidator->validate($value, $valueIdentifier) && $maxValidator->validate(
$value,
$valueIdentifier
);
return $this->success;
}
}