Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
FrmtrFloat | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFractionalDigits | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setFractionalDigits | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
createFormatter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
4 | */ |
5 | |
6 | declare(strict_types=1); |
7 | |
8 | namespace pvc\frmtr\numeric; |
9 | |
10 | use NumberFormatter; |
11 | use pvc\frmtr\err\InvalidMinMaxFractionalDigitException; |
12 | use pvc\interfaces\struct\range\RangeInterface; |
13 | |
14 | /** |
15 | * Class FrmtrFloat |
16 | */ |
17 | class FrmtrFloat extends FrmtrNumber |
18 | { |
19 | /** |
20 | * @param RangeInterface<int> $fractionalDigits |
21 | */ |
22 | public function __construct(protected RangeInterface $fractionalDigits) |
23 | { |
24 | /** |
25 | * these are the default values for Decimal NumberFormatter when you create it |
26 | */ |
27 | $this->fractionalDigits->setRange(0, 3); |
28 | } |
29 | |
30 | /** |
31 | * getFractionalDigits |
32 | * @return RangeInterface<int> |
33 | */ |
34 | public function getFractionalDigits(): RangeInterface |
35 | { |
36 | return $this->fractionalDigits; |
37 | } |
38 | |
39 | /** |
40 | * setMinFractionalDigits |
41 | * @param non-negative-int $minDigits |
42 | * @param non-negative-int $maxDigits |
43 | * @throws InvalidMinMaxFractionalDigitException |
44 | */ |
45 | |
46 | public function setFractionalDigits(int $minDigits, int $maxDigits): void |
47 | { |
48 | /** |
49 | * the theoretical limit is something really large like 2^32, so we are not going to error check it on the |
50 | * large side |
51 | */ |
52 | if ($maxDigits < 0 || $minDigits < 0) { |
53 | throw new InvalidMinMaxFractionalDigitException(); |
54 | } |
55 | $this->fractionalDigits->setRange($minDigits, $maxDigits); |
56 | } |
57 | |
58 | /** |
59 | * @function createFormatter |
60 | * @return NumberFormatter |
61 | */ |
62 | #[\Override] |
63 | protected function createFormatter(): NumberFormatter |
64 | { |
65 | $fractionalDigits = $this->fractionalDigits->getRange(); |
66 | |
67 | $formatter = parent::createFormatter(); |
68 | $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $fractionalDigits[0]); |
69 | $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $fractionalDigits[1]); |
70 | return $formatter; |
71 | } |
72 | } |