ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FloatTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27
29{
32 private const REG_STRING = '/^\s*(-?(0|([1-9]\d*)))([.,]\d*)?\s*$/';
33 private const REG_STRING_FLOATING = '/^\s*-?\d+[eE]-?\d+\s*$/';
34
38 public function transform($from): float
39 {
40 if ($from !== INF && $from !== -INF && is_float($from) && !is_nan($from)) {
41 return $from;
42 }
43
44 if (is_int($from)) {
45 return (float) $from;
46 }
47
48 if (is_bool($from)) {
49 return (float) $from;
50 }
51
52 if (is_string($from)) {
53 $preg_match_string = preg_match(self::REG_STRING, $from, $RegMatch);
54 if ($preg_match_string) {
55 return (float) str_replace(',', '.', $from);
56 }
57
58 $preg_match_floating_string = preg_match(self::REG_STRING_FLOATING, $from, $RegMatch);
59 if ($preg_match_floating_string) {
60 return (float) $from;
61 }
62
64 sprintf('The value "%s" could not be transformed into an float', var_export($from, true)),
65 'not_float',
66 $from
67 );
68 }
69
71 sprintf('The value "%s" could not be transformed into an float', var_export($from, true)),
72 'not_float',
73 $from
74 );
75 }
76}
A transformation is a function from one datatype to another.