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

h4kuna / number-format / 6186467286

14 Sep 2023 01:51PM UTC coverage: 98.621% (-1.4%) from 100.0%
6186467286

push

github

h4kuna
Add __invoke for use in callback

13 of 13 new or added lines in 7 files covered. (100.0%)

286 of 290 relevant lines covered (98.62%)

0.99 hits per line

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

98.21
/src/Number/Formatters/NumberFormatter.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Format\Number\Formatters;
4

5
use h4kuna\Format\Number\Formatter;
6
use h4kuna\Format\Number\NumberFormat;
7
use h4kuna\Format\Number\Parameters\ZeroClear;
8
use h4kuna\Format\Number\Round;
9
use h4kuna\Format\Utils\Space;
10

11
class NumberFormatter implements Formatter
12
{
13
        /**
14
         * @var callable(float, int): float|null
15
         */
16
        public /* readonly */
17
                $roundCallback;
18

19
        private string $maskReplaced = '';
20

21

22
        /**
23
         * @param string $mask - Mask must contains number 1 and character ⎵ for dynamic unit.
24
         */
25
        public function __construct(
1✔
26
                public /* readonly */ int $decimals = 2,
27
                public /* readonly */ string $decimalPoint = ',',
28
                public /* readonly */ string $thousandsSeparator = ' ',
29
                public /* readonly */ bool $nbsp = true,
30
                public /* readonly */ int $zeroClear = ZeroClear::NO,
31
                public /* readonly */ string $emptyValue = Space::AS_NULL,
32
                public /* readonly */ bool $zeroIsEmpty = false,
33
                public /* readonly */ string $unit = '',
34
                public /* readonly */ bool $showUnitIfEmpty = true,
35
                public /* readonly */ string $mask = '1 ⎵',
36
                null|int|callable $round = null,
37
        )
38
        {
39
                $this->roundCallback = self::makeRoundCallback($round, $this->decimals);
1✔
40
                $this->initMaskReplaced();
1✔
41
                $this->initThousandsSeparator();
1✔
42
        }
1✔
43

44

45
        private static function makeRoundCallback(null|int|callable $round, int $decimals): ?callable
1✔
46
        {
47
                if ($decimals < 0 && $round === null) {
1✔
48
                        return Round::create();
1✔
49
                } elseif ($round === null) {
1✔
50
                        return null;
1✔
51
                } elseif (is_int($round)) {
1✔
52
                        return Round::create($round);
1✔
53
                }
54

55
                return $round;
1✔
56
        }
57

58

59
        private function initMaskReplaced(): void
60
        {
61
                if ($this->unit !== '' && $this->mask !== '') {
1✔
62
                        $replace = ['⎵' => $this->unit];
1✔
63
                        if ($this->nbsp) {
1✔
64
                                $replace[' '] = Space::NBSP;
1✔
65
                        }
66

67
                        $this->maskReplaced = strtr($this->mask, $replace);
1✔
68
                } else {
69
                        $this->maskReplaced = '';
1✔
70
                }
71
        }
1✔
72

73

74
        private function initThousandsSeparator(): void
75
        {
76
                if ($this->nbsp && str_contains($this->thousandsSeparator, ' ')) {
1✔
77
                        $this->thousandsSeparator = Space::nbsp($this->thousandsSeparator);
1✔
78
                } elseif ($this->nbsp === false && str_contains($this->thousandsSeparator, Space::NBSP)) {
1✔
79
                        $this->thousandsSeparator = Space::white($this->thousandsSeparator);
1✔
80
                }
81
        }
1✔
82

83

84
        public function modify(
1✔
85
                ?int $decimals = null,
86
                ?string $decimalPoint = null,
87
                ?string $thousandsSeparator = null,
88
                ?bool $nbsp = null,
89
                ?int $zeroClear = null,
90
                ?string $emptyValue = null,
91
                ?bool $zeroIsEmpty = null,
92
                ?string $unit = null,
93
                ?bool $showUnitIfEmpty = null,
94
                ?string $mask = null,
95
                null|int|callable $round = null,
96
        ): self
97
        {
98
                $that = clone $this;
1✔
99
                $that->decimals = $decimals ?? $this->decimals;
1✔
100
                $that->decimalPoint = $decimalPoint ?? $that->decimalPoint;
1✔
101
                $that->thousandsSeparator = $thousandsSeparator ?? $that->thousandsSeparator;
1✔
102
                $that->nbsp = $nbsp ?? $that->nbsp;
1✔
103
                $that->zeroClear = $zeroClear ?? $that->zeroClear;
1✔
104
                $that->emptyValue = $emptyValue ?? $that->emptyValue;
1✔
105
                $that->zeroIsEmpty = $zeroIsEmpty ?? $that->zeroIsEmpty;
1✔
106
                $that->showUnitIfEmpty = $showUnitIfEmpty ?? $that->showUnitIfEmpty;
1✔
107
                $that->roundCallback = $round === null ? $that->roundCallback : self::makeRoundCallback($round, $this->decimals);
1✔
108

109
                if ($mask !== null || $unit !== null) {
1✔
110
                        $that->mask = $mask ?? $that->mask;
1✔
111
                        $that->unit = $unit ?? $that->unit;
1✔
112
                }
113
                $that->initMaskReplaced();
1✔
114
                $that->initThousandsSeparator();
1✔
115

116
                return $that;
1✔
117
        }
118

119

120
        public function format(string|int|float|null $number): string
1✔
121
        {
122
                return NumberFormat::unit(
1✔
123
                        $number,
1✔
124
                        $this->decimals,
1✔
125
                        $this->decimalPoint,
1✔
126
                        $this->thousandsSeparator,
1✔
127
                        false,
1✔
128
                        $this->emptyValue,
1✔
129
                        $this->zeroIsEmpty,
1✔
130
                        $this->zeroClear,
1✔
131
                        $this->maskReplaced,
1✔
132
                        $this->showUnitIfEmpty,
1✔
133
                        $this->roundCallback,
1✔
134
                );
135
        }
136

137

138
        public function __invoke(float|int|string|null $number): string
139
        {
140
                return $this->format($number);
×
141
        }
142

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