Passed
Push — newDispatchingSerializer ( ef7ce2 )
by no
02:38
created

SerializerFactory::newDispatchingSerializer()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 5
nop 1
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use InvalidArgumentException;
6
use Serializers\DispatchableSerializer;
7
use Serializers\DispatchingSerializer;
8
use Serializers\Serializer;
9
use Wikibase\DataModel\Serializers\AliasGroupListSerializer;
10
use Wikibase\DataModel\Serializers\AliasGroupSerializer;
11
use Wikibase\DataModel\Serializers\ItemSerializer;
12
use Wikibase\DataModel\Serializers\PropertySerializer;
13
use Wikibase\DataModel\Serializers\ReferenceListSerializer;
14
use Wikibase\DataModel\Serializers\ReferenceSerializer;
15
use Wikibase\DataModel\Serializers\SiteLinkSerializer;
16
use Wikibase\DataModel\Serializers\SnakListSerializer;
17
use Wikibase\DataModel\Serializers\SnakSerializer;
18
use Wikibase\DataModel\Serializers\StatementListSerializer;
19
use Wikibase\DataModel\Serializers\StatementSerializer;
20
use Wikibase\DataModel\Serializers\TermListSerializer;
21
use Wikibase\DataModel\Serializers\TermSerializer;
22
use Wikibase\DataModel\Serializers\TypedSnakSerializer;
23
24
/**
25
 * Factory for constructing Serializer objects that can serialize WikibaseDataModel objects.
26
 *
27
 * @since 0.1
28
 *
29
 * @license GPL-2.0+
30
 * @author Thomas Pellissier Tanon
31
 * @author Bene* < [email protected] >
32
 * @author Addshore
33
 */
34
class SerializerFactory {
35
36
	const OPTION_DEFAULT = 0;
37
	/** @since 1.2.0 */
38
	const OPTION_OBJECTS_FOR_MAPS = 1;
39
	/** @since 1.7.0 */
40
	const OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH = 2;
41
	const OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH = 4;
42
	const OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH = 8;
43
44
	/**
45
	 * @var int
46
	 */
47
	private $options;
48
49
	/**
50
	 * @var Serializer
51
	 */
52
	private $dataValueSerializer;
53
54
	/**
55
	 * @param Serializer $dataValueSerializer serializer for DataValue objects
56
	 * @param int $options set multiple with bitwise or
57
	 *
58
	 * @throws InvalidArgumentException
59
	 */
60
	public function __construct( Serializer $dataValueSerializer, $options = 0 ) {
61
		if ( !is_int( $options ) ) {
62
			throw new InvalidArgumentException( '$options must be an integer' );
63
		}
64
65
		$this->dataValueSerializer = $dataValueSerializer;
66
		$this->options = $options;
67
	}
68
69
	/**
70
	 * @return bool
71
	 */
72
	private function shouldUseObjectsForMaps() {
73
		return (bool)( $this->options & self::OPTION_OBJECTS_FOR_MAPS );
74
	}
75
76
	/**
77
	 * @return bool
78
	 */
79
	private function shouldSerializeMainSnaksWithHash() {
80
		return !(bool)( $this->options & self::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH );
81
	}
82
83
	/**
84
	 * @return bool
85
	 */
86
	private function shouldSerializeQualifierSnaksWithHash() {
87
		return !(bool)( $this->options & self::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH );
88
	}
89
90
	/**
91
	 * @return bool
92
	 */
93
	private function shouldSerializeReferenceSnaksWithHash() {
94
		return !(bool)( $this->options & self::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH );
95
	}
96
97
	/**
98
	 * @deprecated since 2.3, use newDispatchingEntitySerializer instead
99
	 *
100
	 * @return Serializer A dispatching serializer that can only serialize Items and Properties.
101
	 */
102
	public function newEntitySerializer() {
103
		return new DispatchingSerializer( array(
0 ignored issues
show
Documentation introduced by
array($this->newItemSeri...ewPropertySerializer()) is of type array<integer,object<Ser...alizers\\Serializer>"}>, 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...
104
			$this->newItemSerializer(),
105
			$this->newPropertySerializer()
106
		) );
107
	}
108
109
	/**
110
	 * @since 2.3
111
	 *
112
	 * @param DispatchableSerializer[]|callable[] $dispatchableSerializers A list of either
113
	 *  DispatchableSerializer objects, or callbacks that return such objects.
114
	 *
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 newDispatchingSerializer( array $dispatchableSerializers ) {
119
		foreach ( $dispatchableSerializers as &$serializer ) {
120
			if ( is_callable( $serializer ) ) {
121
				$serializer = call_user_func( $serializer, $this );
122
			}
123
124
			if ( !( $serializer instanceof DispatchableSerializer ) ) {
125
				throw new InvalidArgumentException( '' );
126
			}
127
		}
128
129
		return new DispatchingSerializer( $dispatchableSerializers );
0 ignored issues
show
Documentation introduced by
$dispatchableSerializers is of type array<integer,object<Ser...leSerializer>|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...
130
	}
131
132
	/**
133
	 * Returns a Serializer that can serialize Item objects.
134
	 *
135
	 * @since 2.1
136
	 *
137
	 * @return Serializer
138
	 */
139
	public function newItemSerializer() {
140
		return new ItemSerializer(
141
			$this->newTermListSerializer(),
142
			$this->newAliasGroupListSerializer(),
143
			$this->newStatementListSerializer(),
144
			$this->newSiteLinkSerializer(),
145
			$this->shouldUseObjectsForMaps()
146
		);
147
	}
148
149
	/**
150
	 * Returns a Serializer that can serialize Property objects.
151
	 *
152
	 * @since 2.1
153
	 *
154
	 * @return Serializer
155
	 */
156
	public function newPropertySerializer() {
157
		return new PropertySerializer(
158
			$this->newTermListSerializer(),
159
			$this->newAliasGroupListSerializer(),
160
			$this->newStatementListSerializer()
161
		);
162
	}
163
164
	/**
165
	 * Returns a Serializer that can serialize SiteLink objects.
166
	 *
167
	 * @return Serializer
168
	 */
169
	public function newSiteLinkSerializer() {
170
		return new SiteLinkSerializer();
171
	}
172
173
	/**
174
	 * Returns a Serializer that can serialize StatementList objects.
175
	 *
176
	 * @since 1.4
177
	 *
178
	 * @return Serializer
179
	 */
180
	public function newStatementListSerializer() {
181
		return new StatementListSerializer(
182
			$this->newStatementSerializer(),
183
			$this->shouldUseObjectsForMaps()
184
		);
185
	}
186
187
	/**
188
	 * Returns a Serializer that can serialize Statement objects.
189
	 *
190
	 * @since 1.4
191
	 *
192
	 * @return Serializer
193
	 */
194
	public function newStatementSerializer() {
195
		return new StatementSerializer(
196
			$this->newSnakSerializer( $this->shouldSerializeMainSnaksWithHash() ),
197
			$this->newSnakListSerializer( $this->shouldSerializeQualifierSnaksWithHash() ),
198
			$this->newReferencesSerializer()
199
		);
200
	}
201
202
	/**
203
	 * Returns a Serializer that can serialize ReferenceList objects.
204
	 *
205
	 * @return Serializer
206
	 */
207
	public function newReferencesSerializer() {
208
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
209
	}
210
211
	/**
212
	 * Returns a Serializer that can serialize Reference objects.
213
	 *
214
	 * @return Serializer
215
	 */
216
	public function newReferenceSerializer() {
217
		return new ReferenceSerializer(
218
			$this->newSnakListSerializer(
219
				$this->shouldSerializeReferenceSnaksWithHash()
220
			)
221
		);
222
	}
223
224
	/**
225
	 * Returns a Serializer that can serialize SnakList objects.
226
	 *
227
	 * @param bool $serializeSnaksWithHash
228
	 *
229
	 * @since 1.4
230
	 *
231
	 * @return Serializer
232
	 */
233
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
234
		return new SnakListSerializer(
235
			$this->newSnakSerializer( $serializeSnaksWithHash ),
236
			$this->shouldUseObjectsForMaps()
237
		);
238
	}
239
240
	/**
241
	 * Returns a Serializer that can serialize Snak objects.
242
	 *
243
	 * @param bool $serializeWithHash
244
	 *
245
	 * @return Serializer
246
	 */
247
	public function newSnakSerializer( $serializeWithHash = true ) {
248
		return new SnakSerializer(
249
			$this->dataValueSerializer,
250
			$serializeWithHash
251
		);
252
	}
253
254
	/**
255
	 * Returns a Serializer that can serialize TypedSnak objects.
256
	 *
257
	 * @param bool $serializeWithHash
258
	 *
259
	 * @since 1.3
260
	 *
261
	 * @return Serializer
262
	 */
263
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
264
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
265
	}
266
267
	/**
268
	 * Returns a Serializer that can serialize Term objects.
269
	 *
270
	 * @since 1.5
271
	 *
272
	 * @return Serializer
273
	 */
274
	public function newTermSerializer() {
275
		return new TermSerializer();
276
	}
277
278
	/**
279
	 * Returns a Serializer that can serialize TermList objects.
280
	 *
281
	 * @since 1.5
282
	 *
283
	 * @return Serializer
284
	 */
285
	public function newTermListSerializer() {
286
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
287
	}
288
289
	/**
290
	 * Returns a Serializer that can serialize AliasGroup objects.
291
	 *
292
	 * @since 1.6
293
	 *
294
	 * @return Serializer
295
	 */
296
	public function newAliasGroupSerializer() {
297
		return new AliasGroupSerializer();
298
	}
299
300
	/**
301
	 * Returns a Serializer that can serialize AliasGroupList objects.
302
	 *
303
	 * @since 1.5
304
	 *
305
	 * @return Serializer
306
	 */
307
	public function newAliasGroupListSerializer() {
308
		return new AliasGroupListSerializer( $this->newAliasGroupSerializer(), $this->shouldUseObjectsForMaps() );
309
	}
310
311
}
312