Passed
Push — 6.0 ( e6360e...a5ddb0 )
by Olivier
01:38
created

LocalizedCurrency   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name_for() 0 11 3
A format() 0 5 1
A __construct() 0 8 1
1
<?php
2
3
namespace ICanBoogie\CLDR;
4
5
/**
6
 * A localized currency.
7
 *
8
 * @extends LocalizedObjectWithFormatter<Currency, LocalizedCurrencyFormatter>
9
 */
10
final class LocalizedCurrency extends LocalizedObjectWithFormatter
11
{
12
    /**
13
     * @var string The localized name of the currency.
14
     */
15
    public readonly string $name;
16
17
    /**
18
     * @var string The localized symbol of the currency.
19
     */
20
    public readonly string $symbol;
21
22
    public function __construct(object $target, Locale $locale)
23
    {
24
        $l = $locale['currencies'][$target->code];
25
        $this->name = $l['displayName'];
0 ignored issues
show
Bug introduced by
The property name is declared read-only in ICanBoogie\CLDR\LocalizedCurrency.
Loading history...
26
        // Not all currencies have a symbol, we default to the currency code in those cases.
27
        $this->symbol = $l['symbol'] ?? $target->code;
0 ignored issues
show
Bug introduced by
The property symbol is declared read-only in ICanBoogie\CLDR\LocalizedCurrency.
Loading history...
28
29
        parent::__construct($target, $locale, $locale->currency_formatter);
30
    }
31
32
    /**
33
     * Returns the localized name of the currency.
34
     *
35
     * @param int|null $count Used for pluralization.
36
     */
37
    public function name_for(int $count = null): string
38
    {
39
        $offset = 'displayName';
40
41
        if ($count == 1) {
42
            $offset .= '-count-one';
43
        } elseif ($count) {
0 ignored issues
show
Bug Best Practice introduced by
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 ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

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
Loading history...
44
            $offset .= '-count-other';
45
        }
46
47
        return $this->locale['currencies'][$this->target->code][$offset];
48
    }
49
50
    /**
51
     * Formats currency using locale's conventions.
52
     *
53
     * @param float|int|numeric-string $number
0 ignored issues
show
Documentation Bug introduced by
The doc comment float|int|numeric-string at position 4 could not be parsed: Unknown type name 'numeric-string' at position 4 in float|int|numeric-string.
Loading history...
54
     */
55
    public function format(
56
        float|int|string $number,
57
        string $pattern = LocalizedCurrencyFormatter::PATTERN_STANDARD
58
    ): string {
59
        return $this->formatter->format($number, $this->target, $pattern);
60
    }
61
}
62