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
143 lines (126 loc) · 2.52 KB
/
MonolingualTextValue.php
File metadata and controls
143 lines (126 loc) · 2.52 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
<?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;
/**
* @since 0.1
*
* @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( array( $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;
}
/**
* Returns the text.
*
* @since 0.1
*
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Returns the language code.
*
* @since 0.1
*
* @return string
*/
public function getLanguageCode() {
return $this->languageCode;
}
/**
* @see DataValue::getArrayValue
*
* @return string[]
*/
public function getArrayValue() {
return array(
'text' => $this->text,
'language' => $this->languageCode,
);
}
/**
* Constructs a new instance of the DataValue from the provided data.
* This can round-trip with @see getArrayValue
*
* @since 0.1
*
* @param string[] $data
*
* @return static
* @throws IllegalValueException
*/
public static function newFromArray( array $data ) {
self::requireArrayFields( $data, array( 'language', 'text' ) );
return new static( $data['language'], $data['text'] );
}
}