|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/validize project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Daikon\Validize\Validator; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\Assert; |
|
12
|
|
|
use Daikon\Interop\Assertion; |
|
13
|
|
|
use Daikon\ValueObject\TextList; |
|
14
|
|
|
|
|
15
|
|
|
final class TextListValidator extends Validator |
|
16
|
|
|
{ |
|
17
|
|
|
private const MAX_SIZE = 0; |
|
18
|
|
|
private const MIN_TEXT_LEN = 1; |
|
19
|
|
|
private const MAX_TEXT_LEN = 80; |
|
20
|
|
|
|
|
21
|
|
|
/** @param mixed $input */ |
|
22
|
|
|
protected function validate($input): TextList |
|
23
|
|
|
{ |
|
24
|
|
|
Assertion::isArray($input, 'Must be an array.'); |
|
25
|
|
|
|
|
26
|
|
|
$path = $this->getPath(); |
|
27
|
|
|
$settings = $this->getSettings(); |
|
28
|
|
|
$size = $settings['size'] ?? self::MAX_SIZE; |
|
29
|
|
|
$min = $settings['min'] ?? self::MIN_TEXT_LEN; |
|
30
|
|
|
$max = $settings['max'] ?? self::MAX_TEXT_LEN; |
|
31
|
|
|
|
|
32
|
|
|
if ($size > 0) { |
|
33
|
|
|
Assertion::maxCount($input, $size, "Must have at most $size items."); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$formatAssertion = Assert::lazy(); |
|
37
|
|
|
foreach ($input as $index => $item) { |
|
38
|
|
|
$formatAssertion |
|
39
|
|
|
->that($item, $path) |
|
40
|
|
|
->string($path."[$index] must be a string.") |
|
41
|
|
|
->betweenLength($min, $max, $path."[$index] must be between $min and $max characters."); |
|
42
|
|
|
} |
|
43
|
|
|
$formatAssertion->verifyNow(); |
|
44
|
|
|
|
|
45
|
|
|
return TextList::fromNative($input); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|