1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
6
|
|
|
use Xabbuh\XApi\Model\Definition; |
7
|
|
|
use Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition; |
8
|
|
|
use Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition; |
9
|
|
|
use Xabbuh\XApi\Model\Interaction\InteractionDefinition; |
10
|
|
|
use Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition; |
11
|
|
|
use Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition; |
12
|
|
|
use Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition; |
13
|
|
|
use Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition; |
14
|
|
|
use Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition; |
15
|
|
|
use Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition; |
16
|
|
|
use Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition; |
17
|
|
|
use Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition; |
18
|
|
|
use Xabbuh\XApi\Model\IRI; |
19
|
|
|
use Xabbuh\XApi\Model\IRL; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Normalizes and denormalizes PHP arrays to {@link Definition} instances. |
23
|
|
|
* |
24
|
|
|
* @author Christian Flothmann <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class DefinitionNormalizer extends Normalizer |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function normalize($object, $format = null, array $context = array()) |
32
|
|
|
{ |
33
|
|
|
if (!$object instanceof Definition) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$data = array(); |
38
|
|
|
|
39
|
|
|
if (null !== $name = $object->getName()) { |
40
|
|
|
$data['name'] = $this->normalizeAttribute($name, $format, $context); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (null !== $description = $object->getDescription()) { |
44
|
|
|
$data['description'] = $this->normalizeAttribute($description, $format, $context); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (null !== $type = $object->getType()) { |
48
|
|
|
$data['type'] = $type->getValue(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (null !== $moreInfo = $object->getMoreInfo()) { |
52
|
|
|
$data['moreInfo'] = $moreInfo->getValue(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (null !== $extensions = $object->getExtensions()) { |
|
|
|
|
56
|
|
|
$data['extensions'] = $this->normalizeAttribute($extensions, $format, $context); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($object instanceof InteractionDefinition) { |
60
|
|
|
if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) { |
61
|
|
|
$data['correctResponsesPattern'] = $object->getCorrectResponsesPattern(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
switch (true) { |
65
|
|
|
case $object instanceof ChoiceInteractionDefinition: |
66
|
|
|
$data['interactionType'] = 'choice'; |
67
|
|
|
|
68
|
|
|
if (null !== $choices = $object->getChoices()) { |
69
|
|
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
70
|
|
|
} |
71
|
|
|
break; |
72
|
|
|
case $object instanceof FillInInteractionDefinition: |
73
|
|
|
$data['interactionType'] = 'fill-in'; |
74
|
|
|
break; |
75
|
|
|
case $object instanceof LikertInteractionDefinition: |
76
|
|
|
$data['interactionType'] = 'likert'; |
77
|
|
|
|
78
|
|
|
if (null !== $scale = $object->getScale()) { |
79
|
|
|
$data['scale'] = $this->normalizeAttribute($scale, $format, $context); |
80
|
|
|
} |
81
|
|
|
break; |
82
|
|
|
case $object instanceof LongFillInInteractionDefinition: |
83
|
|
|
$data['interactionType'] = 'long-fill-in'; |
84
|
|
|
break; |
85
|
|
|
case $object instanceof MatchingInteractionDefinition: |
86
|
|
|
$data['interactionType'] = 'matching'; |
87
|
|
|
|
88
|
|
|
if (null !== $source = $object->getSource()) { |
89
|
|
|
$data['source'] = $this->normalizeAttribute($source, $format, $context); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (null !== $target = $object->getTarget()) { |
93
|
|
|
$data['target'] = $this->normalizeAttribute($target, $format, $context); |
94
|
|
|
} |
95
|
|
|
break; |
96
|
|
|
case $object instanceof NumericInteractionDefinition: |
97
|
|
|
$data['interactionType'] = 'numeric'; |
98
|
|
|
break; |
99
|
|
|
case $object instanceof OtherInteractionDefinition: |
100
|
|
|
$data['interactionType'] = 'other'; |
101
|
|
|
break; |
102
|
|
|
case $object instanceof PerformanceInteractionDefinition: |
103
|
|
|
$data['interactionType'] = 'performance'; |
104
|
|
|
|
105
|
|
|
if (null !== $steps = $object->getSteps()) { |
106
|
|
|
$data['steps'] = $this->normalizeAttribute($steps, $format, $context); |
107
|
|
|
} |
108
|
|
|
break; |
109
|
|
|
case $object instanceof SequencingInteractionDefinition: |
110
|
|
|
$data['interactionType'] = 'sequencing'; |
111
|
|
|
|
112
|
|
|
if (null !== $choices = $object->getChoices()) { |
113
|
|
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
114
|
|
|
} |
115
|
|
|
break; |
116
|
|
|
case $object instanceof TrueFalseInteractionDefinition: |
117
|
|
|
$data['interactionType'] = 'true-false'; |
118
|
|
|
break; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (empty($data)) { |
123
|
|
|
return new \stdClass(); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $data; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function supportsNormalization($data, $format = null) |
133
|
|
|
{ |
134
|
|
|
return $data instanceof Definition; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
141
|
|
|
{ |
142
|
|
|
if (isset($data['interactionType'])) { |
143
|
|
|
switch ($data['interactionType']) { |
144
|
|
|
case 'choice': |
145
|
|
|
$definition = new ChoiceInteractionDefinition(); |
146
|
|
|
|
147
|
|
View Code Duplication |
if (isset($data['choices'])) { |
|
|
|
|
148
|
|
|
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
149
|
|
|
} |
150
|
|
|
break; |
151
|
|
|
case 'fill-in': |
152
|
|
|
$definition = new FillInInteractionDefinition(); |
153
|
|
|
break; |
154
|
|
|
case 'likert': |
155
|
|
|
$definition = new LikertInteractionDefinition(); |
156
|
|
|
|
157
|
|
|
if (isset($data['scale'])) { |
158
|
|
|
$definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
159
|
|
|
} |
160
|
|
|
break; |
161
|
|
|
case 'long-fill-in': |
162
|
|
|
$definition = new LongFillInInteractionDefinition(); |
163
|
|
|
break; |
164
|
|
|
case 'matching': |
165
|
|
|
$definition = new MatchingInteractionDefinition(); |
166
|
|
|
|
167
|
|
|
if (isset($data['source'])) { |
168
|
|
|
$definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (isset($data['target'])) { |
172
|
|
|
$definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
173
|
|
|
} |
174
|
|
|
break; |
175
|
|
|
case 'numeric': |
176
|
|
|
$definition = new NumericInteractionDefinition(); |
177
|
|
|
break; |
178
|
|
|
case 'other': |
179
|
|
|
$definition = new OtherInteractionDefinition(); |
180
|
|
|
break; |
181
|
|
|
case 'performance': |
182
|
|
|
$definition = new PerformanceInteractionDefinition(); |
183
|
|
|
|
184
|
|
|
if (isset($data['steps'])) { |
185
|
|
|
$definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
186
|
|
|
} |
187
|
|
|
break; |
188
|
|
|
case 'sequencing': |
189
|
|
|
$definition = new SequencingInteractionDefinition(); |
190
|
|
|
|
191
|
|
View Code Duplication |
if (isset($data['choices'])) { |
|
|
|
|
192
|
|
|
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
193
|
|
|
} |
194
|
|
|
break; |
195
|
|
|
case 'true-false': |
196
|
|
|
$definition = new TrueFalseInteractionDefinition(); |
197
|
|
|
break; |
198
|
|
|
default: |
199
|
|
|
throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType'])); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (isset($data['correctResponsesPattern'])) { |
203
|
|
|
$definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
$definition = new Definition(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
if (isset($data['name'])) { |
|
|
|
|
210
|
|
|
$name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
211
|
|
|
$definition = $definition->withName($name); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
View Code Duplication |
if (isset($data['description'])) { |
|
|
|
|
215
|
|
|
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
216
|
|
|
$definition = $definition->withDescription($description); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (isset($data['type'])) { |
220
|
|
|
$definition = $definition->withType(IRI::fromString($data['type'])); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (isset($data['moreInfo'])) { |
224
|
|
|
$definition = $definition->withMoreInfo(IRL::fromString($data['moreInfo'])); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
if (isset($data['extensions'])) { |
|
|
|
|
228
|
|
|
$extensions = $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context); |
229
|
|
|
$definition = $definition->withExtensions($extensions); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $definition; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
239
|
|
|
{ |
240
|
|
|
$supportedDefinitionClasses = array( |
241
|
|
|
'Xabbuh\XApi\Model\Definition', |
242
|
|
|
'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition', |
243
|
|
|
'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition', |
244
|
|
|
'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition', |
245
|
|
|
'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition', |
246
|
|
|
'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition', |
247
|
|
|
'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition', |
248
|
|
|
'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition', |
249
|
|
|
'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition', |
250
|
|
|
'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition', |
251
|
|
|
'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition', |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
return in_array($type, $supportedDefinitionClasses, true); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.