Completed
Pull Request — master (#235)
by no
15:42 queued 12:49
created

SerializerFactory::shouldSerializeSnaksWithHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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