Passed
Push — modernize ( 28d263...e9ecad )
by
unknown
01:46
created

MonolingualTextValue::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
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 13
	public function __construct( $languageCode, $text ) {
32 13
		if ( !is_string( $languageCode ) || $languageCode === '' ) {
33 6
			throw new IllegalValueException( '$languageCode must be a non-empty string' );
34
		}
35 7
		if ( !is_string( $text ) ) {
36 4
			throw new IllegalValueException( '$text must be a string' );
37
		}
38
39 3
		$this->languageCode = $languageCode;
40 3
		$this->text = $text;
41 3
	}
42
43
	/**
44
	 * @see Serializable::serialize
45
	 *
46
	 * @return string
47
	 */
48 2
	public function serialize() {
49 2
		return serialize( [ $this->languageCode, $this->text ] );
50
	}
51
52
	/**
53
	 * @see Serializable::unserialize
54
	 *
55
	 * @param string $value
56
	 */
57 1
	public function unserialize( $value ) {
58 1
		list( $languageCode, $text ) = unserialize( $value );
59 1
		$this->__construct( $languageCode, $text );
60 1
	}
61
62
	/**
63
	 * @see DataValue::getType
64
	 *
65
	 * @return string
66
	 */
67 1
	public static function getType() {
68 1
		return 'monolingualtext';
69
	}
70
71
	/**
72
	 * @deprecated Kept for compatibility with older DataValues versions.
73
	 * Do not use.
74
	 *
75
	 * @return string
76
	 */
77 1
	public function getSortKey() {
78
		// TODO: we might want to re-think this key. Perhaps the language should simply be omitted.
79 1
		return $this->languageCode . $this->text;
80
	}
81
82
	/**
83
	 * @see DataValue::getValue
84
	 *
85
	 * @return self
86
	 */
87
	public function getValue() {
88
		return $this;
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94 1
	public function getText() {
95 1
		return $this->text;
96
	}
97
98
	/**
99
	 * @return string
100
	 */
101 1
	public function getLanguageCode() {
102 1
		return $this->languageCode;
103
	}
104
105
	/**
106
	 * @see DataValue::getArrayValue
107
	 *
108
	 * @return string[]
109
	 */
110 1
	public function getArrayValue() {
111
		return [
112 1
			'text' => $this->text,
113 1
			'language' => $this->languageCode,
114
		];
115
	}
116
117
	/**
118
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
119
	 * This is expected to round-trip with @see getArrayValue.
120
	 *
121
	 * @deprecated since 1.0.0. Static DataValue::newFromArray constructors like this are
122
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
123
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
124
	 *
125
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
126
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
127
	 *  this. This is not even guaranteed to be an array!
128
	 *
129
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
130
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
131
	 * @return self
132
	 */
133 7
	public static function newFromArray( $data ) {
134 7
		self::requireArrayFields( $data, [ 'language', 'text' ] );
135
136 1
		return new static( $data['language'], $data['text'] );
137
	}
138
139
}
140