Test Failed
Push — newDispatchingSerializer ( ef7ce2...4fa051 )
by no
02:54
created

SerializerFactory::newAliasGroupSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use DataValues\IllegalValueException;
6
use InvalidArgumentException;
7
use Serializers\DispatchableSerializer;
8
use Serializers\DispatchingSerializer;
9
use Serializers\Serializer;
10
use Wikibase\DataModel\Serializers\AliasGroupListSerializer;
11
use Wikibase\DataModel\Serializers\AliasGroupSerializer;
12
use Wikibase\DataModel\Serializers\ItemSerializer;
13
use Wikibase\DataModel\Serializers\PropertySerializer;
14
use Wikibase\DataModel\Serializers\ReferenceListSerializer;
15
use Wikibase\DataModel\Serializers\ReferenceSerializer;
16
use Wikibase\DataModel\Serializers\SiteLinkSerializer;
17
use Wikibase\DataModel\Serializers\SnakListSerializer;
18
use Wikibase\DataModel\Serializers\SnakSerializer;
19
use Wikibase\DataModel\Serializers\StatementListSerializer;
20
use Wikibase\DataModel\Serializers\StatementSerializer;
21
use Wikibase\DataModel\Serializers\TermListSerializer;
22
use Wikibase\DataModel\Serializers\TermSerializer;
23
use Wikibase\DataModel\Serializers\TypedSnakSerializer;
24
25
/**
26
 * Factory for constructing Serializer objects that can serialize WikibaseDataModel objects.
27
 *
28
 * @since 0.1
29
 *
30
 * @license GPL-2.0+
31
 * @author Thomas Pellissier Tanon
32
 * @author Bene* < [email protected] >
33
 * @author Addshore
34
 */
35
class SerializerFactory {
36
37
	const OPTION_DEFAULT = 0;
38
	/** @since 1.2.0 */
39
	const OPTION_OBJECTS_FOR_MAPS = 1;
40
	/** @since 1.7.0 */
41
	const OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH = 2;
42
	const OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH = 4;
43
	const OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH = 8;
44
45
	/**
46
	 * @var DispatchableSerializer[]|callable[]
47
	 */
48
	private $entitySerializers;
49
50
	/**
51
	 * @var Serializer
52
	 */
53
	private $dataValueSerializer;
54
55
	/**
56
	 * @var int
57
	 */
58
	private $options;
59
60
	/**
61
	 * @since 2.3
62
	 *
63
	 * @param DispatchableSerializer[]|callable[] $entitySerializers A list of either
64
	 *  DispatchableSerializer objects that are able to serialize Entities, or callbacks that return
65
	 *  such objects.
66
	 * @param Serializer $dataValueSerializer serializer for DataValue objects
67
	 * @param int $options set multiple with bitwise or
68
	 *
69
	 * @throws InvalidArgumentException
70
	 */
71
	public function __construct(
72
		array $entitySerializers,
73
		Serializer $dataValueSerializer,
74
		$options = 0
75
	) {
76
		if ( !is_int( $options ) ) {
77
			throw new InvalidArgumentException( '$options must be an integer' );
78
		}
79
80
		$this->entitySerializers = $entitySerializers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entitySerializers of type array<integer,object<Ser...leSerializer>|callable> is incompatible with the declared type array<integer,callable> of property $entitySerializers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
		$this->dataValueSerializer = $dataValueSerializer;
82
		$this->options = $options;
83
	}
84
85
	/**
86
	 * @return bool
87
	 */
88
	private function shouldUseObjectsForMaps() {
89
		return (bool)( $this->options & self::OPTION_OBJECTS_FOR_MAPS );
90
	}
91
92
	/**
93
	 * @return bool
94
	 */
95
	private function shouldSerializeMainSnaksWithHash() {
96
		return !(bool)( $this->options & self::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH );
97
	}
98
99
	/**
100
	 * @return bool
101
	 */
102
	private function shouldSerializeQualifierSnaksWithHash() {
103
		return !(bool)( $this->options & self::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH );
104
	}
105
106
	/**
107
	 * @return bool
108
	 */
109
	private function shouldSerializeReferenceSnaksWithHash() {
110
		return !(bool)( $this->options & self::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH );
111
	}
112
113
	/**
114
	 * @throws IllegalValueException
115
	 * @return Serializer A dispatching serializer that can serialize whatever the give serializers
116
	 *  support. There is no default support for Items or Properties.
117
	 */
118
	public function newEntitySerializer() {
119
		foreach ( $this->entitySerializers as &$serializer ) {
120
			if ( is_callable( $serializer ) ) {
121
				$serializer = call_user_func( $serializer, $this );
122
			}
123
124
			if ( !( $serializer instanceof DispatchableSerializer ) ) {
125
				throw new IllegalValueException(
126
					'Expected a DispatchableSerializer or a callable returning one'
127
				);
128
			}
129
		}
130
131
		return new DispatchingSerializer( $this->entitySerializers );
0 ignored issues
show
Documentation introduced by
$this->entitySerializers is of type array<integer,callable>, but the function expects a array<integer,object<Ser...ispatchableSerializer>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
	}
133
134
	/**
135
	 * Returns a Serializer that can serialize Item objects.
136
	 *
137
	 * @since 2.1
138
	 *
139
	 * @return Serializer
140
	 */
141
	public function newItemSerializer() {
142
		return new ItemSerializer(
143
			$this->newTermListSerializer(),
144
			$this->newAliasGroupListSerializer(),
145
			$this->newStatementListSerializer(),
146
			$this->newSiteLinkSerializer(),
147
			$this->shouldUseObjectsForMaps()
148
		);
149
	}
150
151
	/**
152
	 * Returns a Serializer that can serialize Property objects.
153
	 *
154
	 * @since 2.1
155
	 *
156
	 * @return Serializer
157
	 */
158
	public function newPropertySerializer() {
159
		return new PropertySerializer(
160
			$this->newTermListSerializer(),
161
			$this->newAliasGroupListSerializer(),
162
			$this->newStatementListSerializer()
163
		);
164
	}
165
166
	/**
167
	 * Returns a Serializer that can serialize SiteLink objects.
168
	 *
169
	 * @return Serializer
170
	 */
171
	public function newSiteLinkSerializer() {
172
		return new SiteLinkSerializer();
173
	}
174
175
	/**
176
	 * Returns a Serializer that can serialize StatementList objects.
177
	 *
178
	 * @since 1.4
179
	 *
180
	 * @return Serializer
181
	 */
182
	public function newStatementListSerializer() {
183
		return new StatementListSerializer(
184
			$this->newStatementSerializer(),
185
			$this->shouldUseObjectsForMaps()
186
		);
187
	}
188
189
	/**
190
	 * Returns a Serializer that can serialize Statement objects.
191
	 *
192
	 * @since 1.4
193
	 *
194
	 * @return Serializer
195
	 */
196
	public function newStatementSerializer() {
197
		return new StatementSerializer(
198
			$this->newSnakSerializer( $this->shouldSerializeMainSnaksWithHash() ),
199
			$this->newSnakListSerializer( $this->shouldSerializeQualifierSnaksWithHash() ),
200
			$this->newReferencesSerializer()
201
		);
202
	}
203
204
	/**
205
	 * Returns a Serializer that can serialize ReferenceList objects.
206
	 *
207
	 * @return Serializer
208
	 */
209
	public function newReferencesSerializer() {
210
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
211
	}
212
213
	/**
214
	 * Returns a Serializer that can serialize Reference objects.
215
	 *
216
	 * @return Serializer
217
	 */
218
	public function newReferenceSerializer() {
219
		return new ReferenceSerializer(
220
			$this->newSnakListSerializer(
221
				$this->shouldSerializeReferenceSnaksWithHash()
222
			)
223
		);
224
	}
225
226
	/**
227
	 * Returns a Serializer that can serialize SnakList objects.
228
	 *
229
	 * @param bool $serializeSnaksWithHash
230
	 *
231
	 * @since 1.4
232
	 *
233
	 * @return Serializer
234
	 */
235
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
236
		return new SnakListSerializer(
237
			$this->newSnakSerializer( $serializeSnaksWithHash ),
238
			$this->shouldUseObjectsForMaps()
239
		);
240
	}
241
242
	/**
243
	 * Returns a Serializer that can serialize Snak objects.
244
	 *
245
	 * @param bool $serializeWithHash
246
	 *
247
	 * @return Serializer
248
	 */
249
	public function newSnakSerializer( $serializeWithHash = true ) {
250
		return new SnakSerializer(
251
			$this->dataValueSerializer,
252
			$serializeWithHash
253
		);
254
	}
255
256
	/**
257
	 * Returns a Serializer that can serialize TypedSnak objects.
258
	 *
259
	 * @param bool $serializeWithHash
260
	 *
261
	 * @since 1.3
262
	 *
263
	 * @return Serializer
264
	 */
265
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
266
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
267
	}
268
269
	/**
270
	 * Returns a Serializer that can serialize Term objects.
271
	 *
272
	 * @since 1.5
273
	 *
274
	 * @return Serializer
275
	 */
276
	public function newTermSerializer() {
277
		return new TermSerializer();
278
	}
279
280
	/**
281
	 * Returns a Serializer that can serialize TermList objects.
282
	 *
283
	 * @since 1.5
284
	 *
285
	 * @return Serializer
286
	 */
287
	public function newTermListSerializer() {
288
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
289
	}
290
291
	/**
292
	 * Returns a Serializer that can serialize AliasGroup objects.
293
	 *
294
	 * @since 1.6
295
	 *
296
	 * @return Serializer
297
	 */
298
	public function newAliasGroupSerializer() {
299
		return new AliasGroupSerializer();
300
	}
301
302
	/**
303
	 * Returns a Serializer that can serialize AliasGroupList objects.
304
	 *
305
	 * @since 1.5
306
	 *
307
	 * @return Serializer
308
	 */
309
	public function newAliasGroupListSerializer() {
310
		return new AliasGroupListSerializer( $this->newAliasGroupSerializer(), $this->shouldUseObjectsForMaps() );
311
	}
312
313
}
314