b2pweb /
bdf-form
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\Form\Leaf; |
||
| 4 | |||
| 5 | use TypeError; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Element for a float value |
||
| 9 | * |
||
| 10 | * @see FloatElementBuilder for build the element |
||
| 11 | * |
||
| 12 | * @extends LeafElement<float> |
||
| 13 | */ |
||
| 14 | class FloatElement extends LeafElement |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | * |
||
| 19 | * @return float|null |
||
| 20 | */ |
||
| 21 | 32 | protected function toPhp($httpValue): ?float |
|
| 22 | { |
||
| 23 | 32 | return $httpValue === null || $httpValue === '' ? null : (float) $httpValue; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | 9 | protected function toHttp($phpValue): ?string |
|
| 30 | { |
||
| 31 | 9 | return $phpValue === null ? null : (string) $phpValue; |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | * |
||
| 37 | * @return float|null |
||
| 38 | */ |
||
| 39 | 19 | protected function tryCast($value): ?float |
|
| 40 | { |
||
| 41 | 19 | if ($value === null) { |
|
| 42 | 1 | return null; |
|
| 43 | } |
||
| 44 | |||
| 45 | 18 | if (!is_numeric($value)) { |
|
| 46 | 7 | throw new TypeError('The import()\'ed value of a '.static::class.' must be numeric or null'); |
|
| 47 | } |
||
| 48 | |||
| 49 | 11 | return (float) $value; |
|
| 50 | } |
||
| 51 | } |
||
| 52 |