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