Debian Codegraph @

vendor/wikimedia/parsoid/src/NodeData/DataMwI18n.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\NodeData;
5
6 /**
7 * data-mw-i18n information, used for internationalization. This data is used to represent the
8 * information necessary for later localization of messages (in spans) or element attributes values.
9 */
10 class DataMwI18n implements \JsonSerializable {
11
12 /** @var array */
13 private $i18nInfo = [];
14
15 /**
16 * Get the I18nInfo associated to a span (which will be used to fill in the span content) or
17 * null, if non-existent.
18 * @return I18nInfo|null
19 */
20 public function getSpanInfo(): ?I18nInfo {
21 return $this->i18nInfo['/'] ?? null;
22 }
23
24 /**
25 * Get the I18nInfo that will be used to localize an element attribute value with the name
26 * $name or null, if non-existent.
27 * @param string $name
28 * @return I18nInfo|null
29 */
30 public function getAttributeInfo( string $name ): ?I18nInfo {
31 return $this->i18nInfo[$name] ?? null;
32 }
33
34 /**
35 * Get the name of the localized attributes or an empty array if no localized attributes
36 * @return array
37 */
38 public function getAttributeNames(): array {
39 $res = [];
40 foreach ( $this->i18nInfo as $k => $v ) {
41 if ( $k !== '/' ) {
42 $res[] = $k;
43 }
44 }
45 return $res;
46 }
47
48 /**
49 * Defines the internationalization parameters of a string contained in a span.
50 */
51 public function setSpanInfo( I18nInfo $info ) {
52 $this->i18nInfo['/'] = $info;
53 }
54
55 /**
56 * Defines the internationalization parameters of the $name attribute's value.
57 */
58 public function setAttributeInfo( string $name, I18nInfo $info ) {
59 $this->i18nInfo[$name] = $info;
60 }
61
62 public function jsonSerialize(): array {
63 return $this->i18nInfo;
64 }
65
66 public static function fromJson( array $json ): DataMwI18n {
67 $i18n = new DataMwI18n();
68 foreach ( $json as $k => $v ) {
69 $i18n->i18nInfo[$k] = new I18nInfo( $v['lang'], $v['key'], $v['params'] );
70 }
71 return $i18n;
72 }
73 }