Completed
Pull Request — master (#212)
by no
05:11 queued 02:36
created

SerializerFactory::newSnakSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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 3.0
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 = self::OPTION_DEFAULT
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 DispatchableSerializer A dispatching serializer that can serialize whatever the
116
	 *  entity serializers given in the constructor support. There is no default support for Items
117
	 *  or Properties.
118
	 */
119
	public function newEntitySerializer() {
120
		foreach ( $this->entitySerializers as &$serializer ) {
121
			if ( is_callable( $serializer ) ) {
122
				$serializer = call_user_func( $serializer, $this );
123
			}
124
125
			if ( !( $serializer instanceof DispatchableSerializer ) ) {
126
				throw new IllegalValueException(
127
					'Expected a DispatchableSerializer or a callable returning one'
128
				);
129
			}
130
		}
131
132
		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...
133
	}
134
135
	/**
136
	 * Returns a Serializer that can serialize Item objects.
137
	 *
138
	 * @since 2.1
139
	 *
140
	 * @return Serializer
141
	 */
142
	public function newItemSerializer() {
143
		return new ItemSerializer(
144
			$this->newTermListSerializer(),
145
			$this->newAliasGroupListSerializer(),
146
			$this->newStatementListSerializer(),
147
			$this->newSiteLinkSerializer(),
148
			$this->shouldUseObjectsForMaps()
149
		);
150
	}
151
152
	/**
153
	 * Returns a Serializer that can serialize Property objects.
154
	 *
155
	 * @since 2.1
156
	 *
157
	 * @return Serializer
158
	 */
159
	public function newPropertySerializer() {
160
		return new PropertySerializer(
161
			$this->newTermListSerializer(),
162
			$this->newAliasGroupListSerializer(),
163
			$this->newStatementListSerializer()
164
		);
165
	}
166
167
	/**
168
	 * Returns a Serializer that can serialize SiteLink objects.
169
	 *
170
	 * @return Serializer
171
	 */
172
	public function newSiteLinkSerializer() {
173
		return new SiteLinkSerializer();
174
	}
175
176
	/**
177
	 * Returns a Serializer that can serialize StatementList objects.
178
	 *
179
	 * @since 1.4
180
	 *
181
	 * @return Serializer
182
	 */
183
	public function newStatementListSerializer() {
184
		return new StatementListSerializer(
185
			$this->newStatementSerializer(),
186
			$this->shouldUseObjectsForMaps()
187
		);
188
	}
189
190
	/**
191
	 * Returns a Serializer that can serialize Statement objects.
192
	 *
193
	 * @since 1.4
194
	 *
195
	 * @return Serializer
196
	 */
197
	public function newStatementSerializer() {
198
		return new StatementSerializer(
199
			$this->newSnakSerializer( $this->shouldSerializeMainSnaksWithHash() ),
200
			$this->newSnakListSerializer( $this->shouldSerializeQualifierSnaksWithHash() ),
201
			$this->newReferencesSerializer()
202
		);
203
	}
204
205
	/**
206
	 * Returns a Serializer that can serialize ReferenceList objects.
207
	 *
208
	 * @return Serializer
209
	 */
210
	public function newReferencesSerializer() {
211
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
212
	}
213
214
	/**
215
	 * Returns a Serializer that can serialize Reference objects.
216
	 *
217
	 * @return Serializer
218
	 */
219
	public function newReferenceSerializer() {
220
		return new ReferenceSerializer(
221
			$this->newSnakListSerializer(
222
				$this->shouldSerializeReferenceSnaksWithHash()
223
			)
224
		);
225
	}
226
227
	/**
228
	 * Returns a Serializer that can serialize SnakList objects.
229
	 *
230
	 * @param bool $serializeSnaksWithHash
231
	 *
232
	 * @since 1.4
233
	 *
234
	 * @return Serializer
235
	 */
236
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
237
		return new SnakListSerializer(
238
			$this->newSnakSerializer( $serializeSnaksWithHash ),
239
			$this->shouldUseObjectsForMaps()
240
		);
241
	}
242
243
	/**
244
	 * Returns a Serializer that can serialize Snak objects.
245
	 *
246
	 * @param bool $serializeWithHash
247
	 *
248
	 * @return Serializer
249
	 */
250
	public function newSnakSerializer( $serializeWithHash = true ) {
251
		return new SnakSerializer(
252
			$this->dataValueSerializer,
253
			$serializeWithHash
254
		);
255
	}
256
257
	/**
258
	 * Returns a Serializer that can serialize TypedSnak objects.
259
	 *
260
	 * @param bool $serializeWithHash
261
	 *
262
	 * @since 1.3
263
	 *
264
	 * @return Serializer
265
	 */
266
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
267
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
268
	}
269
270
	/**
271
	 * Returns a Serializer that can serialize Term objects.
272
	 *
273
	 * @since 1.5
274
	 *
275
	 * @return Serializer
276
	 */
277
	public function newTermSerializer() {
278
		return new TermSerializer();
279
	}
280
281
	/**
282
	 * Returns a Serializer that can serialize TermList objects.
283
	 *
284
	 * @since 1.5
285
	 *
286
	 * @return Serializer
287
	 */
288
	public function newTermListSerializer() {
289
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
290
	}
291
292
	/**
293
	 * Returns a Serializer that can serialize AliasGroup objects.
294
	 *
295
	 * @since 1.6
296
	 *
297
	 * @return Serializer
298
	 */
299
	public function newAliasGroupSerializer() {
300
		return new AliasGroupSerializer();
301
	}
302
303
	/**
304
	 * Returns a Serializer that can serialize AliasGroupList objects.
305
	 *
306
	 * @since 1.5
307
	 *
308
	 * @return Serializer
309
	 */
310
	public function newAliasGroupListSerializer() {
311
		return new AliasGroupListSerializer( $this->newAliasGroupSerializer(), $this->shouldUseObjectsForMaps() );
312
	}
313
314
}
315