forked from DataValues/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonolingualTextValue.php
More file actions
138 lines (121 loc) · 3.03 KB
/
MonolingualTextValue.php
File metadata and controls
138 lines (121 loc) · 3.03 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
<?php
namespace DataValues;
/**
* Class representing a monolingual text value.
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class MonolingualTextValue extends DataValueObject {
/**
* @var string
*/
private $languageCode;
/**
* @var string
*/
private $text;
/**
* @param string $languageCode
* @param string $text
*
* @throws IllegalValueException
*/
public function __construct( $languageCode, $text ) {
if ( !is_string( $languageCode ) || $languageCode === '' ) {
throw new IllegalValueException( '$languageCode must be a non-empty string' );
}
if ( !is_string( $text ) ) {
throw new IllegalValueException( '$text must be a string' );
}
$this->languageCode = $languageCode;
$this->text = $text;
}
/**
* @see Serializable::serialize
*
* @return string
*/
public function serialize() {
return serialize( [ $this->languageCode, $this->text ] );
}
/**
* @see Serializable::unserialize
*
* @param string $value
*/
public function unserialize( $value ) {
list( $languageCode, $text ) = unserialize( $value );
$this->__construct( $languageCode, $text );
}
/**
* @see DataValue::getType
*
* @return string
*/
public static function getType() {
return 'monolingualtext';
}
/**
* @see DataValue::getSortKey
*
* @return string
*/
public function getSortKey() {
// TODO: we might want to re-think this key. Perhaps the language should simply be omitted.
return $this->languageCode . $this->text;
}
/**
* @see DataValue::getValue
*
* @return self
*/
public function getValue() {
return $this;
}
/**
* @return string
*/
public function getText() {
return $this->text;
}
/**
* @return string
*/
public function getLanguageCode() {
return $this->languageCode;
}
/**
* @see DataValue::getArrayValue
*
* @return string[]
*/
public function getArrayValue() {
return [
'text' => $this->text,
'language' => $this->languageCode,
];
}
/**
* 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 ) {
self::requireArrayFields( $data, [ 'language', 'text' ] );
return new static( $data['language'], $data['text'] );
}
}