@@ 20-54 (lines=35) @@ | ||
17 | * |
|
18 | * @return float Value casted into float or -1 |
|
19 | */ |
|
20 | public function mustBeFloat($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): float |
|
21 | { |
|
22 | if (is_numeric($value)) { |
|
23 | $value = (float) $value; |
|
24 | } |
|
25 | ||
26 | $this->validationEngine->validateFieldValue( |
|
27 | $parentValidator ?: $this, |
|
28 | function() use ($value, $propertyPath, $exceptionMessage) { |
|
29 | if (is_float($value)) { |
|
30 | return; |
|
31 | } |
|
32 | ||
33 | if (null === $exceptionMessage) { |
|
34 | $exceptionMessage = sprintf( |
|
35 | 'Value "%s" is not a float.', |
|
36 | $value |
|
37 | ); |
|
38 | } |
|
39 | ||
40 | throw new InvalidArgumentException( |
|
41 | $exceptionMessage, |
|
42 | 0, |
|
43 | $propertyPath, |
|
44 | $value |
|
45 | ); |
|
46 | } |
|
47 | ); |
|
48 | ||
49 | if (!is_float($value)) { |
|
50 | return -1; |
|
51 | } |
|
52 | ||
53 | return $value; |
|
54 | } |
|
55 | } |
|
56 |
@@ 35-69 (lines=35) @@ | ||
32 | * |
|
33 | * @return int Value casted into int or -1 |
|
34 | */ |
|
35 | public function mustBeInteger($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): int |
|
36 | { |
|
37 | if (is_numeric($value)) { |
|
38 | $value = (int) $value; |
|
39 | } |
|
40 | ||
41 | $this->validationEngine->validateFieldValue( |
|
42 | $parentValidator ?: $this, |
|
43 | function() use ($value, $propertyPath, $exceptionMessage) { |
|
44 | if (is_int($value)) { |
|
45 | return; |
|
46 | } |
|
47 | ||
48 | if (null === $exceptionMessage) { |
|
49 | $exceptionMessage = sprintf( |
|
50 | 'Value "%s" is not an integer.', |
|
51 | $value |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | throw new InvalidArgumentException( |
|
56 | $exceptionMessage, |
|
57 | 0, |
|
58 | $propertyPath, |
|
59 | $value |
|
60 | ); |
|
61 | } |
|
62 | ); |
|
63 | ||
64 | if (!is_int($value)) { |
|
65 | return -1; |
|
66 | } |
|
67 | ||
68 | return $value; |
|
69 | } |
|
70 | } |
|
71 |