Passed
Push — main ( fe9415...2d1379 )
by Michael
47s queued 12s
created

LocaleNumberFormatter   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 58
ccs 9
cts 9
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Illuminate\Support\Collection;
8
use MichaelRubel\Formatters\Formatter;
9
use NumberFormatter;
10
11
class LocaleNumberFormatter implements Formatter
12
{
13
    /**
14
     * "Locale" key to pass to the collection of $items.
15
     *
16
     * @var string
17
     */
18
    public string $locale_key = 'locale';
19
20
    /**
21
     * "Number" key to pass to the collection of $items.
22
     *
23
     * @var string
24
     */
25
    public string $number_key = 'number';
26
27
    /**
28
     * "Style" key to pass to the collection of $items.
29
     *
30
     * @var string
31
     */
32
    public string $style_key = 'style';
33
34
    /**
35
     * Extendable fraction digits.
36
     *
37
     * @var int
38
     */
39
    public int $fraction_digits = 2;
40
41
    /**
42
     * Default number if $number_key isn't passed.
43
     *
44
     * @var float
45
     */
46
    public float $default_number = 0;
47
48
    /**
49
     * Format the date and time.
50
     *
51
     * @param Collection $items
52
     *
53
     * @return string
54
     */
55 2
    public function format(Collection $items): string
56
    {
57 2
        $formatter = new NumberFormatter(
58 2
            $items->get($this->locale_key) ?? app()->getLocale(),
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $items->get($this->locale_key) ?? app()->/** @scrutinizer ignore-call */ getLocale(),
Loading history...
59 2
            $items->get($this->style_key) ?? NumberFormatter::DECIMAL
60
        );
61
62 2
        $formatter->setAttribute(
63 2
            NumberFormatter::FRACTION_DIGITS,
64 2
            $this->fraction_digits
65
        );
66
67 2
        return $formatter->format(
68 2
            (float) $items->get($this->number_key) ?? $this->default_number
69
        );
70
    }
71
}
72