Passed
Push — 6.0 ( 4ac4e1...87e1d7 )
by Olivier
01:56
created

CurrencyLocalized::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
The property name is declared read-only in ICanBoogie\CLDR\Numbers\CurrencyLocalized.
Loading history...
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
Bug introduced by
The property symbol is declared read-only in ICanBoogie\CLDR\Numbers\CurrencyLocalized.
Loading history...
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
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...
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
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...
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
Bug introduced by
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 ignore-call  annotation

62
        return $this->formatter->/** @scrutinizer ignore-call */ format($number, $this->target, $pattern);
Loading history...
63
    }
64
}
65