SerializerFactory   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 15
dl 0
loc 275
ccs 65
cts 65
cp 1
rs 10
c 0
b 0
f 0

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 6 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-or-later
30
 * @author Thomas Pellissier Tanon
31
 * @author Bene* < [email protected] >
32
 * @author Addshore
33
 */
34
class SerializerFactory {
35
36
	public const OPTION_DEFAULT = 0;
37
	/** @since 1.2.0 */
38
	public const OPTION_OBJECTS_FOR_MAPS = 1;
39
	/**
40
	 * @since 1.7.0
41
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
42
	 */
43
	public const OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH = 2;
44
	/**
45
	 * @since 1.7.0
46
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
47
	 */
48
	public const OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH = 4;
49
	/**
50
	 * @since 1.7.0
51
	 * @deprecated since 2.5 use OPTION_SERIALIZE_SNAKS_WITHOUT_HASH
52
	 */
53
	public const OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH = 8;
54
	/**
55
	 * Omit hashes when serializing snaks.
56
	 * @since 2.5.0
57
	 */
58
	public const OPTION_SERIALIZE_SNAKS_WITHOUT_HASH = 14; /* =
59
		self::OPTION_SERIALIZE_MAIN_SNAKS_WITHOUT_HASH |
60
		self::OPTION_SERIALIZE_QUALIFIER_SNAKS_WITHOUT_HASH |
61
		self::OPTION_SERIALIZE_REFERENCE_SNAKS_WITHOUT_HASH; */
62
63
	/**
64
	 * @var int
65
	 */
66
	private $options;
67
68
	/**
69
	 * @var Serializer
70
	 */
71
	private $dataValueSerializer;
72
73
	/**
74
	 * @param Serializer $dataValueSerializer serializer for DataValue objects
75
	 * @param int $options set multiple with bitwise or
76
	 *
77
	 * @throws InvalidArgumentException
78
	 */
79 52
	public function __construct( Serializer $dataValueSerializer, $options = 0 ) {
80 52
		if ( !is_int( $options ) ) {
81 1
			throw new InvalidArgumentException( '$options must be an integer' );
82
		}
83
84 51
		$this->dataValueSerializer = $dataValueSerializer;
85 51
		$this->options = $options;
86 51
	}
87
88
	/**
89
	 * @return bool
90
	 */
91 38
	private function shouldUseObjectsForMaps() {
92 38
		return (bool)( $this->options & self::OPTION_OBJECTS_FOR_MAPS );
93
	}
94
95
	/**
96
	 * @return bool
97
	 */
98 22
	private function shouldSerializeMainSnaksWithHash() {
99 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...
100
	}
101
102
	/**
103
	 * @return bool
104
	 */
105 22
	private function shouldSerializeQualifierSnaksWithHash() {
106 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...
107
	}
108
109
	/**
110
	 * @return bool
111
	 */
112 30
	private function shouldSerializeReferenceSnaksWithHash() {
113 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...
114
	}
115
116
	/**
117
	 * @return DispatchableSerializer A serializer that can only serialize Item and Property
118
	 *  objects, but no other entity types. In contexts with custom entity types other than items
119
	 *  and properties this is not what you want. If in doubt, favor a custom
120
	 *  `DispatchingSerializer` containing the exact entity serializers you need.
121
	 */
122 1
	public function newEntitySerializer() {
123 1
		return new DispatchingSerializer( [
124 1
			$this->newItemSerializer(),
125 1
			$this->newPropertySerializer()
126
		] );
127
	}
128
129
	/**
130
	 * Returns a Serializer that can serialize Item objects.
131
	 *
132
	 * @since 2.1
133
	 *
134
	 * @return DispatchableSerializer
135
	 */
136 8
	public function newItemSerializer() {
137 8
		return new ItemSerializer(
138 8
			$this->newTermListSerializer(),
139 8
			$this->newAliasGroupListSerializer(),
140 8
			$this->newStatementListSerializer(),
141 8
			$this->newSiteLinkSerializer(),
142 8
			$this->shouldUseObjectsForMaps()
143
		);
144
	}
145
146
	/**
147
	 * Returns a Serializer that can serialize Property objects.
148
	 *
149
	 * @since 2.1
150
	 *
151
	 * @return DispatchableSerializer
152
	 */
153 3
	public function newPropertySerializer() {
154 3
		return new PropertySerializer(
155 3
			$this->newTermListSerializer(),
156 3
			$this->newAliasGroupListSerializer(),
157 3
			$this->newStatementListSerializer()
158
		);
159
	}
160
161
	/**
162
	 * Returns a Serializer that can serialize SiteLink objects.
163
	 *
164
	 * @return Serializer
165
	 */
166 12
	public function newSiteLinkSerializer() {
167 12
		return new SiteLinkSerializer();
168
	}
169
170
	/**
171
	 * Returns a Serializer that can serialize StatementList objects.
172
	 *
173
	 * @since 1.4
174
	 *
175
	 * @return Serializer
176
	 */
177 14
	public function newStatementListSerializer() {
178 14
		return new StatementListSerializer(
179 14
			$this->newStatementSerializer(),
180 14
			$this->shouldUseObjectsForMaps()
181
		);
182
	}
183
184
	/**
185
	 * Returns a Serializer that can serialize Statement objects.
186
	 *
187
	 * @since 1.4
188
	 *
189
	 * @return DispatchableSerializer
190
	 */
191 22
	public function newStatementSerializer() {
192 22
		return new StatementSerializer(
193 22
			$this->newSnakSerializer( $this->shouldSerializeMainSnaksWithHash() ),
194 22
			$this->newSnakListSerializer( $this->shouldSerializeQualifierSnaksWithHash() ),
195 22
			$this->newReferencesSerializer()
196
		);
197
	}
198
199
	/**
200
	 * Returns a Serializer that can serialize ReferenceList objects.
201
	 *
202
	 * @return Serializer
203
	 */
204 26
	public function newReferencesSerializer() {
205 26
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
206
	}
207
208
	/**
209
	 * Returns a Serializer that can serialize Reference objects.
210
	 *
211
	 * @return Serializer
212
	 */
213 30
	public function newReferenceSerializer() {
214 30
		return new ReferenceSerializer(
215 30
			$this->newSnakListSerializer(
216 30
				$this->shouldSerializeReferenceSnaksWithHash()
217
			)
218
		);
219
	}
220
221
	/**
222
	 * Returns a Serializer that can serialize SnakList objects.
223
	 *
224
	 * @param bool $serializeSnaksWithHash
225
	 *
226
	 * @since 1.4
227
	 *
228
	 * @return Serializer
229
	 */
230 36
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
231 36
		return new SnakListSerializer(
232 36
			$this->newSnakSerializer( $serializeSnaksWithHash ),
233 36
			$this->shouldUseObjectsForMaps()
234
		);
235
	}
236
237
	/**
238
	 * Returns a Serializer that can serialize Snak objects.
239
	 *
240
	 * @param bool $serializeWithHash
241
	 *
242
	 * @return Serializer
243
	 */
244 43
	public function newSnakSerializer( $serializeWithHash = true ) {
245 43
		return new SnakSerializer( $this->dataValueSerializer, $serializeWithHash );
246
	}
247
248
	/**
249
	 * Returns a Serializer that can serialize TypedSnak objects.
250
	 *
251
	 * @param bool $serializeWithHash
252
	 *
253
	 * @since 1.3
254
	 *
255
	 * @return Serializer
256
	 */
257 1
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
258 1
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
259
	}
260
261
	/**
262
	 * Returns a Serializer that can serialize Term objects.
263
	 *
264
	 * @since 1.5
265
	 *
266
	 * @return Serializer
267
	 */
268 12
	public function newTermSerializer() {
269 12
		return new TermSerializer();
270
	}
271
272
	/**
273
	 * Returns a Serializer that can serialize TermList objects.
274
	 *
275
	 * @since 1.5
276
	 *
277
	 * @return Serializer
278
	 */
279 11
	public function newTermListSerializer() {
280 11
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
281
	}
282
283
	/**
284
	 * Returns a Serializer that can serialize AliasGroup objects.
285
	 *
286
	 * @since 1.6
287
	 *
288
	 * @return Serializer
289
	 */
290 12
	public function newAliasGroupSerializer() {
291 12
		return new AliasGroupSerializer();
292
	}
293
294
	/**
295
	 * Returns a Serializer that can serialize AliasGroupList objects.
296
	 *
297
	 * @since 1.5
298
	 *
299
	 * @return Serializer
300
	 */
301 11
	public function newAliasGroupListSerializer() {
302 11
		return new AliasGroupListSerializer(
303 11
			$this->newAliasGroupSerializer(),
304 11
			$this->shouldUseObjectsForMaps()
305
		);
306
	}
307
308
}
309