|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xabbuh\XApi\Serializer\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
|
23 |
|
public function normalize($object, $format = null, array $context = array()) |
|
32
|
|
|
{ |
|
33
|
23 |
|
if (!$object instanceof Definition) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
23 |
|
$data = array(); |
|
38
|
|
|
|
|
39
|
23 |
|
if (null !== $name = $object->getName()) { |
|
40
|
3 |
|
$data['name'] = $this->normalizeAttribute($name, $format, $context); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
23 |
|
if (null !== $description = $object->getDescription()) { |
|
44
|
3 |
|
$data['description'] = $this->normalizeAttribute($description, $format, $context); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
23 |
|
if (null !== $type = $object->getType()) { |
|
48
|
3 |
|
$data['type'] = $type->getValue(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
23 |
|
if (null !== $moreInfo = $object->getMoreInfo()) { |
|
52
|
2 |
|
$data['moreInfo'] = $moreInfo->getValue(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
23 |
|
if (null !== $extensions = $object->getExtensions()) { |
|
56
|
3 |
|
$data['extensions'] = $this->normalizeAttribute($extensions, $format, $context); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
23 |
|
if ($object instanceof InteractionDefinition) { |
|
60
|
10 |
|
if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) { |
|
61
|
1 |
|
$data['correctResponsesPattern'] = $object->getCorrectResponsesPattern(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
switch (true) { |
|
65
|
10 |
|
case $object instanceof ChoiceInteractionDefinition: |
|
66
|
1 |
|
$data['interactionType'] = 'choice'; |
|
67
|
|
|
|
|
68
|
1 |
|
if (null !== $choices = $object->getChoices()) { |
|
69
|
1 |
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
|
70
|
|
|
} |
|
71
|
1 |
|
break; |
|
72
|
|
|
case $object instanceof FillInInteractionDefinition: |
|
73
|
1 |
|
$data['interactionType'] = 'fill-in'; |
|
74
|
1 |
|
break; |
|
75
|
|
|
case $object instanceof LikertInteractionDefinition: |
|
76
|
1 |
|
$data['interactionType'] = 'likert'; |
|
77
|
|
|
|
|
78
|
1 |
|
if (null !== $scale = $object->getScale()) { |
|
79
|
1 |
|
$data['scale'] = $this->normalizeAttribute($scale, $format, $context); |
|
80
|
|
|
} |
|
81
|
1 |
|
break; |
|
82
|
7 |
|
case $object instanceof LongFillInInteractionDefinition: |
|
83
|
|
|
$data['interactionType'] = 'long-fill-in'; |
|
84
|
|
|
break; |
|
85
|
|
|
case $object instanceof MatchingInteractionDefinition: |
|
86
|
1 |
|
$data['interactionType'] = 'matching'; |
|
87
|
|
|
|
|
88
|
1 |
|
if (null !== $source = $object->getSource()) { |
|
89
|
1 |
|
$data['source'] = $this->normalizeAttribute($source, $format, $context); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
if (null !== $target = $object->getTarget()) { |
|
93
|
1 |
|
$data['target'] = $this->normalizeAttribute($target, $format, $context); |
|
94
|
|
|
} |
|
95
|
1 |
|
break; |
|
96
|
|
|
case $object instanceof NumericInteractionDefinition: |
|
97
|
1 |
|
$data['interactionType'] = 'numeric'; |
|
98
|
1 |
|
break; |
|
99
|
|
|
case $object instanceof OtherInteractionDefinition: |
|
100
|
2 |
|
$data['interactionType'] = 'other'; |
|
101
|
2 |
|
break; |
|
102
|
|
|
case $object instanceof PerformanceInteractionDefinition: |
|
103
|
1 |
|
$data['interactionType'] = 'performance'; |
|
104
|
|
|
|
|
105
|
1 |
|
if (null !== $steps = $object->getSteps()) { |
|
106
|
1 |
|
$data['steps'] = $this->normalizeAttribute($steps, $format, $context); |
|
107
|
|
|
} |
|
108
|
1 |
|
break; |
|
109
|
|
|
case $object instanceof SequencingInteractionDefinition: |
|
110
|
1 |
|
$data['interactionType'] = 'sequencing'; |
|
111
|
|
|
|
|
112
|
1 |
|
if (null !== $choices = $object->getChoices()) { |
|
113
|
1 |
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
|
114
|
|
|
} |
|
115
|
1 |
|
break; |
|
116
|
|
|
case $object instanceof TrueFalseInteractionDefinition: |
|
117
|
1 |
|
$data['interactionType'] = 'true-false'; |
|
118
|
1 |
|
break; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
23 |
|
if (empty($data)) { |
|
123
|
5 |
|
return new \stdClass(); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
18 |
|
return $data; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
139 |
|
public function supportsNormalization($data, $format = null) |
|
133
|
|
|
{ |
|
134
|
139 |
|
return $data instanceof Definition; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
25 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
141
|
|
|
{ |
|
142
|
25 |
|
if (isset($data['interactionType'])) { |
|
143
|
10 |
|
switch ($data['interactionType']) { |
|
144
|
10 |
|
case 'choice': |
|
145
|
1 |
|
$definition = new ChoiceInteractionDefinition(); |
|
146
|
|
|
|
|
147
|
1 |
|
if (isset($data['choices'])) { |
|
148
|
1 |
|
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
149
|
|
|
} |
|
150
|
1 |
|
break; |
|
151
|
9 |
|
case 'fill-in': |
|
152
|
1 |
|
$definition = new FillInInteractionDefinition(); |
|
153
|
1 |
|
break; |
|
154
|
8 |
|
case 'likert': |
|
155
|
1 |
|
$definition = new LikertInteractionDefinition(); |
|
156
|
|
|
|
|
157
|
1 |
|
if (isset($data['scale'])) { |
|
158
|
1 |
|
$definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
159
|
|
|
} |
|
160
|
1 |
|
break; |
|
161
|
7 |
|
case 'long-fill-in': |
|
162
|
|
|
$definition = new LongFillInInteractionDefinition(); |
|
163
|
|
|
break; |
|
164
|
7 |
|
case 'matching': |
|
165
|
1 |
|
$definition = new MatchingInteractionDefinition(); |
|
166
|
|
|
|
|
167
|
1 |
|
if (isset($data['source'])) { |
|
168
|
1 |
|
$definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
if (isset($data['target'])) { |
|
172
|
1 |
|
$definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
173
|
|
|
} |
|
174
|
1 |
|
break; |
|
175
|
6 |
|
case 'numeric': |
|
176
|
1 |
|
$definition = new NumericInteractionDefinition(); |
|
177
|
1 |
|
break; |
|
178
|
5 |
|
case 'other': |
|
179
|
2 |
|
$definition = new OtherInteractionDefinition(); |
|
180
|
2 |
|
break; |
|
181
|
3 |
|
case 'performance': |
|
182
|
1 |
|
$definition = new PerformanceInteractionDefinition(); |
|
183
|
|
|
|
|
184
|
1 |
|
if (isset($data['steps'])) { |
|
185
|
1 |
|
$definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
186
|
|
|
} |
|
187
|
1 |
|
break; |
|
188
|
2 |
|
case 'sequencing': |
|
189
|
1 |
|
$definition = new SequencingInteractionDefinition(); |
|
190
|
|
|
|
|
191
|
1 |
|
if (isset($data['choices'])) { |
|
192
|
1 |
|
$definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context)); |
|
193
|
|
|
} |
|
194
|
1 |
|
break; |
|
195
|
1 |
|
case 'true-false': |
|
196
|
1 |
|
$definition = new TrueFalseInteractionDefinition(); |
|
197
|
1 |
|
break; |
|
198
|
|
|
default: |
|
199
|
|
|
throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType'])); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
10 |
|
if (isset($data['correctResponsesPattern'])) { |
|
203
|
10 |
|
$definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']); |
|
204
|
|
|
} |
|
205
|
|
|
} else { |
|
206
|
15 |
|
$definition = new Definition(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
25 |
|
if (isset($data['name'])) { |
|
210
|
5 |
|
$name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
|
211
|
5 |
|
$definition = $definition->withName($name); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
25 |
|
if (isset($data['description'])) { |
|
215
|
3 |
|
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
|
216
|
3 |
|
$definition = $definition->withDescription($description); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
25 |
|
if (isset($data['type'])) { |
|
220
|
3 |
|
$definition = $definition->withType(IRI::fromString($data['type'])); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
25 |
|
if (isset($data['moreInfo'])) { |
|
224
|
2 |
|
$definition = $definition->withMoreInfo(IRL::fromString($data['moreInfo'])); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
25 |
|
if (isset($data['extensions'])) { |
|
228
|
3 |
|
$extensions = $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context); |
|
229
|
3 |
|
$definition = $definition->withExtensions($extensions); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
25 |
|
return $definition; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* {@inheritdoc} |
|
237
|
|
|
*/ |
|
238
|
143 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
239
|
|
|
{ |
|
240
|
|
|
$supportedDefinitionClasses = array( |
|
241
|
143 |
|
'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
|
143 |
|
return in_array($type, $supportedDefinitionClasses, true); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.