Test Failed
Pull Request — master (#91)
by Jeroen De
16:38 queued 06:39
created

MonolingualTextValue::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 {
0 ignored issues
show
Bug introduced by
There is one abstract method getSortKey in this class; you could implement it, or declare this class as abstract.
Loading history...
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::getValue
73
	 *
74
	 * @return self
75
	 */
76 1
	public function getValue() {
77
		return $this;
78 1
	}
79
80
	/**
81
	 * @return string
82
	 */
83
	public function getText() {
84
		return $this->text;
85
	}
86 2
87 2
	/**
88
	 * @return string
89
	 */
90
	public function getLanguageCode() {
91
		return $this->languageCode;
92
	}
93 2
94 2
	/**
95
	 * @see DataValue::getArrayValue
96
	 *
97
	 * @return string[]
98
	 */
99
	public function getArrayValue() {
100 2
		return [
101 2
			'text' => $this->text,
102
			'language' => $this->languageCode,
103
		];
104
	}
105
106
	/**
107
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
108
	 * This is expected to round-trip with @see getArrayValue.
109 5
	 *
110
	 * @deprecated since 1.0.0. Static DataValue::newFromArray constructors like this are
111 5
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
112 5
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
113
	 *
114
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
115
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
116
	 *  this. This is not even guaranteed to be an array!
117
	 *
118
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
119
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
120
	 * @return self
121
	 */
122
	public static function newFromArray( $data ) {
123
		self::requireArrayFields( $data, [ 'language', 'text' ] );
124
125
		return new static( $data['language'], $data['text'] );
126
	}
127
128
}
129