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