forked from Skillshare/formatphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberFormatOptions.php
More file actions
347 lines (310 loc) · 14.4 KB
/
NumberFormatOptions.php
File metadata and controls
347 lines (310 loc) · 14.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* This file is part of formatphp/formatphp
*
* formatphp/formatphp is open source software: you can distribute
* it and/or modify it under the terms of the MIT License
* (the "License"). You may not use this file except in
* compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com>
* @copyright Copyright (c) FormatPHP Contributors <https://formatphp.dev>
* @license https://opensource.org/licenses/MIT MIT License
*/
declare(strict_types=1);
namespace FormatPHP\Intl;
use FormatPHP\Icu\MessageFormat\Parser\Type\OptionSerializer;
use JsonSerializable;
/**
* @phpstan-type CompactDisplayType "short" | "long"
* @phpstan-type CurrencyDisplayType "symbol" | "code" | "name" | "narrowSymbol"
* @phpstan-type CurrencySignType "standard" | "accounting"
* @phpstan-type CurrencyType non-empty-string
* @phpstan-type DigitsType positive-int
* @phpstan-type FractionDigitsType DigitsType | 0
* @phpstan-type NotationType "standard" | "scientific" | "engineering" | "compact"
* @phpstan-type NumeralType "adlm" | "ahom" | "arab" | "arabext" | "bali" | "beng" | "bhks" | "brah" | "cakm" | "cham" | "deva" | "fullwide" | "gong" | "gonm" | "gujr" | "guru" | "hanidec" | "hmng" | "java" | "kali" | "khmr" | "knda" | "lana" | "lanatham" | "laoo" | "latn" | "lepc" | "limb" | "mathbold" | "mathdbl" | "mathmono" | "mathsanb" | "mathsans" | "mlym" | "modi" | "mong" | "mroo" | "mtei" | "mymr" | "mymrshan" | "mymrtlng" | "newa" | "nkoo" | "olck" | "orya" | "osma" | "rohg" | "saur" | "shrd" | "sind" | "sora" | "sund" | "takr" | "talu" | "tamldec" | "telu" | "thai" | "tibt" | "tirh" | "vaii" | "wara" | "wcho" | non-empty-string
* @phpstan-type RoundingModeType "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | "halfOdd" | "unnecessary"
* @phpstan-type RoundingPriorityType "auto" | "morePrecision" | "lessPrecision"
* @phpstan-type ScaleType float
* @phpstan-type SignDisplayType "auto" | "always" | "never" | "exceptZero" | "negative"
* @phpstan-type StyleType "decimal" | "percent" | "currency" | "unit"
* @phpstan-type TrailingZeroDisplayType "auto" | "stripIfInteger"
* @phpstan-type UnitDisplayType "short" | "long" | "narrow"
* @phpstan-type UnitType "acre" | "bit" | "byte" | "celsius" | "centimeter" | "day" | "degree" | "fahrenheit" | "fluid-ounce" | "foot" | "gallon" | "gigabit" | "gigabyte" | "gram" | "hectare" | "hour" | "inch" | "kilobit" | "kilobyte" | "kilogram" | "kilometer" | "liter" | "megabit" | "megabyte" | "meter" | "mile" | "mile-scandinavian" | "milliliter" | "millimeter" | "millisecond" | "minute" | "month" | "ounce" | "percent" | "petabyte" | "pound" | "second" | "stone" | "terabit" | "terabyte" | "week" | "yard" | "year" | non-empty-string
* @phpstan-type UseGroupingType "always" | "auto" | "false" | "min2" | "thousands" | "true"
* @phpstan-type OptionsType array{compactDisplay?: CompactDisplayType, currency?: CurrencyType, currencyDisplay?: CurrencyDisplayType, currencySign?: CurrencySignType, maximumFractionDigits?: FractionDigitsType, maximumSignificantDigits?: DigitsType, minimumFractionDigits?: FractionDigitsType, minimumIntegerDigits?: DigitsType, minimumSignificantDigits?: DigitsType, notation?: NotationType, numberingSystem?: NumeralType, roundingMode?: RoundingModeType, roundingPriority?: RoundingPriorityType, scale?: ScaleType, signDisplay?: SignDisplayType, style?: StyleType, trailingZeroDisplay?: TrailingZeroDisplayType, unit?: UnitType, unitDisplay?: UnitDisplayType, useGrouping?: UseGroupingType}
*/
class NumberFormatOptions implements JsonSerializable
{
use OptionSerializer;
public const STYLE_CURRENCY = 'currency';
public const STYLE_DECIMAL = 'decimal';
public const STYLE_PERCENT = 'percent';
public const STYLE_UNIT = 'unit';
public const CURRENCY_SIGN_ACCOUNTING = 'accounting';
public const CURRENCY_SIGN_STANDARD = 'standard';
public const CURRENCY_DISPLAY_CODE = 'code';
public const CURRENCY_DISPLAY_NAME = 'name';
public const CURRENCY_DISPLAY_NARROW_SYMBOL = 'narrowSymbol';
public const CURRENCY_DISPLAY_SYMBOL = 'symbol';
public const UNIT_DISPLAY_LONG = 'long';
public const UNIT_DISPLAY_NARROW = 'narrow';
public const UNIT_DISPLAY_SHORT = 'short';
public const NOTATION_COMPACT = 'compact';
public const NOTATION_ENGINEERING = 'engineering';
public const NOTATION_SCIENTIFIC = 'scientific';
public const NOTATION_STANDARD = 'standard';
public const SIGN_DISPLAY_ALWAYS = 'always';
public const SIGN_DISPLAY_AUTO = 'auto';
public const SIGN_DISPLAY_EXCEPT_ZERO = 'exceptZero';
public const SIGN_DISPLAY_NEVER = 'never';
public const SIGN_DISPLAY_NEGATIVE = 'negative';
public const COMPACT_DISPLAY_LONG = 'long';
public const COMPACT_DISPLAY_SHORT = 'short';
public const ROUNDING_PRIORITY_AUTO = 'auto';
public const ROUNDING_PRIORITY_LESS_PRECISION = 'lessPrecision';
public const ROUNDING_PRIORITY_MORE_PRECISION = 'morePrecision';
public const ROUNDING_MODE_CEIL = 'ceil';
public const ROUNDING_MODE_FLOOR = 'floor';
public const ROUNDING_MODE_EXPAND = 'expand';
public const ROUNDING_MODE_TRUNC = 'trunc';
public const ROUNDING_MODE_HALF_CEIL = 'halfCeil';
public const ROUNDING_MODE_HALF_FLOOR = 'halfFloor';
public const ROUNDING_MODE_HALF_EXPAND = 'halfExpand';
public const ROUNDING_MODE_HALF_TRUNC = 'halfTrunc';
public const ROUNDING_MODE_HALF_EVEN = 'halfEven';
public const ROUNDING_MODE_HALF_ODD = 'halfOdd';
public const ROUNDING_MODE_UNNECESSARY = 'unnecessary';
public const TRAILING_ZERO_DISPLAY_AUTO = 'auto';
public const TRAILING_ZERO_DISPLAY_STRIP_IF_INTEGER = 'stripIfInteger';
public const USE_GROUPING_ALWAYS = 'always';
public const USE_GROUPING_AUTO = 'auto';
public const USE_GROUPING_FALSE = 'false';
public const USE_GROUPING_MIN2 = 'min2';
public const USE_GROUPING_THOUSANDS = 'thousands';
public const USE_GROUPING_TRUE = 'true';
/**
* Only used when `notation` is "compact".
*
* Takes either "short" (default) or "long".
*
* @var CompactDisplayType | null
*/
public ?string $compactDisplay = null;
/**
* The currency to use in currency formatting.
*
* Possible values are the ISO 4217 currency codes, such as "USD" for the US
* dollar, "EUR" for the euro, or "CNY" for the Chinese RMB — see the current
* currency & funds code list.
*
* There is no default value; if the style is "currency", the currency
* property must be provided.
*
* @link https://www.currency-iso.org/en/home/tables/table-a1.html current currency & funds code list
*
* @var CurrencyType | null
*/
public ?string $currency = null;
/**
* How to display the currency in currency formatting.
*
* @var CurrencyDisplayType | null
*/
public ?string $currencyDisplay = null;
/**
* In many locales, accounting format means to wrap the number with
* parentheses instead of appending a minus sign. You can enable this
* formatting by setting the currencySign option to "accounting". The
* default value is "standard".
*
* @var CurrencySignType | null
*/
public ?string $currencySign = null;
/**
* The maximum number of fraction digits to use.
*
* Possible values are from 0 to 20; the default for plain number formatting
* is the larger of minimumFractionDigits and 3; the default for currency
* formatting is the larger of minimumFractionDigits and the number of minor
* unit digits provided by the ISO 4217 currency code list (2 if the list
* doesn't provide that information); the default for percent formatting is
* the larger of minimumFractionDigits and 0.
*
* @link https://www.currency-iso.org/en/home/tables/table-a1.html ISO 4217 currency code list
*
* @var FractionDigitsType | null
*/
public ?int $maximumFractionDigits = null;
/**
* The maximum number of significant digits to use.
*
* Possible values are from 1 to 21; the default is 21.
*
* @var DigitsType | null
*/
public ?int $maximumSignificantDigits = null;
/**
* The minimum number of fraction digits to use.
*
* Possible values are from 0 to 20; the default for plain number and percent
* formatting is 0; the default for currency formatting is the number of
* minor unit digits provided by the ISO 4217 currency code list (2 if the
* list doesn't provide that information).
*
* @var FractionDigitsType | null
*/
public ?int $minimumFractionDigits = null;
/**
* The minimum number of integer digits to use.
*
* Possible values are from 1 to 21; the default is 1.
*
* @var DigitsType | null
*/
public ?int $minimumIntegerDigits = null;
/**
* The minimum number of significant digits to use.
*
* Possible values are from 1 to 21; the default is 1.
*
* @var DigitsType | null
*/
public ?int $minimumSignificantDigits = null;
/**
* The formatting that should be displayed for the number.
*
* The default is "standard".
*
* @var NotationType | null
*/
public ?string $notation = null;
/**
* Numbering system.
*
* @var NumeralType | null
*/
public ?string $numberingSystem = null;
/**
* @var RoundingModeType | null
*/
public ?string $roundingMode = null;
/**
* An indication declaring how to resolve conflicts between maximum fraction
* digits and maximum significant digits.
*
* There are two modes, "morePrecision" and "lessPrecision":
*
* - "morePrecision", or relaxed, means to relax one of the two constraints
* (fraction digits or significant digits) in order to round the number to
* a higher level of precision.
* - "lessPrecision", or strict, means to enforce both constraints, resulting
* in the number being rounded to a lower level of precision.
*
* The default settings for compact notation rounding are Max-Fraction = 0
* (round to the nearest integer), Max-Significant = 2 (round to 2
* significant digits), and priority RELAXED (choose the constraint that
* results in more digits being displayed).
*
* Conflicting *minimum* fraction and significant digits are always resolved
* in the direction that results in more trailing zeros.
*
* @link https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/number/NumberFormatter.RoundingPriority.html
*
* @var RoundingPriorityType | null
*/
public ?string $roundingPriority = null;
/**
* The scale is a decimal number by which to multiply before formatting.
*
* For example, if scale is 100, then the number being formatted will be
* multiplied by 100 before formatting.
*
* @var ScaleType | null
*/
public ?float $scale = null;
/**
* When to display the sign for the number.
*
* This defaults to "auto".
*
* @var SignDisplayType | null
*/
public ?string $signDisplay = null;
/**
* The formatting style to use.
*
* The default is "decimal".
*
* @var StyleType | null
*/
public ?string $style = null;
/**
* @var TrailingZeroDisplayType | null
*/
public ?string $trailingZeroDisplay = null;
/**
* The unit to use in unit formatting.
*
* Possible values are core unit identifiers, defined in UTS #35, Part 2,
* Section 6. A subset of units from the full list was selected for use in
* ECMAScript. Pairs of simple units can be concatenated with "-per-" to
* make a compound unit. There is no default value; if the style is "unit",
* the unit property must be provided.
*
* @link https://unicode.org/reports/tr35/tr35-general.html#Unit_Elements UTS #3, Part 2, Section 6
* @link https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier ECMAScript subset of CLDR units
* @link https://github.com/unicode-org/cldr/blob/master/common/validity/unit.xml Full list of CLDR units
*
* @var UnitType | null
*/
public ?string $unit = null;
/**
* The unit formatting style to use in unit formatting.
*
* The default is "short".
*
* @var UnitDisplayType | null
*/
public ?string $unitDisplay = null;
/**
* Whether to use grouping separators, such as thousands separators or
* thousand/lakh/crore separators.
*
* @var UseGroupingType | null
*/
public ?string $useGrouping = null;
/**
* @param OptionsType $options
*/
public function __construct(array $options = [])
{
$this->compactDisplay = $options['compactDisplay'] ?? null;
$this->currency = $options['currency'] ?? null;
$this->currencyDisplay = $options['currencyDisplay'] ?? null;
$this->currencySign = $options['currencySign'] ?? null;
$this->maximumFractionDigits = $options['maximumFractionDigits'] ?? null;
$this->maximumSignificantDigits = $options['maximumSignificantDigits'] ?? null;
$this->minimumFractionDigits = $options['minimumFractionDigits'] ?? null;
$this->minimumIntegerDigits = $options['minimumIntegerDigits'] ?? null;
$this->minimumSignificantDigits = $options['minimumSignificantDigits'] ?? null;
$this->notation = $options['notation'] ?? null;
$this->numberingSystem = $options['numberingSystem'] ?? null;
$this->roundingMode = $options['roundingMode'] ?? null;
$this->roundingPriority = $options['roundingPriority'] ?? null;
$this->scale = $options['scale'] ?? null;
$this->signDisplay = $options['signDisplay'] ?? null;
$this->style = $options['style'] ?? null;
$this->trailingZeroDisplay = $options['trailingZeroDisplay'] ?? null;
$this->unit = $options['unit'] ?? null;
$this->unitDisplay = $options['unitDisplay'] ?? null;
$this->useGrouping = $options['useGrouping'] ?? null;
}
}