|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure; |
|
4
|
|
|
|
|
5
|
|
|
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException; |
|
6
|
|
|
use Imedia\Ammit\UI\Resolver\UIValidationEngine; |
|
7
|
|
|
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Guillaume MOREL <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
trait StringValidatorTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var UIValidationEngine */ |
|
15
|
|
|
protected $validationEngine; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Exceptions are caught in order to be processed later |
|
19
|
|
|
* @param mixed $value String ? |
|
20
|
|
|
* |
|
21
|
|
|
* @return string|null Casted to string or null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function mustBeStringOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (empty($value)) { |
|
26
|
|
|
return null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $this->mustBeString($value, $propertyPath, $parentValidator, $exceptionMessage); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Exceptions are caught in order to be processed later |
|
34
|
|
|
* @param mixed $value String ? |
|
35
|
|
|
* |
|
36
|
|
|
* @return string Casted to string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function mustBeString($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->validationEngine->validateFieldValue( |
|
41
|
1 |
|
$parentValidator ?: $this, |
|
42
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage) { |
|
43
|
1 |
|
if (is_string($value)) { |
|
44
|
1 |
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
if (null === $exceptionMessage) { |
|
48
|
|
|
$exceptionMessage = sprintf( |
|
|
|
|
|
|
49
|
|
|
'Value "%s" is not a string.', |
|
50
|
|
|
$value |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
throw new InvalidArgumentException( |
|
55
|
|
|
$exceptionMessage, |
|
56
|
1 |
|
0, |
|
57
|
|
|
$propertyPath, |
|
58
|
|
|
$value |
|
59
|
|
|
); |
|
60
|
1 |
|
} |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
1 |
|
if (null === $value || !is_string($value)) { |
|
64
|
1 |
|
return ''; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return (string) $value; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope