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