Currency::fraction_for()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Numbers;
4
5
use ICanBoogie\CLDR\Core\Locale;
6
use ICanBoogie\CLDR\Core\Localizable;
7
8
/**
9
 * Representation of a currency.
10
 *
11
 * @link https://www.unicode.org/reports/tr35/tr35-72/tr35-numbers.html#Currencies
12
 *
13
 * @implements Localizable<Currency, CurrencyLocalized>
14
 */
15
final class Currency implements Localizable
16
{
17
    /**
18
     * Whether a currency code is defined.
19
     *
20
     * @param string $code
21
     *     A currency code; for example, EUR.
22
     */
23
    public static function is_defined(string $code): bool
24
    {
25
        return in_array($code, CurrencyData::CODES);
26
    }
27
28
    /**
29
     * @param string $code
30
     *     A currency code; for example, EUR.
31
     *
32
     * @throws CurrencyNotDefined
33
     */
34
    public static function assert_is_defined(string $code): void
35
    {
36
        self::is_defined($code)
37
            or throw new CurrencyNotDefined($code);
38
    }
39
40
    /**
41
     * Returns a {@see CurrencyCode} of the specified code.
42
     *
43
     * @param string $code
44
     *     A currency code; for example, EUR.
45
     *
46
     * @throws CurrencyNotDefined
47
     */
48
    public static function of(string $code): self
49
    {
50
        static $instances;
51
52
        self::assert_is_defined($code);
53
54
        return $instances[$code] ??= new self($code, self::fraction_for($code));
55
    }
56
57
    /**
58
     * Returns the {@see Fraction} for the specified currency code.
59
     *
60
     * @param string $code
61
     * *     A currency code; for example, EUR.
62
     */
63
    private static function fraction_for(string $code): Fraction
64
    {
65
        static $default_fraction;
66
67
        $data = CurrencyData::FRACTIONS[$code] ?? null;
68
69
        if (!$data) {
70
            return $default_fraction ??= self::fraction_for(CurrencyData::FRACTIONS_FALLBACK);
71
        }
72
73
        return Fraction::from($data);
74
    }
75
76
    /**
77
     * @param string $code
78
     *     A currency code; for example, EUR.
79
    */
80
    private function __construct(
81
        public readonly string $code,
82
        public readonly Fraction $fraction,
83
    ) {
84
    }
85
86
    /**
87
     * Returns the {@see $code} of the currency.
88
     */
89
    public function __toString(): string
90
    {
91
        return $this->code;
92
    }
93
94
    public function __serialize(): array
95
    {
96
        return [ 'code' => $this->code ];
97
    }
98
99
    /**
100
     * @param array{ code: string } $data
0 ignored issues
show
Bug introduced by
The type ICanBoogie\CLDR\Numbers\code was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
     */
102
    public function __unserialize(array $data): void
103
    {
104
        $this->code = $data['code'];
0 ignored issues
show
Bug introduced by
The property code is declared read-only in ICanBoogie\CLDR\Numbers\Currency.
Loading history...
105
        $this->fraction = self::fraction_for($this->code);
0 ignored issues
show
Bug introduced by
The property fraction is declared read-only in ICanBoogie\CLDR\Numbers\Currency.
Loading history...
106
    }
107
108
    /**
109
     * Returns a localized currency.
110
     */
111
    public function localized(Locale $locale): CurrencyLocalized
112
    {
113
        return new CurrencyLocalized($this, $locale);
114
    }
115
}
116