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\ChoiceInteraction; |
8
|
|
|
use Xabbuh\XApi\Model\Interaction\FillInInteraction; |
9
|
|
|
use Xabbuh\XApi\Model\Interaction\Interaction; |
10
|
|
|
use Xabbuh\XApi\Model\Interaction\LikertInteraction; |
11
|
|
|
use Xabbuh\XApi\Model\Interaction\LongFillInInteraction; |
12
|
|
|
use Xabbuh\XApi\Model\Interaction\MatchingInteraction; |
13
|
|
|
use Xabbuh\XApi\Model\Interaction\NumericInteraction; |
14
|
|
|
use Xabbuh\XApi\Model\Interaction\OtherInteraction; |
15
|
|
|
use Xabbuh\XApi\Model\Interaction\PerformanceInteraction; |
16
|
|
|
use Xabbuh\XApi\Model\Interaction\SequencingInteraction; |
17
|
|
|
use Xabbuh\XApi\Model\Interaction\TrueFalseInteraction; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Denormalizes PHP arrays to {@link Definition} instances. |
21
|
|
|
* |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class DefinitionNormalizer extends Normalizer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
11 |
|
public function normalize($object, $format = null, array $context = array()) |
30
|
|
|
{ |
31
|
11 |
|
if (!$object instanceof Definition) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
$data = array(); |
36
|
|
|
|
37
|
11 |
|
if (null !== $name = $object->getName()) { |
38
|
3 |
|
$data['name'] = $this->normalizeAttribute($name, $format, $context); |
39
|
|
|
} |
40
|
|
|
|
41
|
11 |
|
if (null !== $description = $object->getDescription()) { |
42
|
3 |
|
$data['description'] = $this->normalizeAttribute($description, $format, $context); |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
if (null !== $type = $object->getType()) { |
46
|
3 |
|
$data['type'] = $type; |
47
|
|
|
} |
48
|
|
|
|
49
|
11 |
|
if (null !== $moreInfo = $object->getMoreInfo()) { |
50
|
2 |
|
$data['moreInfo'] = $moreInfo; |
51
|
|
|
} |
52
|
|
|
|
53
|
11 |
|
if ($object instanceof Interaction) { |
54
|
|
|
if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) { |
55
|
|
|
$data['correctResponsesPattern'] = $object->getCorrectResponsesPattern(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
switch (true) { |
59
|
|
|
case $object instanceof ChoiceInteraction: |
60
|
|
|
$data['interactionType'] = 'choice'; |
61
|
|
|
|
62
|
|
|
if (null !== $choices = $object->getChoices()) { |
63
|
|
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
64
|
|
|
} |
65
|
|
|
break; |
66
|
|
|
case $object instanceof FillInInteraction: |
67
|
|
|
$data['interactionType'] = 'fill-in'; |
68
|
|
|
break; |
69
|
|
|
case $object instanceof LikertInteraction: |
70
|
|
|
$data['interactionType'] = 'likert'; |
71
|
|
|
|
72
|
|
|
if (null !== $scale = $object->getScale()) { |
73
|
|
|
$data['scale'] = $this->normalizeAttribute($scale, $format, $context); |
74
|
|
|
} |
75
|
|
|
break; |
76
|
|
|
case $object instanceof LongFillInInteraction: |
77
|
|
|
$data['interactionType'] = 'long-fill-in'; |
78
|
|
|
break; |
79
|
|
|
case $object instanceof MatchingInteraction: |
80
|
|
|
$data['interactionType'] = 'matching'; |
81
|
|
|
|
82
|
|
|
if (null !== $source = $object->getSource()) { |
83
|
|
|
$data['source'] = $this->normalizeAttribute($source, $format, $context); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (null !== $target = $object->getTarget()) { |
87
|
|
|
$data['target'] = $this->normalizeAttribute($target, $format, $context); |
88
|
|
|
} |
89
|
|
|
break; |
90
|
|
|
case $object instanceof NumericInteraction: |
91
|
|
|
$data['interactionType'] = 'numeric'; |
92
|
|
|
break; |
93
|
|
|
case $object instanceof OtherInteraction: |
94
|
|
|
$data['interactionType'] = 'other'; |
95
|
|
|
break; |
96
|
|
|
case $object instanceof PerformanceInteraction: |
97
|
|
|
$data['interactionType'] = 'performance'; |
98
|
|
|
|
99
|
|
|
if (null !== $steps = $object->getSteps()) { |
100
|
|
|
$data['steps'] = $this->normalizeAttribute($steps, $format, $context); |
101
|
|
|
} |
102
|
|
|
break; |
103
|
|
|
case $object instanceof SequencingInteraction: |
104
|
|
|
$data['interactionType'] = 'sequencing'; |
105
|
|
|
|
106
|
|
|
if (null !== $choices = $object->getChoices()) { |
107
|
|
|
$data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
108
|
|
|
} |
109
|
|
|
break; |
110
|
|
|
case $object instanceof TrueFalseInteraction: |
111
|
|
|
$data['interactionType'] = 'true-false'; |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
11 |
|
if (empty($data)) { |
117
|
5 |
|
return new \stdClass(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
return $data; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
126 |
|
public function supportsNormalization($data, $format = null) |
127
|
|
|
{ |
128
|
126 |
|
return $data instanceof Definition; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
13 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
135
|
|
|
{ |
136
|
13 |
|
$name = null; |
137
|
13 |
|
$description = null; |
138
|
13 |
|
$type = isset($data['type']) ? $data['type'] : null; |
139
|
13 |
|
$moreInfo = isset($data['moreInfo']) ? $data['moreInfo'] : null; |
140
|
|
|
|
141
|
13 |
|
if (isset($data['name'])) { |
142
|
5 |
|
$name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
143
|
|
|
} |
144
|
|
|
|
145
|
13 |
|
if (isset($data['description'])) { |
146
|
3 |
|
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
147
|
|
|
} |
148
|
|
|
|
149
|
13 |
|
if (isset($data['interactionType'])) { |
150
|
|
|
$definition = null; |
|
|
|
|
151
|
|
|
|
152
|
|
|
switch ($data['interactionType']) { |
153
|
|
|
case 'choice': |
154
|
|
|
$definition = new ChoiceInteraction($name, $description, $type, $moreInfo); |
155
|
|
|
break; |
156
|
|
|
case 'fill-in': |
157
|
|
|
$definition = new FillInInteraction($name, $description, $type, $moreInfo); |
158
|
|
|
break; |
159
|
|
|
case 'likert': |
160
|
|
|
$definition = new LikertInteraction($name, $description, $type, $moreInfo); |
161
|
|
|
break; |
162
|
|
|
case 'long-fill-in': |
163
|
|
|
$definition = new LongFillInInteraction($name, $description, $type, $moreInfo); |
164
|
|
|
break; |
165
|
|
|
case 'matching': |
166
|
|
|
$definition = new MatchingInteraction($name, $description, $type, $moreInfo); |
167
|
|
|
break; |
168
|
|
|
case 'numeric': |
169
|
|
|
$definition = new NumericInteraction($name, $description, $type, $moreInfo); |
170
|
|
|
break; |
171
|
|
|
case 'other': |
172
|
|
|
$definition = new OtherInteraction($name, $description, $type, $moreInfo); |
173
|
|
|
break; |
174
|
|
|
case 'performance': |
175
|
|
|
$definition = new PerformanceInteraction($name, $description, $type, $moreInfo); |
176
|
|
|
break; |
177
|
|
|
case 'sequencing': |
178
|
|
|
$definition = new SequencingInteraction($name, $description, $type, $moreInfo); |
179
|
|
|
break; |
180
|
|
|
case 'true-false': |
181
|
|
|
$definition = new TrueFalseInteraction($name, $description, $type, $moreInfo); |
182
|
|
|
break; |
183
|
|
|
default: |
184
|
|
|
throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType'])); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (isset($data['correctResponsesPattern'])) { |
188
|
|
|
$definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $definition; |
192
|
|
|
} |
193
|
|
|
|
194
|
13 |
|
return new Definition($name, $description, $type, $moreInfo); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
132 |
|
public function supportsDenormalization($data, $type, $format = null) |
201
|
|
|
{ |
202
|
|
|
$supportedDefinitionClasses = array( |
203
|
132 |
|
'Xabbuh\XApi\Model\Definition', |
204
|
|
|
'Xabbuh\XApi\Model\Interaction\ChoiceInteraction', |
205
|
|
|
'Xabbuh\XApi\Model\Interaction\FillInInteraction', |
206
|
|
|
'Xabbuh\XApi\Model\Interaction\LikertInteraction', |
207
|
|
|
'Xabbuh\XApi\Model\Interaction\LongFillInInteraction', |
208
|
|
|
'Xabbuh\XApi\Model\Interaction\MatchingInteraction', |
209
|
|
|
'Xabbuh\XApi\Model\Interaction\NumericInteraction', |
210
|
|
|
'Xabbuh\XApi\Model\Interaction\OtherInteraction', |
211
|
|
|
'Xabbuh\XApi\Model\Interaction\PerformanceInteraction', |
212
|
|
|
'Xabbuh\XApi\Model\Interaction\SequencingInteraction', |
213
|
|
|
'Xabbuh\XApi\Model\Interaction\TrueFalseInteraction', |
214
|
|
|
); |
215
|
|
|
|
216
|
132 |
|
return in_array($type, $supportedDefinitionClasses, true); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.