1 | <?php |
||||
2 | |||||
3 | namespace ICanBoogie\CLDR\Numbers; |
||||
4 | |||||
5 | use ICanBoogie\CLDR\Core\Locale; |
||||
6 | use ICanBoogie\CLDR\Core\LocalizedObjectWithFormatter; |
||||
7 | |||||
8 | /** |
||||
9 | * A localized currency. |
||||
10 | * |
||||
11 | * @extends LocalizedObjectWithFormatter<Currency, CurrencyFormatterLocalized> |
||||
12 | */ |
||||
13 | final class CurrencyLocalized extends LocalizedObjectWithFormatter |
||||
14 | { |
||||
15 | /** |
||||
16 | * @var string The localized name of the currency. |
||||
17 | */ |
||||
18 | public readonly string $name; |
||||
19 | |||||
20 | /** |
||||
21 | * @var string The localized symbol of the currency. |
||||
22 | */ |
||||
23 | public readonly string $symbol; |
||||
24 | |||||
25 | public function __construct(object $target, Locale $locale) |
||||
26 | { |
||||
27 | $l = $locale['currencies'][$target->code]; |
||||
28 | $this->name = $l['displayName']; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
29 | // Not all currencies have a symbol, we default to the currency code in those cases. |
||||
30 | $this->symbol = $l['symbol'] ?? $target->code; |
||||
0 ignored issues
–
show
|
|||||
31 | |||||
32 | parent::__construct($target, $locale, $locale->currency_formatter); |
||||
33 | } |
||||
34 | |||||
35 | /** |
||||
36 | * Returns the localized name of the currency. |
||||
37 | * |
||||
38 | * @param int|null $count Used for pluralization. |
||||
39 | */ |
||||
40 | public function name_for(?int $count = null): string |
||||
41 | { |
||||
42 | $offset = 'displayName'; |
||||
43 | |||||
44 | if ($count == 1) { |
||||
45 | $offset .= '-count-one'; |
||||
46 | } elseif ($count) { |
||||
0 ignored issues
–
show
The expression
$count of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||
47 | $offset .= '-count-other'; |
||||
48 | } |
||||
49 | |||||
50 | return $this->locale['currencies'][$this->target->code][$offset]; |
||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * Formats currency using locale's conventions. |
||||
55 | * |
||||
56 | * @param float|int|numeric-string $number |
||||
0 ignored issues
–
show
|
|||||
57 | */ |
||||
58 | public function format( |
||||
59 | float|int|string $number, |
||||
60 | string $pattern = CurrencyFormatterLocalized::PATTERN_STANDARD |
||||
61 | ): string { |
||||
62 | return $this->formatter->format($number, $this->target, $pattern); |
||||
0 ignored issues
–
show
The method
format() does not exist on ICanBoogie\CLDR\Core\Formatter . Since it exists in all sub-types, consider adding an abstract or default implementation to ICanBoogie\CLDR\Core\Formatter .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
63 | } |
||||
64 | } |
||||
65 |