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