Passed
Push — removeOptions ( 71febe )
by
unknown
07:50
created

SerializerFactory::newPropertySerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 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
	 * Omit hashes when serializing snaks.
40
	 * @since 2.5.0
41
	 */
42
	const OPTION_SERIALIZE_SNAKS_WITHOUT_HASH = 2;
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 52
	public function __construct( Serializer $dataValueSerializer, $options = 0 ) {
61 52
		if ( !is_int( $options ) ) {
62 1
			throw new InvalidArgumentException( '$options must be an integer' );
63
		}
64
65 51
		$this->dataValueSerializer = $dataValueSerializer;
66 51
		$this->options = $options;
67 51
	}
68
69
	/**
70
	 * @return bool
71
	 */
72 38
	private function shouldUseObjectsForMaps() {
73 38
		return (bool)( $this->options & self::OPTION_OBJECTS_FOR_MAPS );
74
	}
75
76
	/**
77
	 * @return bool
78
	 */
79 30
	private function shouldSerializeSnaksWithHash() {
80 30
		return !(bool)( $this->options & self::OPTION_SERIALIZE_SNAKS_WITHOUT_HASH );
81
	}
82
83
	/**
84
	 * Returns a Serializer that can serialize Item and Property objects.
85
	 *
86
	 * @return Serializer
87
	 */
88 1
	public function newEntitySerializer() {
89 1
		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...
90 1
			$this->newItemSerializer(),
91 1
			$this->newPropertySerializer()
92
		] );
93
	}
94
95
	/**
96
	 * Returns a Serializer that can serialize Item objects.
97
	 *
98
	 * @since 2.1
99
	 *
100
	 * @return Serializer
101
	 */
102 8
	public function newItemSerializer() {
103 8
		return new ItemSerializer(
104 8
			$this->newTermListSerializer(),
105 8
			$this->newAliasGroupListSerializer(),
106 8
			$this->newStatementListSerializer(),
107 8
			$this->newSiteLinkSerializer(),
108 8
			$this->shouldUseObjectsForMaps()
109
		);
110
	}
111
112
	/**
113
	 * Returns a Serializer that can serialize Property objects.
114
	 *
115
	 * @since 2.1
116
	 *
117
	 * @return Serializer
118
	 */
119 3
	public function newPropertySerializer() {
120 3
		return new PropertySerializer(
121 3
			$this->newTermListSerializer(),
122 3
			$this->newAliasGroupListSerializer(),
123 3
			$this->newStatementListSerializer()
124
		);
125
	}
126
127
	/**
128
	 * Returns a Serializer that can serialize SiteLink objects.
129
	 *
130
	 * @return Serializer
131
	 */
132 12
	public function newSiteLinkSerializer() {
133 12
		return new SiteLinkSerializer();
134
	}
135
136
	/**
137
	 * Returns a Serializer that can serialize StatementList objects.
138
	 *
139
	 * @since 1.4
140
	 *
141
	 * @return Serializer
142
	 */
143 14
	public function newStatementListSerializer() {
144 14
		return new StatementListSerializer(
145 14
			$this->newStatementSerializer(),
146 14
			$this->shouldUseObjectsForMaps()
147
		);
148
	}
149
150
	/**
151
	 * Returns a Serializer that can serialize Statement objects.
152
	 *
153
	 * @since 1.4
154
	 *
155
	 * @return Serializer
156
	 */
157 22
	public function newStatementSerializer() {
158 22
		return new StatementSerializer(
159 22
			$this->newSnakSerializer( $this->shouldSerializeSnaksWithHash() ),
160 22
			$this->newSnakListSerializer( $this->shouldSerializeSnaksWithHash() ),
161 22
			$this->newReferencesSerializer()
162
		);
163
	}
164
165
	/**
166
	 * Returns a Serializer that can serialize ReferenceList objects.
167
	 *
168
	 * @return Serializer
169
	 */
170 26
	public function newReferencesSerializer() {
171 26
		return new ReferenceListSerializer( $this->newReferenceSerializer() );
172
	}
173
174
	/**
175
	 * Returns a Serializer that can serialize Reference objects.
176
	 *
177
	 * @return Serializer
178
	 */
179 30
	public function newReferenceSerializer() {
180 30
		return new ReferenceSerializer(
181 30
			$this->newSnakListSerializer( $this->shouldSerializeSnaksWithHash() )
182
		);
183
	}
184
185
	/**
186
	 * Returns a Serializer that can serialize SnakList objects.
187
	 *
188
	 * @param bool $serializeSnaksWithHash
189
	 *
190
	 * @since 1.4
191
	 *
192
	 * @return Serializer
193
	 */
194 36
	public function newSnakListSerializer( $serializeSnaksWithHash = true ) {
195 36
		return new SnakListSerializer(
196 36
			$this->newSnakSerializer( $serializeSnaksWithHash ),
197 36
			$this->shouldUseObjectsForMaps()
198
		);
199
	}
200
201
	/**
202
	 * Returns a Serializer that can serialize Snak objects.
203
	 *
204
	 * @param bool $serializeWithHash
205
	 *
206
	 * @return Serializer
207
	 */
208 43
	public function newSnakSerializer( $serializeWithHash = true ) {
209 43
		return new SnakSerializer( $this->dataValueSerializer, $serializeWithHash );
210
	}
211
212
	/**
213
	 * Returns a Serializer that can serialize TypedSnak objects.
214
	 *
215
	 * @param bool $serializeWithHash
216
	 *
217
	 * @since 1.3
218
	 *
219
	 * @return Serializer
220
	 */
221 1
	public function newTypedSnakSerializer( $serializeWithHash = true ) {
222 1
		return new TypedSnakSerializer( $this->newSnakSerializer( $serializeWithHash ) );
223
	}
224
225
	/**
226
	 * Returns a Serializer that can serialize Term objects.
227
	 *
228
	 * @since 1.5
229
	 *
230
	 * @return Serializer
231
	 */
232 12
	public function newTermSerializer() {
233 12
		return new TermSerializer();
234
	}
235
236
	/**
237
	 * Returns a Serializer that can serialize TermList objects.
238
	 *
239
	 * @since 1.5
240
	 *
241
	 * @return Serializer
242
	 */
243 11
	public function newTermListSerializer() {
244 11
		return new TermListSerializer( $this->newTermSerializer(), $this->shouldUseObjectsForMaps() );
245
	}
246
247
	/**
248
	 * Returns a Serializer that can serialize AliasGroup objects.
249
	 *
250
	 * @since 1.6
251
	 *
252
	 * @return Serializer
253
	 */
254 12
	public function newAliasGroupSerializer() {
255 12
		return new AliasGroupSerializer();
256
	}
257
258
	/**
259
	 * Returns a Serializer that can serialize AliasGroupList objects.
260
	 *
261
	 * @since 1.5
262
	 *
263
	 * @return Serializer
264
	 */
265 11
	public function newAliasGroupListSerializer() {
266 11
		return new AliasGroupListSerializer( $this->newAliasGroupSerializer(), $this->shouldUseObjectsForMaps() );
267
	}
268
269
}
270