Passed
Push — master ( 690199...a44c3b )
by Jeroen De
01:49
created

MultilingualTextValue::newFromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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