forked from Skillshare/formatphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayNamesOptions.php
More file actions
120 lines (106 loc) · 4.01 KB
/
DisplayNamesOptions.php
File metadata and controls
120 lines (106 loc) · 4.01 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
<?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;
/**
* Options for formatting display names
*
* @link https://tc39.es/ecma402/#sec-intl-displaynames-constructor
*
* @phpstan-type FallbackType "code" | "none"
* @phpstan-type LanguageDisplayType "dialect" | "standard"
* @phpstan-type StyleType "long" | "narrow" | "short"
* @phpstan-type TypeType "calendar" | "currency" | "dateTimeField" | "language" | "region" | "script"
* @phpstan-type OptionsType array{fallback?: FallbackType, languageDisplay?: LanguageDisplayType, style?: StyleType, type?: TypeType}
*/
class DisplayNamesOptions implements JsonSerializable
{
use OptionSerializer;
public const FALLBACK_CODE = 'code';
public const FALLBACK_NONE = 'none';
public const LANGUAGE_DISPLAY_DIALECT = 'dialect';
public const LANGUAGE_DISPLAY_STANDARD = 'standard';
public const STYLE_LONG = 'long';
public const STYLE_NARROW = 'narrow';
public const STYLE_SHORT = 'short';
public const TYPE_CALENDAR = 'calendar';
public const TYPE_CURRENCY = 'currency';
public const TYPE_DATE_TIME_FIELD = 'dateTimeField';
public const TYPE_LANGUAGE = 'language';
public const TYPE_REGION = 'region';
public const TYPE_SCRIPT = 'script';
/**
* The fallback strategy to use
*
* If we are unable to format a display name, we will return the same code
* provided if `fallback` is set to "code." If `fallback` is "none," then
* we return `null`. The default `fallback` is "code."
*
* @var FallbackType | null
*/
public ?string $fallback;
/**
* A suggestion for displaying the language according to the locale's
* dialect or standard representation
*
* In JavaScript, this defaults to "dialect," and some implementations do
* not appear to honor "standard" at all, though this might be a result of
* the version of the ICU data files bundled with the implementation.
*
* For now, PHP supports only the "standard" representation, so "dialect"
* has no effect.
*
* @var LanguageDisplayType | null
*/
public ?string $languageDisplay;
/**
* The formatting style to use
*
* This currently only affects the display name when `type` is "currency."
*
* @var StyleType | null
*/
public ?string $style;
/**
* The type of data for which we wish to format a display name
*
* This currently supports "currency," "language," "region," and "script."
*
* While ECMA-402 defines "calendar" and "dateTimeField" as additional types,
* these types are not implemented in Node.js or in any browsers. In fact,
* if set, the implementations throw exceptions, so this implementation
* follows the same pattern.
*
* @var TypeType | null
*/
public ?string $type;
/**
* @param OptionsType $options
*/
public function __construct(array $options = [])
{
$this->fallback = $options['fallback'] ?? null;
$this->languageDisplay = $options['languageDisplay'] ?? null;
$this->style = $options['style'] ?? null;
$this->type = $options['type'] ?? null;
}
}