Passed
Pull Request — main (#1)
by Michael
03:09
created

LocaleNumberFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
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
     * @var string
15
     */
16
    public string $locale_key = 'locale';
17
18
    /**
19
     * @var string
20
     */
21
    public string $number_key = 'number';
22
23
    /**
24
     * @var string
25
     */
26
    public string $style_key = 'style';
27
28
    /**
29
     * @var string
30
     */
31
    public string $pattern_key = 'pattern';
32
33
    /**
34
     * @var float
35
     */
36
    public float $default_number = 0;
37
38
    /**
39
     * Format the date and time.
40
     *
41
     * @param Collection $items
42
     *
43
     * @return string
44
     */
45
    public function format(Collection $items): string
46
    {
47
        $formatter = new NumberFormatter(
48
            $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

48
            $items->get($this->locale_key) ?? app()->/** @scrutinizer ignore-call */ getLocale(),
Loading history...
49
            $items->get($this->style_key) ?? NumberFormatter::DECIMAL,
50
            $items->get($this->pattern_key) ?? null
51
        );
52
53
        return $formatter->format(
54
            (float) $items->get($this->number_key) ?? $this->default_number
55
        );
56
    }
57
}
58