• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / CLDR / 15865804447

25 Jun 2025 02:21AM UTC coverage: 95.929% (-1.3%) from 97.22%
15865804447

push

github

olvlvl
Add LanguageId and LocaleId

204 of 227 new or added lines in 18 files covered. (89.87%)

1296 of 1351 relevant lines covered (95.93%)

36.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/src/Repository.php
1
<?php
2

3
namespace ICanBoogie\CLDR;
4

5
use ICanBoogie\Accessor\AccessorTrait;
6
use ICanBoogie\CLDR\BCP47\BCP47;
7
use ICanBoogie\CLDR\Core\Locale;
8
use ICanBoogie\CLDR\Core\LocaleData;
9
use ICanBoogie\CLDR\Core\LocaleId;
10
use ICanBoogie\CLDR\General\Lists\ListFormatter;
11
use ICanBoogie\CLDR\General\Lists\ListPattern;
12
use ICanBoogie\CLDR\Numbers\CurrencyFormatter;
13
use ICanBoogie\CLDR\Numbers\NumberFormatter;
14
use ICanBoogie\CLDR\Numbers\NumberPattern;
15
use ICanBoogie\CLDR\Numbers\Symbols;
16
use ICanBoogie\CLDR\Provider\ResourceNotFound;
17
use ICanBoogie\CLDR\Supplemental\Plurals;
18
use ICanBoogie\CLDR\Supplemental\Supplemental;
19
use ICanBoogie\CLDR\Supplemental\Territory\Territory;
20
use ICanBoogie\CLDR\Supplemental\Territory\TerritoryCode;
21
use WeakMap;
22

23
use function array_shift;
24
use function explode;
25

26
/**
27
 * Representation of the CLDR.
28
 *
29
 * @property-read Supplemental $supplemental
30
 * @uses self::get_supplemental()
31
 * @property-read BCP47 $bcp47
32
 * @uses self::get_bcp47()
33
 * @property-read NumberFormatter $number_formatter
34
 * @uses self::get_number_formatter()
35
 * @property-read CurrencyFormatter $currency_formatter
36
 * @uses self::get_currency_formatter()
37
 * @property-read ListFormatter $list_formatter
38
 * @uses self::get_list_formatter()
39
 * @property-read Plurals $plurals
40
 * @uses self::get_plurals()
41
 *
42
 * @link https://github.com/unicode-org/cldr-json/tree/47.0.0
43
 */
44
final class Repository
45
{
46
    /**
47
     * @uses get_supplemental
48
     * @uses get_bcp47
49
     * @uses get_number_formatter
50
     * @uses get_currency_formatter
51
     * @uses get_list_formatter
52
     * @uses get_list_formatter
53
     * @uses get_plurals
54
     */
55
    use AccessorTrait;
56

57
    /**
58
     * @var array<string>
59
     */
60
    public array $available_locales;
61

62
    public function __construct(
63
        public readonly Provider $provider
64
    ) {
65
        $this->available_locales = LocaleData::AVAILABLE_LOCALES;
×
NEW
66
        $this->locales = new WeakMap();
×
NEW
67
        $this->territories = new WeakMap();
×
68
    }
69

70
    private Supplemental $supplemental;
71

72
    private function get_supplemental(): Supplemental
73
    {
74
        return $this->supplemental ??= new Supplemental($this);
53✔
75
    }
76

77
    private BCP47 $bcp47;
78

79
    private function get_bcp47(): BCP47
80
    {
81
        return $this->bcp47 ??= new BCP47($this);
1✔
82
    }
83

84
    private NumberFormatter $number_formatter;
85

86
    private function get_number_formatter(): NumberFormatter
87
    {
88
        return $this->number_formatter ??= new NumberFormatter();
6✔
89
    }
90

91
    private CurrencyFormatter $currency_formatter;
92

93
    private function get_currency_formatter(): CurrencyFormatter
94
    {
95
        return $this->currency_formatter ??= new CurrencyFormatter($this->get_number_formatter());
5✔
96
    }
97

98
    private ListFormatter $list_formatter;
99

100
    private function get_list_formatter(): ListFormatter
101
    {
102
        return $this->list_formatter ??= new ListFormatter();
5✔
103
    }
104

105
    private Plurals $plurals;
106

107
    private function get_plurals(): Plurals
108
    {
109
        return $this->plurals ??= new Plurals($this->get_supplemental()['plurals']);
28✔
110
    }
111

112
    /**
113
     * Fetches the data available at the specified path.
114
     *
115
     * @param string|null $data_path Path to the data to extract.
116
     *
117
     * @throws ResourceNotFound
118
     *
119
     * @phpstan-ignore-next-line
120
     */
121
    public function fetch(string $path, ?string $data_path = null): array
122
    {
123
        $data = $this->provider->provide($path);
96✔
124

125
        if ($data_path) {
96✔
126
            $data_path = explode('/', $data_path);
96✔
127

128
            while ($data_path) {
96✔
129
                $p = array_shift($data_path);
96✔
130
                $data = $data[$p];
96✔
131
            }
132
        }
133

134
        return $data;
96✔
135
    }
136

137
    /**
138
     * Format a number with the specified pattern.
139
     *
140
     * Note, if the pattern contains '%', the number will be multiplied by 100 first. If the
141
     * pattern contains '‰', the number will be multiplied by 1000.
142
     *
143
     * @param float|int|numeric-string $number
144
     *     The number to format.
145
     * @param string|NumberPattern $pattern
146
     *     The pattern used to format the number.
147
     */
148
    public function format_number(
149
        float|int|string $number,
150
        NumberPattern|string $pattern,
151
        ?Symbols $symbols = null,
152
    ): string {
153
        return $this->number_formatter->format($number, $pattern, $symbols);
1✔
154
    }
155

156
    /**
157
     * Format a number with the specified pattern.
158
     *
159
     * @param float|int|numeric-string $number
160
     *      The number to format.
161
     *
162
     * @see CurrencyFormatter::format()
163
     */
164
    public function format_currency(
165
        float|int|string $number,
166
        NumberPattern|string $pattern,
167
        ?Symbols $symbols = null,
168
        string $currencySymbol = CurrencyFormatter::DEFAULT_CURRENCY_SYMBOL
169
    ): string {
170
        return $this->currency_formatter->format($number, $pattern, $symbols, $currencySymbol);
1✔
171
    }
172

173
    /**
174
     * Formats variable-length lists of scalars.
175
     *
176
     * @param scalar[] $list
177
     *
178
     * @see ListFormatter::format()
179
     */
180
    public function format_list(array $list, ListPattern $list_pattern): string
181
    {
182
        return $this->list_formatter->format($list, $list_pattern);
1✔
183
    }
184

185
    /**
186
     * @var WeakMap<LocaleId, Locale>
187
     */
188
    private WeakMap $locales;
189

190
    /**
191
     * @param string|LocaleId $id
192
     *     A locale ID; for example, fr-BE.
193
     */
194
    public function locale_for(string|LocaleId $id): Locale
195
    {
196
        $id = LocaleId::from($id);
178✔
197

198
        return $this->locales[$id] ??= new Locale($this, $id);
177✔
199
    }
200

201
    /**
202
     * @var WeakMap<TerritoryCode, Territory>
203
     */
204
    private WeakMap $territories;
205

206
    /**
207
     * @param string|TerritoryCode $code
208
     *     A territory code; for example, CA.
209
     */
210
    public function territory_for(string|TerritoryCode $code): Territory
211
    {
212
        $code = TerritoryCode::of($code);
3✔
213

214
        return $this->territories[$code] ??= new Territory($this, $code);
2✔
215
    }
216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc