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

MultilingualTextValue::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 {
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
	 * Array with language codes pointing to their associated texts.
17
	 *
18
	 * @var MonolingualTextValue[]
19
	 */
20
	private $texts = [];
21
22
	/**
23
	 * @param MonolingualTextValue[] $monolingualValues
24
	 *
25
	 * @throws IllegalValueException
26
	 */
27 40
	public function __construct( array $monolingualValues ) {
28 40
		foreach ( $monolingualValues as $monolingualValue ) {
29 35
			if ( !( $monolingualValue instanceof MonolingualTextValue ) ) {
30 15
				throw new IllegalValueException( 'Can only construct MultilingualTextValue from MonolingualTextValue objects' );
31
			}
32
33 22
			$languageCode = $monolingualValue->getLanguageCode();
34
35 22
			if ( array_key_exists( $languageCode, $this->texts ) ) {
36 1
				throw new IllegalValueException( 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' );
37
			}
38
39 22
			$this->texts[$languageCode] = $monolingualValue;
40
		}
41 24
	}
42
43
	/**
44
	 * @see Serializable::serialize
45
	 *
46
	 * @return string
47
	 */
48 15
	public function serialize() {
49 15
		return serialize( $this->texts );
50
	}
51
52
	/**
53
	 * @see Serializable::unserialize
54
	 *
55
	 * @param string $value
56
	 */
57 15
	public function unserialize( $value ) {
58 15
		$this->__construct( unserialize( $value ) );
59 15
	}
60
61
	/**
62
	 * @see DataValue::getType
63
	 *
64
	 * @return string
65
	 */
66 10
	public static function getType() {
67 10
		return 'multilingualtext';
68
	}
69
70
	/**
71
	 * Returns the texts as an array of monolingual text values,
72
	 * with the language codes as array keys.
73
	 *
74
	 * @return MonolingualTextValue[]
75 3
	 */
76 3
	public function getTexts() {
77
		return $this->texts;
78
	}
79
80
	/**
81
	 * Returns the multilingual text value
82
	 * @see DataValue::getValue
83
	 *
84
	 * @return self
85 5
	 */
86 5
	public function getValue() {
87
		return $this;
88
	}
89
90
	/**
91
	 * @see DataValue::getArrayValue
92
	 *
93
	 * @return mixed
94
	 */
95 10
	public function getArrayValue() {
96 10
		$values = [];
97
98
		/**
99
		 * @var MonolingualTextValue $text
100
		 */
101
		foreach ( $this->texts as $text ) {
102
			$values[] = $text->getArrayValue();
103
		}
104 11
105 11
		return $values;
106
	}
107
108
	/**
109
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
110 11
	 * This is expected to round-trip with @see getArrayValue.
111 9
	 *
112
	 * @deprecated since 1.0.0. Static DataValue::newFromArray constructors like this are
113
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
114 11
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
115
	 *
116
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
117
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
118
	 *  this. This is not even guaranteed to be an array!
119
	 *
120
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
121
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
122
	 * @return self
123
	 */
124
	public static function newFromArray( $data ) {
125
		if ( !is_array( $data ) ) {
126
			throw new IllegalValueException( "array expected" );
127
		}
128
129
		$values = [];
130
131
		foreach ( $data as $monolingualValue ) {
132
			$values[] = MonolingualTextValue::newFromArray( $monolingualValue );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MonolingualTextValue::newFromArray() has been deprecated with message: since 1.0.0. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
133
		}
134
135
		return new static( $values );
136
	}
137
138
}
139