• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / data-type / 10057590774

23 Jul 2024 10:53AM UTC coverage: 96.646% (-0.6%) from 97.239%
10057590774

push

github

h4kuna
add nullable

0 of 4 new or added lines in 4 files covered. (0.0%)

634 of 656 relevant lines covered (96.65%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/Basic/Floats.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\DataType\Basic;
4

5
use h4kuna\DataType;
6
use h4kuna\DataType\Exceptions\InvalidTypeException;
7
use Nette\StaticClass;
8
use Nette\Utils;
9

10
final class Floats
11
{
12
        use StaticClass;
13

14
        public static function nullable(mixed $value): ?float
15
        {
NEW
16
                return $value === null ? null : self::from($value);
×
17
        }
18

19

20
        public static function from(
1✔
21
                mixed $value,
22
                string $decimalPoint = ',',
23
                string $thousandSeparator = ' '
24
        ): float
25
        {
26
                if (is_numeric($value) || $value === '' || is_bool($value) || $value === null) {
1✔
27
                        return (float) $value;
1✔
28
                } elseif (is_array($value) || is_object($value)) {
1✔
29
                        throw InvalidTypeException::invalidFloat($value);
1✔
30
                }
31
                assert(is_string($value));
32

33
                if (Utils\Strings::match($value, '/^\d{1,2}:\d{1,2}(:\d{1,2})?$/') !== null) {
1✔
34
                        return self::fromHour($value);
1✔
35
                }
36

37
                $out = str_replace([$thousandSeparator, $decimalPoint], ['', '.'], $value);
1✔
38
                if (is_numeric($out)) {
1✔
39
                        return (float) $out;
1✔
40
                }
41

42
                throw InvalidTypeException::invalidFloat($value);
1✔
43
        }
44

45

46
        /**
47
         * Format HH:MM or HH:MM:SS
48
         */
49
        public static function fromHour(string $value): float
1✔
50
        {
51
                $minus = false;
1✔
52
                if (str_starts_with($value, '-')) {
1✔
53
                        $minus = true;
1✔
54
                        $value = substr($value, 1);
1✔
55
                }
56
                $out = 0.0;
1✔
57
                foreach (explode(':', $value) as $i => $v) {
1✔
58
                        $out += (Integer::from($v) / pow(60, $i));
1✔
59
                }
60

61
                return $minus ? $out * -1 : $out;
1✔
62
        }
63

64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc