1 | <?php |
||
13 | class MultilingualTextValue extends DataValueObject { |
||
14 | |||
15 | /** |
||
16 | * Array with language codes pointing to their associated texts. |
||
17 | * |
||
18 | * @var MonolingualTextValue[] |
||
19 | */ |
||
20 | private $texts = []; |
||
21 | |||
22 | /** |
||
23 | * @since 0.1 |
||
24 | * |
||
25 | * @param MonolingualTextValue[] $monolingualValues |
||
26 | * |
||
27 | * @throws IllegalValueException |
||
28 | */ |
||
29 | 40 | public function __construct( array $monolingualValues ) { |
|
30 | 40 | foreach ( $monolingualValues as $monolingualValue ) { |
|
31 | 35 | if ( !( $monolingualValue instanceof MonolingualTextValue ) ) { |
|
32 | 15 | throw new IllegalValueException( 'Can only construct MultilingualTextValue from MonolingualTextValue objects' ); |
|
33 | } |
||
34 | |||
35 | 22 | $languageCode = $monolingualValue->getLanguageCode(); |
|
36 | |||
37 | 22 | if ( array_key_exists( $languageCode, $this->texts ) ) { |
|
38 | 1 | throw new IllegalValueException( 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' ); |
|
39 | } |
||
40 | |||
41 | 22 | $this->texts[$languageCode] = $monolingualValue; |
|
42 | } |
||
43 | 24 | } |
|
44 | |||
45 | /** |
||
46 | * @see Serializable::serialize |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 15 | public function serialize() { |
|
53 | |||
54 | /** |
||
55 | * @see Serializable::unserialize |
||
56 | * |
||
57 | * @param string $value |
||
58 | */ |
||
59 | 15 | public function unserialize( $value ) { |
|
62 | |||
63 | /** |
||
64 | * @see DataValue::getType |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 10 | public static function getType() { |
|
71 | |||
72 | /** |
||
73 | * @see DataValue::getSortKey |
||
74 | * |
||
75 | * @return string|float|int |
||
76 | */ |
||
77 | 3 | public function getSortKey() { |
|
78 | 3 | return empty( $this->texts ) ? '' : reset( $this->texts )->getSortKey(); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns the texts as an array of monolingual text values. |
||
83 | * |
||
84 | * @since 0.1 |
||
85 | * |
||
86 | * @return MonolingualTextValue[] |
||
87 | */ |
||
88 | 5 | public function getTexts() { |
|
91 | |||
92 | /** |
||
93 | * Returns the multilingual text value |
||
94 | * @see DataValue::getValue |
||
95 | * |
||
96 | * @return self |
||
97 | */ |
||
98 | 10 | public function getValue() { |
|
101 | |||
102 | /** |
||
103 | * @see DataValue::getArrayValue |
||
104 | * |
||
105 | * @return mixed |
||
106 | */ |
||
107 | 11 | public function getArrayValue() { |
|
119 | |||
120 | /** |
||
121 | * Constructs a new instance of the DataValue from the provided data. |
||
122 | * This can round-trip with |
||
123 | * @see getArrayValue |
||
124 | * |
||
125 | * @since 0.1 |
||
126 | * |
||
127 | * @param mixed $data |
||
128 | * |
||
129 | * @throws IllegalValueException if $data is not an array. |
||
130 | * @return static |
||
131 | */ |
||
132 | 5 | public static function newFromArray( $data ) { |
|
149 | |||
150 | } |
||
151 |