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

h4kuna / number-format / 8891877124

30 Apr 2024 08:41AM UTC coverage: 97.324% (-1.3%) from 98.625%
8891877124

push

github

h4kuna
Unit & Byte format improvement

4 of 8 new or added lines in 1 file covered. (50.0%)

291 of 299 relevant lines covered (97.32%)

0.97 hits per line

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

80.0
/src/Number/Units/UnitFormat.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Format\Number\Units;
4

5
use h4kuna\Format;
6

7
class UnitFormat
8
{
9
        private Format\Number\Formats $formats;
10

11

12
        public function __construct(
1✔
13
                private string $symbol,
14
                private Unit $unit,
15
                Format\Number\Formats|Format\Number\Formatter|null $formats = null,
16
        )
17
        {
18
                if ($formats === null) {
1✔
19
                        $formats = self::createFormats();
1✔
NEW
20
                } elseif ($formats instanceof Format\Number\Formatter) {
×
NEW
21
                        $formatter = $formats;
×
NEW
22
                        $formats = self::createFormats();
×
NEW
23
                        $formats->setDefault($formatter); // @phpstan-ignore-line
×
24
                }
25
                $this->formats = $formats;
1✔
26
        }
1✔
27

28

29
        public function convert(float $number, ?string $unitTo = null): string
1✔
30
        {
31
                return $this->convertFrom($number, null, $unitTo);
1✔
32
        }
33

34

35
        /**
36
         * @param string|null $unitFrom - null mean defined in constructor
37
         * @param string|null $unitTo - null mean automatic
38
         */
39
        public function convertFrom(float $number, ?string $unitFrom, ?string $unitTo = null): string
1✔
40
        {
41
                $unitValue = $this->unit->convertFrom($number, $unitFrom, $unitTo);
1✔
42

43
                return $this->format($unitValue->value, $unitValue->unit . $this->symbol);
1✔
44
        }
45

46

47
        private function format(float $value, string $unit): string
1✔
48
        {
49
                return $this->formats->get($unit)->format($value);
1✔
50
        }
51

52

53
        public function fromString(string $value, string $unitTo = Unit::BASE): string
1✔
54
        {
55
                $unitValue = $this->unit->fromString($value, $unitTo);
1✔
56

57
                return $this->format($unitValue->value, $unitValue->unit);
1✔
58
        }
59

60

61
        private static function createFormats(): Format\Number\Formats
62
        {
63
                return new Format\Number\Formats();
1✔
64
        }
65

66
}
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

© 2025 Coveralls, Inc