forked from kornrunner/CLDR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.php
More file actions
190 lines (168 loc) · 4.21 KB
/
Repository.php
File metadata and controls
190 lines (168 loc) · 4.21 KB
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
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\CLDR\Locale\ListPattern;
use ICanBoogie\CLDR\Numbers\Symbols;
/**
* Representation of a CLDR.
*
* <pre>
* <?php
*
* namespace ICanBoogie\CLDR;
*
* $repository = new Repository($provider);
*
* var_dump($repository->locales['fr']);
* var_dump($repository->territories['FR']);
* </pre>
*
* @property-read Provider $provider
* @property-read LocaleCollection $locales
* @property-read Supplemental $supplemental
* @property-read TerritoryCollection $territories
* @property-read CurrencyCollection $currencies
* @property-read NumberFormatter $number_formatter
* @property-read CurrencyFormatter $currency_formatter
* @property-read ListFormatter $list_formatter
* @property-read Plurals $plurals
* @property-read array $available_locales
*
* @see http://www.unicode.org/repos/cldr-aux/json/24/
*/
#[\AllowDynamicProperties]
final class Repository
{
/**
* @uses get_provider
* @uses lazy_get_locales
* @uses lazy_get_supplemental
* @uses lazy_get_territories
* @uses lazy_get_currencies
* @uses lazy_get_number_formatter
* @uses lazy_get_currency_formatter
* @uses lazy_get_list_formatter
* @uses lazy_get_list_formatter
* @uses lazy_get_plurals
* @uses lazy_get_available_locales
*/
use AccessorTrait;
/**
* @var Provider
*/
private $provider;
public $locales;
private function get_provider(): Provider
{
return $this->provider;
}
private function lazy_get_locales(): LocaleCollection
{
return new LocaleCollection($this);
}
private function lazy_get_supplemental(): Supplemental
{
return new Supplemental($this);
}
private function lazy_get_territories(): TerritoryCollection
{
return new TerritoryCollection($this);
}
private function lazy_get_currencies(): CurrencyCollection
{
return new CurrencyCollection($this);
}
private function lazy_get_number_formatter(): NumberFormatter
{
return new NumberFormatter();
}
private function lazy_get_currency_formatter(): CurrencyFormatter
{
return new CurrencyFormatter();
}
private function lazy_get_list_formatter(): ListFormatter
{
return new ListFormatter();
}
private function lazy_get_plurals(): Plurals
{
return new Plurals($this->supplemental['plurals']);
}
/**
* @return string[]
*
* @throws ResourceNotFound
*/
private function lazy_get_available_locales(): array
{
return $this->fetch('availableLocales')['availableLocales']['modern'];
}
public function __construct(Provider $provider)
{
$this->provider = $provider;
$this->locales = new LocaleCollection($this);
}
/**
* Fetches the data available at the specified path.
*
* @return array<string, mixed>
*
* @throws ResourceNotFound
*/
public function fetch(string $path): array
{
return $this->provider->provide($path);
}
/**
* Format a number with the specified pattern.
*
* @param float|int $number The number to be formatted.
*
* @see NumberFormatter::format()
*/
public function format_number($number, string $pattern, ?Symbols $symbols = null): string
{
return $this->number_formatter->format($number, $pattern, $symbols);
}
/**
* Format a number with the specified pattern.
*
* @param float|int $number The number to be formatted.
*
* @see CurrencyFormatter::format()
*/
public function format_currency(
$number,
string $pattern,
?Symbols $symbols = null,
string $currencySymbol = CurrencyFormatter::DEFAULT_CURRENCY_SYMBOL
): string {
return $this->currency_formatter->format($number, $pattern, $symbols, $currencySymbol);
}
/**
* Formats a 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);
}
/**
* Whether a locale is available.
*/
public function is_locale_available(string $locale): bool
{
return in_array($locale, $this->available_locales);
}
}