Completed
Pull Request — master (#222)
by
unknown
10:28 queued 07:55
created

SerializerFactory   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 21
c 5
b 0
f 0
lcom 1
cbo 15
dl 0
loc 272
ccs 59
cts 59
cp 1
rs 9.1666

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A shouldUseObjectsForMaps() 0 3 1
A shouldSerializeMainSnaksWithHash() 0 3 1
A shouldSerializeQualifierSnaksWithHash() 0 3 1
A shouldSerializeReferenceSnaksWithHash() 0 3 1
A newEntitySerializer() 0 6 1
A newItemSerializer() 0 9 1
A newPropertySerializer() 0 7 1
A newSiteLinkSerializer() 0 3 1
A newStatementListSerializer() 0 6 1
A newStatementSerializer() 0 7 1
A newReferencesSerializer() 0 3 1
A newReferenceSerializer() 0 7 1
A newSnakListSerializer() 0 6 1
A newSnakSerializer() 0 3 1
A newTypedSnakSerializer() 0 3 1
A newTermSerializer() 0 3 1
A newTermListSerializer() 0 3 1
A newAliasGroupSerializer() 0 3 1
A newAliasGroupListSerializer() 0 3 1
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use InvalidArgumentException;
6
use Serializers\DispatchingSerializer;
7
use Serializers\Serializer;
8
use Wikibase\DataModel\Serializers\AliasGroupListSerializer;
9
use Wikibase\DataModel\Serializers\AliasGroupSerializer;
10
use Wikibase\DataModel\Serializers\ItemSerializer;
11
use Wikibase\DataModel\Serializers\PropertySerializer;
12
use Wikibase\DataModel\Serializers\ReferenceListSerializer;
13
use Wikibase\DataModel\Serializers\ReferenceSerializer;
14
use Wikibase\DataModel\Serializers\SiteLinkSerializer;
15
use Wikibase\DataModel\Serializers\SnakListSerializer;
16
use Wikibase\DataModel\Serializers\SnakSerializer;
17
use Wikibase\DataModel\Serializers\StatementListSerializer;
18
use Wikibase\DataModel\Serializers\StatementSerializer;
19
use Wikibase\DataModel\Serializers\TermListSerializer;
20
use Wikibase\DataModel\Serializers\TermSerializer;
21
use Wikibase\DataModel\Serializers\TypedSnakSerializer;
22
23
/**
24
 * Factory for constructing Serializer objects that can serialize WikibaseDataModel objects.
25
 *
26
 * @since 0.1
27
 *
28
 * @license GPL-2.0+
29
 * @author Thomas Pellissier Tanon
30
 * @author Bene* < [email protected] >
31
 * @author Addshore
32
 */
33
class SerializerFactory {
34
35
	const OPTION_DEFAULT = 0;
36
	/** @since 1.2.0 */
37
	const OPTION_OBJECTS_FOR_MAPS = 1;
38
	/**
39
	 * @since 1.7.0
40
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
41
	 */
42
	const OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH = 2;
43
	/**
44
	 * @since 1.7.0
45
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
46
	 */
47
	const OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH = 4;
48
	/**
49
	 * @since 1.7.0
50
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
51
	 */
52
	const OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH = 8;
53
	/**
54
	 * Omit hashes when serializing snaks.
55
	 * @since 2.5.0
56
	 */
57
	const OPTION_SERIALIZE_SNAKS_WITHOUT_HASH = 14; /* =
58
		self::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH |
59
		self::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH |
60
		self::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH; */
61
62
	/**
63
	 * @var int
64
	 */
65
	private $options;
66
67
	/**
68
	 * @var Serializer
69
	 */
70
	private $dataValueSerializer;
71
72
	/**
73
	 * @param Serializer $dataValueSerializer serializer for DataValue objects
74
	 * @param int $options set multiple with bitwise or
75
	 *
76
	 * @throws InvalidArgumentException
77
	 */
78 52
	public function __construct( Serializer $dataValueSerializer, $options = 0 ) {
79 52
		if ( !is_int( $options ) ) {
80 1
			throw new InvalidArgumentException( '$options must be an integer' );
81
		}
82
83 51
		$this->dataValueSerializer = $dataValueSerializer;
84 51
		$this->options = $options;
85 51
	}
86
87
	/**
88
	 * @return bool
89
	 */
90 38
	private function shouldUseObjectsForMaps() {
91 38
		return (bool)( $this->options & self::OPTION_OBJECTS_FOR_MAPS );
92
	}
93
94
	/**
95
	 * @return bool
96
	 */
97 22
	private function shouldSerializeMainSnaksWithHash() {
98 22
		return !(bool)( $this->options & self::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH );
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...MAIN_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
99
	}
100
101
	/**
102
	 * @return bool
103
	 */
104 22
	private function shouldSerializeQualifierSnaksWithHash() {
105 22
		return !(bool)( $this->options & self::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH );
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...FIER_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
106
	}
107
108
	/**
109
	 * @return bool
110
	 */
111 30
	private function shouldSerializeReferenceSnaksWithHash() {
112 30
		return !(bool)( $this->options & self::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH );
0 ignored issues
show
Deprecated Code introduced by
The constant Wikibase\DataModel\Seria...ENCE_SNAKS_WITHOUT_HASH has been deprecated with message: since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH

This class constant 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 constant will be removed from the class and what other constant to use instead.

Loading history...
113
	}
114
115
	/**
116
	 * @deprecated since 2.6, use a DispatchingSerializer containing the exact entity serializers
117
	 * you need instead.
118
	 *
119
	 * @return Serializer that can only serialize Item and Property objects.
120
	 */
121
	public function newEntitySerializer() {
122
		return new DispatchingSerializer( [
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...
123
			$this->newItemSerializer(),
124
			$this->newPropertySerializer()
125
		] );
126
	}
127
128
	/**
129
	 * Returns a Serializer that can serialize Item objects.
130
	 *
131
	 * @since 2.1
132
	 *
133
	 * @return Serializer
134
	 */
135 8
	public function newItemSerializer() {
136 8
		return new ItemSerializer(
137 8
			$this->newTermListSerializer(),
138 8
			$this->newAliasGroupListSerializer(),
139 8
			$this->newStatementListSerializer(),
140 8
			$this->newSiteLinkSerializer(),
141 8
			$this->shouldUseObjectsForMaps()
142
		);
143
	}
144
145
	/**
146
	 * Returns a Serializer that can serialize Property objects.
147
	 *
148
	 * @since 2.1
149
	 *
150
	 * @return Serializer
151
	 */
152 3
	public function newPropertySerializer() {
153 3
		return new PropertySerializer(
154 3
			$this->newTermListSerializer(),
155 3
			$this->newAliasGroupListSerializer(),
156 3
			$this->newStatementListSerializer()
157
		);
158
	}
159
160
	/**
161
	 * Returns a Serializer that can serialize SiteLink objects.
162
	 *
163
	 * @return Serializer
164
	 */
165 12
	public function newSiteLinkSerializer() {
166 12
		return new SiteLinkSerializer();
167
	}
168
169
	/**
170
	 * Returns a Serializer that can serialize StatementList objects.
171
	 *
172
	 * @since 1.4
173
	 *
174
	 * @return Serializer
175
	 */
176 14
	public function newStatementListSerializer() {
177 14
		return new StatementListSerializer(
178 14
			$this->newStatementSerializer(),
179 14
			$this->shouldUseObjectsForMaps()
180
		);
181
	}
182
183
	/**
184
	 * Returns a Serializer that can serialize Statement objects.
185
	 *
186
	 * @since 1.4
187
	 *
188
	 * @return Serializer
189
	 */
190 22
	public function newStatementSerializer() {
191 22
		return new StatementSerializer(
192 22
			$this->newSnakSerializer( $this->shouldSerializeMainSnaksWithHash() ),
193 22
			$this->newSnakListSerializer( $this->shouldSerializeQualifierSnaksWithHash() ),
194 22
			$this->newReferencesSerializer()
195
		);
196
	}
197
198
	/**
199
	 * Returns a Serializer that can serialize ReferenceList objects.
200
	 *
201
	 * @return Serializer
202
	 */
203 26
	public function newReferencesSerializer() {
204 26
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
205
	}
206
207
	/**
208
	 * Returns a Serializer that can serialize Reference objects.
209
	 *
210
	 * @return Serializer
211
	 */
212 30
	public function newReferenceSerializer() {
213 30
		return new ReferenceSerializer(
214 30
			$this->newSnakListSerializer(
215 30
				$this->shouldSerializeReferenceSnaksWithHash()
216
			)
217
		);
218
	}
219
220
	/**
221
	 * Returns a Serializer that can serialize SnakList objects.
222
	 *
223
	 * @param bool $serializeSnaksWithHash
224
	 *
225
	 * @since 1.4
226
	 *
227
	 * @return Serializer
228
	 */
229 36
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
230 36
		return new SnakListSerializer(
231 36
			$this->newSnakSerializer( $serializeSnaksWithHash ),
232 36
			$this->shouldUseObjectsForMaps()
233
		);
234
	}
235
236
	/**
237
	 * Returns a Serializer that can serialize Snak objects.
238
	 *
239
	 * @param bool $serializeWithHash
240
	 *
241
	 * @return Serializer
242
	 */
243 43
	public function newSnakSerializer( $serializeWithHash = true ) {
244 43
		return new SnakSerializer( $this->dataValueSerializer, $serializeWithHash );
245
	}
246
247
	/**
248
	 * Returns a Serializer that can serialize TypedSnak objects.
249
	 *
250
	 * @param bool $serializeWithHash
251
	 *
252
	 * @since 1.3
253
	 *
254
	 * @return Serializer
255
	 */
256 1
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
257 1
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
258
	}
259
260
	/**
261
	 * Returns a Serializer that can serialize Term objects.
262
	 *
263
	 * @since 1.5
264
	 *
265
	 * @return Serializer
266
	 */
267 12
	public function newTermSerializer() {
268 12
		return new TermSerializer();
269
	}
270
271
	/**
272
	 * Returns a Serializer that can serialize TermList objects.
273
	 *
274
	 * @since 1.5
275
	 *
276
	 * @return Serializer
277
	 */
278 11
	public function newTermListSerializer() {
279 11
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
280
	}
281
282
	/**
283
	 * Returns a Serializer that can serialize AliasGroup objects.
284
	 *
285
	 * @since 1.6
286
	 *
287
	 * @return Serializer
288
	 */
289 12
	public function newAliasGroupSerializer() {
290 12
		return new AliasGroupSerializer();
291
	}
292
293
	/**
294
	 * Returns a Serializer that can serialize AliasGroupList objects.
295
	 *
296
	 * @since 1.5
297
	 *
298
	 * @return Serializer
299
	 */
300 11
	public function newAliasGroupListSerializer() {
301 11
		return new AliasGroupListSerializer( $this->newAliasGroupSerializer(), $this->shouldUseObjectsForMaps() );
302
	}
303
304
}
305