@@ 13-43 (lines=31) @@ | ||
10 | /** |
|
11 | * @author Karsten J. Gerber <[email protected]> |
|
12 | */ |
|
13 | class ToFloat implements UnaryFunction |
|
14 | { |
|
15 | /** @var float */ |
|
16 | private $default; |
|
17 | ||
18 | /** |
|
19 | * @param float $default |
|
20 | */ |
|
21 | public function __construct($default = 0.0) |
|
22 | { |
|
23 | $this->default = (float) $default; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param mixed $input |
|
28 | * |
|
29 | * @return float |
|
30 | */ |
|
31 | public function __invoke($input) |
|
32 | { |
|
33 | if (is_object($input) && method_exists($input, '__toString')) { |
|
34 | $input = (string) $input; |
|
35 | } |
|
36 | ||
37 | if (is_numeric($input)) { |
|
38 | return (float) (0 + $input); |
|
39 | } |
|
40 | ||
41 | return $this->default; |
|
42 | } |
|
43 | } |
|
44 |
@@ 13-45 (lines=33) @@ | ||
10 | /** |
|
11 | * @author Karsten J. Gerber <[email protected]> |
|
12 | */ |
|
13 | class ToInteger implements UnaryFunction |
|
14 | { |
|
15 | /** @var int */ |
|
16 | private $default; |
|
17 | ||
18 | /** |
|
19 | * ToInteger constructor. |
|
20 | * |
|
21 | * @param int $default |
|
22 | */ |
|
23 | public function __construct($default = 0) |
|
24 | { |
|
25 | $this->default = (int) $default; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param mixed $input |
|
30 | * |
|
31 | * @return int |
|
32 | */ |
|
33 | public function __invoke($input) |
|
34 | { |
|
35 | if (is_object($input) && method_exists($input, '__toString')) { |
|
36 | $input = (string) $input; |
|
37 | } |
|
38 | ||
39 | if (is_numeric($input)) { |
|
40 | return (int) (0 + $input); |
|
41 | } |
|
42 | ||
43 | return $this->default; |
|
44 | } |
|
45 | } |
|
46 |