Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
I18nInfo | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
__clone | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
createInterfaceI18n | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createPageContentI18n | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createLangI18n | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toJsonArray | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
newFromJsonArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\NodeData; |
5 | |
6 | use Wikimedia\Bcp47Code\Bcp47Code; |
7 | use Wikimedia\JsonCodec\JsonCodecable; |
8 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
9 | |
10 | class I18nInfo implements JsonCodecable { |
11 | use JsonCodecableTrait; |
12 | |
13 | public const USER_LANG = "x-user"; |
14 | public const PAGE_LANG = "x-page"; |
15 | |
16 | /** |
17 | * Value for the "lang" parameter. Can be one of USER_LANG or PAGE_LANG, or a fixed language |
18 | * code (discouraged when USER_LANG or PAGE_LANG could be used instead). |
19 | */ |
20 | public string $lang; |
21 | |
22 | /** |
23 | * Key of the message in localization files |
24 | */ |
25 | public string $key; |
26 | |
27 | /** |
28 | * Ordered list of parameters for the localized message |
29 | * @var ?list |
30 | */ |
31 | public ?array $params; |
32 | |
33 | public function __construct( string $lang, string $key, ?array $params = null ) { |
34 | $this->lang = $lang; |
35 | $this->key = $key; |
36 | $this->params = $params; |
37 | } |
38 | |
39 | public function __clone() { |
40 | // Parameters should generally be immutable, in which case a clone |
41 | // isn't strictly speaking necessary. But just in case someone puts |
42 | // a mutable object in here, deep clone the parameter array. |
43 | foreach ( $this->params ?? [] as $key => &$value ) { |
44 | if ( is_object( $value ) ) { |
45 | $value = clone $value; |
46 | } |
47 | } |
48 | } |
49 | |
50 | /** |
51 | * Creates internationalization information for a string or attribute value in the user |
52 | * interface language. |
53 | * @param string $key |
54 | * @param array|null $params |
55 | * @return I18nInfo |
56 | */ |
57 | public static function createInterfaceI18n( string $key, ?array $params ): I18nInfo { |
58 | return new I18nInfo( self::USER_LANG, $key, $params ); |
59 | } |
60 | |
61 | /** |
62 | * Creates internationalization information for a string or attribute value in the page |
63 | * content language. |
64 | * @param string $key |
65 | * @param array|null $params |
66 | * @return I18nInfo |
67 | */ |
68 | public static function createPageContentI18n( string $key, ?array $params ): I18nInfo { |
69 | return new I18nInfo( self::PAGE_LANG, $key, $params ); |
70 | } |
71 | |
72 | /** |
73 | * Creates internationalization information for a string or attribute value in an arbitrary |
74 | * language. |
75 | * The use of this method is discouraged; use ::createPageContentI18n(...) and |
76 | * ::createInterfaceI18n(...) where possible rather than, respectively, |
77 | * ::createLangI18n($wgContLang, ...) and ::createLangI18n($wgLang, ...). |
78 | * @param Bcp47Code $lang |
79 | * @param string $key |
80 | * @param array|null $params |
81 | * @return I18nInfo |
82 | */ |
83 | public static function createLangI18n( Bcp47Code $lang, string $key, ?array $params ): I18nInfo { |
84 | return new I18nInfo( $lang->toBcp47Code(), $key, $params ); |
85 | } |
86 | |
87 | // Rich attribute serialization support. |
88 | |
89 | /** @inheritDoc */ |
90 | public function toJsonArray(): array { |
91 | $json = [ |
92 | 'lang' => $this->lang, |
93 | 'key' => $this->key, |
94 | ]; |
95 | // Save some space when there are no params |
96 | if ( $this->params !== null ) { |
97 | $json['params'] = $this->params; |
98 | } |
99 | return $json; |
100 | } |
101 | |
102 | /** @inheritDoc */ |
103 | public static function newFromJsonArray( array $json ) { |
104 | return new I18nInfo( $json['lang'], $json['key'], $json['params'] ?? null ); |
105 | } |
106 | } |