Completed
Pull Request — master (#17)
by Christian
06:03
created

DefinitionNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
19
/**
20
 * Normalizes and 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 21
    public function normalize($object, $format = null, array $context = array())
30
    {
31 21
        if (!$object instanceof Definition) {
32
            return;
33
        }
34
35 21
        $data = array();
36
37 21
        if (null !== $name = $object->getName()) {
38 3
            $data['name'] = $this->normalizeAttribute($name, $format, $context);
39
        }
40
41 21
        if (null !== $description = $object->getDescription()) {
42 3
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
43
        }
44
45 21
        if (null !== $type = $object->getType()) {
46 3
            $data['type'] = $type;
47
        }
48
49 21
        if (null !== $moreInfo = $object->getMoreInfo()) {
50 2
            $data['moreInfo'] = $moreInfo;
51
        }
52
53 21
        if ($object instanceof InteractionDefinition) {
54 10
            if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) {
55 1
                $data['correctResponsesPattern'] = $object->getCorrectResponsesPattern();
56
            }
57
58
            switch (true) {
59 10
                case $object instanceof ChoiceInteractionDefinition:
60 1
                    $data['interactionType'] = 'choice';
61
62 1
                    if (null !== $choices = $object->getChoices()) {
63 1
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
64
                    }
65 1
                    break;
66
                case $object instanceof FillInInteractionDefinition:
67 1
                    $data['interactionType'] = 'fill-in';
68 1
                    break;
69
                case $object instanceof LikertInteractionDefinition:
70 1
                    $data['interactionType'] = 'likert';
71
72 1
                    if (null !== $scale = $object->getScale()) {
73 1
                        $data['scale'] = $this->normalizeAttribute($scale, $format, $context);
74
                    }
75 1
                    break;
76 7
                case $object instanceof LongFillInInteractionDefinition:
77
                    $data['interactionType'] = 'long-fill-in';
78
                    break;
79
                case $object instanceof MatchingInteractionDefinition:
80 1
                    $data['interactionType'] = 'matching';
81
82 1
                    if (null !== $source = $object->getSource()) {
83 1
                        $data['source'] = $this->normalizeAttribute($source, $format, $context);
84
                    }
85
86 1
                    if (null !== $target = $object->getTarget()) {
87 1
                        $data['target'] = $this->normalizeAttribute($target, $format, $context);
88
                    }
89 1
                    break;
90
                case $object instanceof NumericInteractionDefinition:
91 1
                    $data['interactionType'] = 'numeric';
92 1
                    break;
93
                case $object instanceof OtherInteractionDefinition:
94 2
                    $data['interactionType'] = 'other';
95 2
                    break;
96
                case $object instanceof PerformanceInteractionDefinition:
97 1
                    $data['interactionType'] = 'performance';
98
99 1
                    if (null !== $steps = $object->getSteps()) {
100 1
                        $data['steps'] = $this->normalizeAttribute($steps, $format, $context);
101
                    }
102 1
                    break;
103
                case $object instanceof SequencingInteractionDefinition:
104 1
                    $data['interactionType'] = 'sequencing';
105
106 1
                    if (null !== $choices = $object->getChoices()) {
107 1
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
108
                    }
109 1
                    break;
110
                case $object instanceof TrueFalseInteractionDefinition:
111 1
                    $data['interactionType'] = 'true-false';
112 1
                    break;
113
            }
114
        }
115
116 21
        if (empty($data)) {
117 5
            return new \stdClass();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \stdClass(); (stdClass) is incompatible with the return type declared by the interface Symfony\Component\Serial...zerInterface::normalize of type array|integer|double|string|boolean.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
118
        }
119
120 16
        return $data;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 139
    public function supportsNormalization($data, $format = null)
127
    {
128 139
        return $data instanceof Definition;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 23
    public function denormalize($data, $class, $format = null, array $context = array())
135
    {
136 23
        $name = null;
137 23
        $description = null;
138 23
        $type = isset($data['type']) ? $data['type'] : null;
139 23
        $moreInfo = isset($data['moreInfo']) ? $data['moreInfo'] : null;
140
141 23
        if (isset($data['name'])) {
142 5
            $name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
143
        }
144
145 23
        if (isset($data['description'])) {
146 3
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
147
        }
148
149 23
        if (isset($data['interactionType'])) {
150 10
            switch ($data['interactionType']) {
151 10
                case 'choice':
152 1
                    $definition = new ChoiceInteractionDefinition($name, $description, $type, $moreInfo);
153
154 1
                    if (isset($data['choices'])) {
155 1
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
156
                    }
157
158 1
                    break;
159 9
                case 'fill-in':
160 1
                    $definition = new FillInInteractionDefinition($name, $description, $type, $moreInfo);
161 1
                    break;
162 8
                case 'likert':
163 1
                    $definition = new LikertInteractionDefinition($name, $description, $type, $moreInfo);
164
165 1
                    if (isset($data['scale'])) {
166 1
                        $definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
167
                    }
168
169 1
                    break;
170 7
                case 'long-fill-in':
171
                    $definition = new LongFillInInteractionDefinition($name, $description, $type, $moreInfo);
172
                    break;
173 7
                case 'matching':
174 1
                    $definition = new MatchingInteractionDefinition($name, $description, $type, $moreInfo);
175
176 1
                    if (isset($data['source'])) {
177 1
                        $definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
178
                    }
179
180 1
                    if (isset($data['target'])) {
181 1
                        $definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
182
                    }
183
184 1
                    break;
185 6
                case 'numeric':
186 1
                    $definition = new NumericInteractionDefinition($name, $description, $type, $moreInfo);
187 1
                    break;
188 5
                case 'other':
189 2
                    $definition = new OtherInteractionDefinition($name, $description, $type, $moreInfo);
190 2
                    break;
191 3
                case 'performance':
192 1
                    $definition = new PerformanceInteractionDefinition($name, $description, $type, $moreInfo);
193
194 1
                    if (isset($data['steps'])) {
195 1
                        $definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
196
                    }
197
198 1
                    break;
199 2
                case 'sequencing':
200 1
                    $definition = new SequencingInteractionDefinition($name, $description, $type, $moreInfo);
201
202 1
                    if (isset($data['choices'])) {
203 1
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
204
                    }
205
206 1
                    break;
207 1
                case 'true-false':
208 1
                    $definition = new TrueFalseInteractionDefinition($name, $description, $type, $moreInfo);
209 1
                    break;
210
                default:
211
                    throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
212
            }
213
214 10
            if (isset($data['correctResponsesPattern'])) {
215 1
                $definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
216
            }
217
218 10
            return $definition;
219
        }
220
221 13
        return new Definition($name, $description, $type, $moreInfo);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 145
    public function supportsDenormalization($data, $type, $format = null)
228
    {
229
        $supportedDefinitionClasses = array(
230 145
            'Xabbuh\XApi\Model\Definition',
231
            'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition',
232
            'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition',
233
            'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition',
234
            'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition',
235
            'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition',
236
            'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition',
237
            'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition',
238
            'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition',
239
            'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition',
240
            'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition',
241
        );
242
243 145
        return in_array($type, $supportedDefinitionClasses, true);
244
    }
245
}
246