Passed
Push — trimNormalizer ( b2284d...0e0038 )
by no
05:06
created

MultilingualTextValue::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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 {
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
	 * @see DataValue::getSortKey
72
	 *
73
	 * @return string|float|int
74
	 */
75 3
	public function getSortKey() {
76 3
		return empty( $this->texts ) ? '' : reset( $this->texts )->getSortKey();
77
	}
78
79
	/**
80
	 * Returns the texts as an array of monolingual text values,
81
	 * with the language codes as array keys.
82
	 *
83
	 * @return MonolingualTextValue[]
84
	 */
85 5
	public function getTexts() {
86 5
		return $this->texts;
87
	}
88
89
	/**
90
	 * Returns the multilingual text value
91
	 * @see DataValue::getValue
92
	 *
93
	 * @return self
94
	 */
95 10
	public function getValue() {
96 10
		return $this;
97
	}
98
99
	/**
100
	 * @see DataValue::getArrayValue
101
	 *
102
	 * @return mixed
103
	 */
104 11
	public function getArrayValue() {
105 11
		$values = [];
106
107
		/**
108
		 * @var MonolingualTextValue $text
109
		 */
110 11
		foreach ( $this->texts as $text ) {
111 9
			$values[] = $text->getArrayValue();
112
		}
113
114 11
		return $values;
115
	}
116
117
	/**
118
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
119
	 * This is expected to round-trip with @see getArrayValue.
120
	 *
121
	 * @deprecated since 1.0.0. Static DataValue::newFromArray constructors like this are
122
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
123
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
124
	 *
125
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
126
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
127
	 *  this. This is not even guaranteed to be an array!
128
	 *
129
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
130
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
131
	 * @return self
132
	 */
133
	public static function newFromArray( $data ) {
134
		if ( !is_array( $data ) ) {
135
			throw new IllegalValueException( "array expected" );
136
		}
137
138
		$values = [];
139
140
		foreach ( $data as $monolingualValue ) {
141
			$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...
142
		}
143
144
		return new static( $values );
145
	}
146
147
}
148