1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class representing a multilingual text value. |
7
|
|
|
* |
8
|
|
|
* @since 0.1 |
9
|
|
|
* |
10
|
|
|
* @license GPL-2.0+ |
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
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
|
|
|
* @param MonolingualTextValue[] $monolingualValues |
24
|
|
|
* |
25
|
|
|
* @throws IllegalValueException |
26
|
|
|
*/ |
27
|
40 |
|
public function __construct( array $monolingualValues ) { |
28
|
40 |
|
foreach ( $monolingualValues as $monolingualValue ) { |
29
|
35 |
|
if ( !( $monolingualValue instanceof MonolingualTextValue ) ) { |
30
|
15 |
|
throw new IllegalValueException( 'Can only construct MultilingualTextValue from MonolingualTextValue objects' ); |
31
|
|
|
} |
32
|
|
|
|
33
|
22 |
|
$languageCode = $monolingualValue->getLanguageCode(); |
34
|
|
|
|
35
|
22 |
|
if ( array_key_exists( $languageCode, $this->texts ) ) { |
36
|
1 |
|
throw new IllegalValueException( 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' ); |
37
|
|
|
} |
38
|
|
|
|
39
|
22 |
|
$this->texts[$languageCode] = $monolingualValue; |
40
|
|
|
} |
41
|
24 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @see Serializable::serialize |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
15 |
|
public function serialize() { |
49
|
15 |
|
return serialize( $this->texts ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see Serializable::unserialize |
54
|
|
|
* |
55
|
|
|
* @param string $value |
56
|
|
|
*/ |
57
|
15 |
|
public function unserialize( $value ) { |
58
|
15 |
|
$this->__construct( unserialize( $value ) ); |
59
|
15 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see DataValue::getType |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
10 |
|
public static function getType() { |
67
|
10 |
|
return 'multilingualtext'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the texts as an array of monolingual text values, |
72
|
|
|
* with the language codes as array keys. |
73
|
|
|
* |
74
|
|
|
* @return MonolingualTextValue[] |
75
|
3 |
|
*/ |
76
|
3 |
|
public function getTexts() { |
77
|
|
|
return $this->texts; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the multilingual text value |
82
|
|
|
* @see DataValue::getValue |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
5 |
|
*/ |
86
|
5 |
|
public function getValue() { |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @see DataValue::getArrayValue |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
10 |
|
public function getArrayValue() { |
96
|
10 |
|
$values = []; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var MonolingualTextValue $text |
100
|
|
|
*/ |
101
|
|
|
foreach ( $this->texts as $text ) { |
102
|
|
|
$values[] = $text->getArrayValue(); |
103
|
|
|
} |
104
|
11 |
|
|
105
|
11 |
|
return $values; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
110
|
11 |
|
* This is expected to round-trip with @see getArrayValue. |
111
|
9 |
|
* |
112
|
|
|
* @deprecated since 1.0.0. Static DataValue::newFromArray constructors like this are |
113
|
|
|
* underspecified (not in the DataValue interface), and misleadingly named (should be named |
114
|
11 |
|
* newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
115
|
|
|
* |
116
|
|
|
* @param mixed $data Warning! Even if this is expected to be a value as returned by |
117
|
|
|
* @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
118
|
|
|
* this. This is not even guaranteed to be an array! |
119
|
|
|
* |
120
|
|
|
* @throws IllegalValueException if $data is not in the expected format. Subclasses of |
121
|
|
|
* InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public static function newFromArray( $data ) { |
125
|
|
|
if ( !is_array( $data ) ) { |
126
|
|
|
throw new IllegalValueException( "array expected" ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$values = []; |
130
|
|
|
|
131
|
|
|
foreach ( $data as $monolingualValue ) { |
132
|
|
|
$values[] = MonolingualTextValue::newFromArray( $monolingualValue ); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new static( $values ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|