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

MultilingualTextValue   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 138
ccs 35
cts 36
cp 0.9722
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A unserialize() 0 3 1
A getType() 0 3 1
A getTexts() 0 3 1
A getValue() 0 3 1
A getArrayValue() 0 12 2
A __construct() 0 15 4
A getSortKey() 0 3 2
A newFromArray() 0 17 4
1
<?php
2
3
namespace DataValues;
4
5
/**
6
 * Class representing a multilingual text value.
7
 *
8
 * @since 0.1
9
 *
10
 * @license GPL-2.0+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class MultilingualTextValue extends DataValueObject {
14
15
	/**
16
	 * Array with language codes pointing to their associated texts.
17
	 *
18
	 * @var MonolingualTextValue[]
19
	 */
20
	private $texts = [];
21
22
	/**
23
	 * @since 0.1
24
	 *
25
	 * @param MonolingualTextValue[] $monolingualValues
26
	 *
27
	 * @throws IllegalValueException
28
	 */
29 40
	public function __construct( array $monolingualValues ) {
30 40
		foreach ( $monolingualValues as $monolingualValue ) {
31 35
			if ( !( $monolingualValue instanceof MonolingualTextValue ) ) {
32 15
				throw new IllegalValueException( 'Can only construct MultilingualTextValue from MonolingualTextValue objects' );
33
			}
34
35 22
			$languageCode = $monolingualValue->getLanguageCode();
36
37 22
			if ( array_key_exists( $languageCode, $this->texts ) ) {
38 1
				throw new IllegalValueException( 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' );
39
			}
40
41 22
			$this->texts[$languageCode] = $monolingualValue;
42
		}
43 24
	}
44
45
	/**
46
	 * @see Serializable::serialize
47
	 *
48
	 * @return string
49
	 */
50 15
	public function serialize() {
51 15
		return serialize( $this->texts );
52
	}
53
54
	/**
55
	 * @see Serializable::unserialize
56
	 *
57
	 * @param string $value
58
	 */
59 15
	public function unserialize( $value ) {
60 15
		$this->__construct( unserialize( $value ) );
61 15
	}
62
63
	/**
64
	 * @see DataValue::getType
65
	 *
66
	 * @return string
67
	 */
68 10
	public static function getType() {
69 10
		return 'multilingualtext';
70
	}
71
72
	/**
73
	 * @see DataValue::getSortKey
74
	 *
75
	 * @return string|float|int
76
	 */
77 3
	public function getSortKey() {
78 3
		return empty( $this->texts ) ? '' : reset( $this->texts )->getSortKey();
79
	}
80
81
	/**
82
	 * Returns the texts as an array of monolingual text values.
83
	 *
84
	 * @since 0.1
85
	 *
86
	 * @return MonolingualTextValue[]
87
	 */
88 5
	public function getTexts() {
89 5
		return $this->texts;
90
	}
91
92
	/**
93
	 * Returns the multilingual text value
94
	 * @see DataValue::getValue
95
	 *
96
	 * @return self
97
	 */
98 10
	public function getValue() {
99 10
		return $this;
100
	}
101
102
	/**
103
	 * @see DataValue::getArrayValue
104
	 *
105
	 * @return mixed
106
	 */
107 11
	public function getArrayValue() {
108 11
		$values = [];
109
110
		/**
111
		 * @var MonolingualTextValue $text
112
		 */
113 11
		foreach ( $this->texts as $text ) {
114 9
			$values[] = $text->getArrayValue();
115
		}
116
117 11
		return $values;
118
	}
119
120
	/**
121
	 * Constructs a new instance of the DataValue from the provided data.
122
	 * This can round-trip with
123
	 * @see   getArrayValue
124
	 *
125
	 * @since 0.1
126
	 *
127
	 * @param mixed $data
128
	 *
129
	 * @throws IllegalValueException if $data is not an array.
130
	 * @return static
131
	 */
132 5
	public static function newFromArray( $data ) {
133 5
		if ( !is_array( $data ) ) {
134
			throw new IllegalValueException( "array expected" );
135
		}
136
137 5
		$values = [];
138
139 5
		foreach ( $data as $monolingualValue ) {
140 5
			if ( !is_array( $monolingualValue ) ) {
141 2
				throw new IllegalValueException( '$data must be an array of arrays' );
142
			}
143
144 3
			$values[] = MonolingualTextValue::newFromArray( $monolingualValue );
145
		}
146
147 1
		return new static( $values );
148
	}
149
150
}
151