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 |
|
|
|
|
46
|
|
|
{ |
47
|
10 |
|
$this->formatter = new NumberFormatter($this->locale ?? app()->getLocale(), $this->style, $this->pattern); |
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.