Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
DataMwI18n | |
0.00% |
0 / 20 |
|
0.00% |
0 / 12 |
272 | |
0.00% |
0 / 1 |
getSpanInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAttributeInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAttributeNames | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
setSpanInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setAttributeInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__clone | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
defaultValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flatten | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toJsonArray | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
newFromJsonArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
jsonClassHintFor | |
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\JsonCodec\Hint; |
7 | use Wikimedia\JsonCodec\JsonCodecableTrait; |
8 | use Wikimedia\Parsoid\Utils\RichCodecable; |
9 | |
10 | /** |
11 | * data-mw-i18n information, used for internationalization. This data is used to represent the |
12 | * information necessary for later localization of messages (in spans) or element attributes values. |
13 | */ |
14 | class DataMwI18n implements RichCodecable { |
15 | use JsonCodecableTrait; |
16 | |
17 | /** @var array<string,I18nInfo> */ |
18 | private $i18nInfo = []; |
19 | |
20 | /** |
21 | * Get the I18nInfo associated to a span (which will be used to fill in the span content) or |
22 | * null, if non-existent. |
23 | * @return I18nInfo|null |
24 | */ |
25 | public function getSpanInfo(): ?I18nInfo { |
26 | return $this->i18nInfo['/'] ?? null; |
27 | } |
28 | |
29 | /** |
30 | * Get the I18nInfo that will be used to localize an element attribute value with the name |
31 | * $name or null, if non-existent. |
32 | * @param string $name |
33 | * @return I18nInfo|null |
34 | */ |
35 | public function getAttributeInfo( string $name ): ?I18nInfo { |
36 | return $this->i18nInfo[$name] ?? null; |
37 | } |
38 | |
39 | /** |
40 | * Get the name of the localized attributes or an empty array if no localized attributes |
41 | * @return array |
42 | */ |
43 | public function getAttributeNames(): array { |
44 | $res = []; |
45 | foreach ( $this->i18nInfo as $k => $v ) { |
46 | if ( $k !== '/' ) { |
47 | $res[] = $k; |
48 | } |
49 | } |
50 | return $res; |
51 | } |
52 | |
53 | /** |
54 | * Defines the internationalization parameters of a string contained in a span. |
55 | */ |
56 | public function setSpanInfo( I18nInfo $info ) { |
57 | $this->i18nInfo['/'] = $info; |
58 | } |
59 | |
60 | /** |
61 | * Defines the internationalization parameters of the $name attribute's value. |
62 | */ |
63 | public function setAttributeInfo( string $name, I18nInfo $info ) { |
64 | $this->i18nInfo[$name] = $info; |
65 | } |
66 | |
67 | public function __clone() { |
68 | // The I18nInfo objects should generally be immutable and thus |
69 | // not require cloning, but just in case someone puts a |
70 | // mutable object inside the I18nInfo::$params array, we'll |
71 | // play it safe and deep clone them. |
72 | foreach ( $this->i18nInfo as &$value ) { |
73 | $value = clone $value; |
74 | } |
75 | } |
76 | |
77 | // Rich attribute serialization support. |
78 | |
79 | /** |
80 | * Return a default value for an unset data-mw-i18n attribute. |
81 | * @return DataMwI18n |
82 | */ |
83 | public static function defaultValue(): DataMwI18n { |
84 | return new DataMwI18n; |
85 | } |
86 | |
87 | public static function hint(): Hint { |
88 | return Hint::build( self::class, Hint::ALLOW_OBJECT ); |
89 | } |
90 | |
91 | /** @inheritDoc */ |
92 | public function flatten(): ?string { |
93 | return null; |
94 | } |
95 | |
96 | /** @inheritDoc */ |
97 | public function toJsonArray(): array { |
98 | return $this->i18nInfo; |
99 | } |
100 | |
101 | /** @inheritDoc */ |
102 | public static function newFromJsonArray( array $json ) { |
103 | $i18n = new DataMwI18n(); |
104 | foreach ( $json as $k => $v ) { |
105 | $i18n->i18nInfo[$k] = $v; |
106 | } |
107 | return $i18n; |
108 | } |
109 | |
110 | /** @inheritDoc */ |
111 | public static function jsonClassHintFor( string $keyName ): ?string { |
112 | return I18nInfo::class; |
113 | } |
114 | } |