1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic; |
4
|
|
|
|
5
|
|
|
use AmmitPhp\Ammit\Domain\StringValidation; |
6
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException; |
7
|
|
|
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine; |
8
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface; |
9
|
|
|
|
10
|
|
|
trait StringBetweenLengthValidatorTrait |
11
|
|
|
{ |
12
|
|
|
/** @var UIValidationEngine */ |
13
|
|
|
protected $validationEngine; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Domain should be responsible for id format |
17
|
|
|
* Exceptions are caught in order to be processed later |
18
|
|
|
* @param mixed $value String ? |
19
|
|
|
* |
20
|
|
|
* @return mixed Untouched value |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
public function mustHaveLengthBetween($value, int $min, int $max, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
23
|
|
|
{ |
24
|
|
|
$this->validationEngine->validateFieldValue( |
25
|
|
|
$parentValidator ?: $this, |
26
|
|
|
function() use ($value, $min, $max, $propertyPath, $exceptionMessage) { |
27
|
|
|
$stringValidation = new StringValidation(); |
28
|
|
|
if ($stringValidation->isStringBetweenValid($value, $min, $max)) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (null === $exceptionMessage) { |
33
|
|
|
$exceptionMessage = sprintf( |
|
|
|
|
34
|
|
|
'Value "%s" must have between %d and %d chars.', |
35
|
|
|
$value, |
36
|
|
|
$min, |
37
|
|
|
$max |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
throw new InvalidArgumentException( |
42
|
|
|
$exceptionMessage, |
43
|
|
|
0, |
44
|
|
|
$propertyPath, |
45
|
|
|
$value |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if (null === $value || !is_string($value)) { |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return (string) $value; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.