LocaleNumberFormatter::format()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 4
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
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 NumberFormatter
15
     */
16
    public NumberFormatter $formatter;
17
18
    /**
19
     * @param  int|float|string|null  $number
20
     * @param  string|null  $locale
21
     * @param  int  $style
22
     * @param  string|null  $pattern
23
     * @param  int  $fraction_digits
24
     * @param  string|null  $grouping_separator
25
     * @param  string|null  $decimal_separator
26
     */
27 10
    public function __construct(
28
        public int|float|string|null $number = null,
29
        public ?string $locale = null,
30
        public int $style = NumberFormatter::DECIMAL,
31
        public ?string $pattern = null,
32
        public int $fraction_digits = 2,
33
        public ?string $grouping_separator = null,
34
        public ?string $decimal_separator = null,
35
    ) {
36 10
    }
37
38
    /**
39
     * Format the number based on locale.
40
     *
41
     * @param  Collection  $items
42
     *
43
     * @return string|false
44
     */
45 10
    public function format(Collection $items): string|false
0 ignored issues
show
Unused Code introduced by
The parameter $items is not used and could be removed. ( Ignorable by Annotation )

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

45
    public function format(/** @scrutinizer ignore-unused */ Collection $items): string|false

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47 10
        $this->formatter = new NumberFormatter($this->locale ?? app()->getLocale(), $this->style, $this->pattern);
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

47
        $this->formatter = new NumberFormatter($this->locale ?? app()->/** @scrutinizer ignore-call */ getLocale(), $this->style, $this->pattern);
Loading history...
48
49 10
        $this->formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $this->fraction_digits);
50
51 10
        if ($this->grouping_separator) {
52 1
            $this->formatter->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $this->grouping_separator);
53
        }
54
55 10
        if ($this->decimal_separator) {
56 1
            $this->formatter->setSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $this->decimal_separator);
57
        }
58
59 10
        return $this->formatter->format(
60 10
            (float) $this->number
61 10
        );
62
    }
63
}
64