ILIAS  release_7 Revision v7.30-3-g800a261c036
FloatTransformation.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/* Copyright (c) 2020 Luka K. A. Stocker, Extended GPL, see docs/LICENSE */
4
6
11
13{
14 const REG_STRING = '/^\s*(-?(0|([1-9]\d*)))([.,]\d*)?\s*$/';
15 const REG_STRING_FLOATING = '/^\s*-?\d+[eE]-?\d+\s*$/';
16
19
23 public function transform($from)
24 {
25 if (is_float($from) && !is_nan($from) && $from !== INF && $from !== -INF) {
26 return $from;
27 }
28
29 if (is_int($from)) {
30 return (float) $from;
31 }
32
33 if (is_bool($from)) {
34 return floatval($from);
35 }
36
37 if (is_string($from)) {
38 $preg_match_string = preg_match(self::REG_STRING, $from, $RegMatch);
39 if ($preg_match_string) {
40 return floatval(str_replace(',', '.', $from));
41 }
42
43 $preg_match_floating_string = preg_match(self::REG_STRING_FLOATING, $from, $RegMatch);
44 if ($preg_match_floating_string) {
45 return floatval($from);
46 }
47
49 sprintf('The value "%s" could not be transformed into an float', $from),
50 'not_float',
51 $from
52 );
53 }
54
56 sprintf('The value "%s" could not be transformed into an float', $from),
57 'not_float',
58 $from
59 );
60 }
61}
An exception for terminatinating execution or to throw for unit testing.
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
A transformation is a function from one datatype to another.