-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRepository.php
196 lines (169 loc) · 5.3 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace ICanBoogie\CLDR;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\CLDR\Core\Locale;
use ICanBoogie\CLDR\Core\LocaleData;
use ICanBoogie\CLDR\Core\LocaleId;
use ICanBoogie\CLDR\General\Lists\ListFormatter;
use ICanBoogie\CLDR\General\Lists\ListPattern;
use ICanBoogie\CLDR\Numbers\CurrencyFormatter;
use ICanBoogie\CLDR\Numbers\NumberFormatter;
use ICanBoogie\CLDR\Numbers\NumberPattern;
use ICanBoogie\CLDR\Numbers\Symbols;
use ICanBoogie\CLDR\Provider\ResourceNotFound;
use ICanBoogie\CLDR\Supplemental\Plurals;
use ICanBoogie\CLDR\Supplemental\Supplemental;
use ICanBoogie\CLDR\Supplemental\Territory\Territory;
use ICanBoogie\CLDR\Supplemental\Territory\TerritoryCode;
use function array_shift;
use function explode;
/**
* Representation of the CLDR.
*
* @property-read Supplemental $supplemental
* @uses self::get_supplemental()
* @property-read NumberFormatter $number_formatter
* @uses self::get_number_formatter()
* @property-read CurrencyFormatter $currency_formatter
* @uses self::get_currency_formatter()
* @property-read ListFormatter $list_formatter
* @uses self::get_list_formatter()
* @property-read Plurals $plurals
* @uses self::get_plurals()
*
* @link https://github.com/unicode-org/cldr-json/tree/45.0.0
*/
final class Repository
{
/**
* @uses get_supplemental
* @uses get_number_formatter
* @uses get_currency_formatter
* @uses get_list_formatter
* @uses get_list_formatter
* @uses get_plurals
*/
use AccessorTrait;
/**
* @var array<string>
*/
public array $available_locales;
public function __construct(
public readonly Provider $provider
) {
$this->available_locales = LocaleData::AVAILABLE_LOCALES;
}
private Supplemental $supplemental;
private function get_supplemental(): Supplemental
{
return $this->supplemental ??= new Supplemental($this);
}
private NumberFormatter $number_formatter;
private function get_number_formatter(): NumberFormatter
{
return $this->number_formatter ??= new NumberFormatter();
}
private CurrencyFormatter $currency_formatter;
private function get_currency_formatter(): CurrencyFormatter
{
return $this->currency_formatter ??= new CurrencyFormatter($this->get_number_formatter());
}
private ListFormatter $list_formatter;
private function get_list_formatter(): ListFormatter
{
return $this->list_formatter ??= new ListFormatter();
}
private Plurals $plurals;
private function get_plurals(): Plurals
{
return $this->plurals ??= new Plurals($this->get_supplemental()['plurals']);
}
/**
* Fetches the data available at the specified path.
*
* @param string|null $data_path Path to the data to extract.
*
* @throws ResourceNotFound
*
* @phpstan-ignore-next-line
*/
public function fetch(string $path, string $data_path = null): array
{
$data = $this->provider->provide($path);
if ($data_path) {
$data_path = explode('/', $data_path);
while ($data_path) {
$p = array_shift($data_path);
$data = $data[$p];
}
}
return $data;
}
/**
* Format a number with the specified pattern.
*
* Note, if the pattern contains '%', the number will be multiplied by 100 first. If the
* pattern contains '‰', the number will be multiplied by 1000.
*
* @param float|int|numeric-string $number
* The number to format.
* @param string|NumberPattern $pattern
* The pattern used to format the number.
*/
public function format_number(
float|int|string $number,
NumberPattern|string $pattern,
Symbols $symbols = null,
): string {
return $this->number_formatter->format($number, $pattern, $symbols);
}
/**
* Format a number with the specified pattern.
*
* @param float|int|numeric-string $number
* The number to format.
*
* @see CurrencyFormatter::format()
*/
public function format_currency(
float|int|string $number,
NumberPattern|string $pattern,
Symbols $symbols = null,
string $currencySymbol = CurrencyFormatter::DEFAULT_CURRENCY_SYMBOL
): string {
return $this->currency_formatter->format($number, $pattern, $symbols, $currencySymbol);
}
/**
* Formats variable-length lists of scalars.
*
* @param scalar[] $list
*
* @see ListFormatter::format()
*/
public function format_list(array $list, ListPattern $list_pattern): string
{
return $this->list_formatter->format($list, $list_pattern);
}
/**
* @param string|LocaleId $id
* A locale ID; for example, fr-BE.
*/
public function locale_for(string|LocaleId $id): Locale
{
if (!$id instanceof LocaleId) {
$id = LocaleId::of($id);
}
return new Locale($this, $id);
}
/**
* @param string|TerritoryCode $code
* A territory code; for example, CA.
*/
public function territory_for(string|TerritoryCode $code): Territory
{
if (!$code instanceof TerritoryCode) {
$code = TerritoryCode::of($code);
}
return new Territory($this, $code);
}
}