Completed
Pull Request — master (#47)
by no
08:35 queued 06:16
created

MonolingualTextValue   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 127
ccs 30
cts 30
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A unserialize() 0 4 1
A getType() 0 3 1
A getValue() 0 3 1
A getText() 0 3 1
A getLanguageCode() 0 3 1
A getArrayValue() 0 6 1
A __construct() 0 11 4
A getSortKey() 0 4 1
A newFromArray() 0 5 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 {
14
15
	/**
16
	 * @var string
17
	 */
18
	private $languageCode;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $text;
24
25
	/**
26
	 * @since 0.1
27
	 *
28
	 * @param string $languageCode
29
	 * @param string $text
30
	 *
31
	 * @throws IllegalValueException
32
	 */
33 20
	public function __construct( $languageCode, $text ) {
34 20
		if ( !is_string( $languageCode ) || $languageCode === '' ) {
35 6
			throw new IllegalValueException( '$languageCode must be a non-empty string' );
36
		}
37 14
		if ( !is_string( $text ) ) {
38 4
			throw new IllegalValueException( '$text must be a string' );
39
		}
40
41 10
		$this->languageCode = $languageCode;
42 10
		$this->text = $text;
43 10
	}
44
45
	/**
46
	 * @see Serializable::serialize
47
	 *
48
	 * @return string
49
	 */
50 6
	public function serialize() {
51 6
		return serialize( [ $this->languageCode, $this->text ] );
52
	}
53
54
	/**
55
	 * @see Serializable::unserialize
56
	 *
57
	 * @param string $value
58
	 */
59 6
	public function unserialize( $value ) {
60 6
		list( $languageCode, $text ) = unserialize( $value );
61 6
		$this->__construct( $languageCode, $text );
62 6
	}
63
64
	/**
65
	 * @see DataValue::getType
66
	 *
67
	 * @return string
68
	 */
69 4
	public static function getType() {
70 4
		return 'monolingualtext';
71
	}
72
73
	/**
74
	 * @see DataValue::getSortKey
75
	 *
76
	 * @return string
77
	 */
78 1
	public function getSortKey() {
79
		// TODO: we might want to re-think this key. Perhaps the language should simply be omitted.
80 1
		return $this->languageCode . $this->text;
81
	}
82
83
	/**
84
	 * @see DataValue::getValue
85
	 *
86
	 * @return self
87
	 */
88 2
	public function getValue() {
89 2
		return $this;
90
	}
91
92
	/**
93
	 * @since 0.1
94
	 *
95
	 * @return string
96
	 */
97 2
	public function getText() {
98 2
		return $this->text;
99
	}
100
101
	/**
102
	 * @since 0.1
103
	 *
104
	 * @return string
105
	 */
106 2
	public function getLanguageCode() {
107 2
		return $this->languageCode;
108
	}
109
110
	/**
111
	 * @see DataValue::getArrayValue
112
	 *
113
	 * @return string[]
114
	 */
115 5
	public function getArrayValue() {
116
		return [
117 5
			'text' => $this->text,
118 5
			'language' => $this->languageCode,
119
		];
120
	}
121
122
	/**
123
	 * Constructs a new instance of the DataValue from the provided data.
124
	 * This can round-trip with @see getArrayValue
125
	 *
126
	 * @since 0.1
127
	 *
128
	 * @param string[] $data
129
	 *
130
	 * @return static
131
	 * @throws IllegalValueException
132
	 */
133 7
	public static function newFromArray( array $data ) {
134 7
		self::requireArrayFields( $data, [ 'language', 'text' ] );
135
136 1
		return new static( $data['language'], $data['text'] );
137
	}
138
139
}
140