Completed
Push — master ( fcc0b1...e0182d )
by Jeroen De
26:09 queued 24:05
created

src/DataValues/MultilingualTextValue.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 *
82
	 * @return MonolingualTextValue[]
83
	 */
84 5
	public function getTexts() {
85 5
		return $this->texts;
86
	}
87
88
	/**
89
	 * Returns the multilingual text value
90
	 * @see DataValue::getValue
91
	 *
92
	 * @return self
93
	 */
94 10
	public function getValue() {
95 10
		return $this;
96
	}
97
98
	/**
99
	 * @see DataValue::getArrayValue
100
	 *
101
	 * @return mixed
102
	 */
103 11
	public function getArrayValue() {
104 11
		$values = [];
105
106
		/**
107
		 * @var MonolingualTextValue $text
108
		 */
109 11
		foreach ( $this->texts as $text ) {
110 9
			$values[] = $text->getArrayValue();
111
		}
112
113 11
		return $values;
114
	}
115
116
	/**
117
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
118
	 * This is expected to round-trip with @see getArrayValue.
119
	 *
120
	 * @deprecated since 0.3.2. Static DataValue::newFromArray constructors like this are
121
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
122
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
123
	 *
124
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
125
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
126
	 *  this. This is not even guaranteed to be an array!
127
	 *
128
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
129
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
130
	 * @return self
131
	 */
132
	public static function newFromArray( $data ) {
133
		if ( !is_array( $data ) ) {
134
			throw new IllegalValueException( "array expected" );
135
		}
136
137
		$values = [];
138
139
		foreach ( $data as $monolingualValue ) {
140
			$values[] = MonolingualTextValue::newFromArray( $monolingualValue );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MonolingualTextValue::newFromArray() has been deprecated with message: since 0.3.2. 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...
141
		}
142
143
		return new static( $values );
144
	}
145
146
}
147