Completed
Push — master ( beeeff...e349d6 )
by Christian
07:48 queued 05:00
created

DefinitionNormalizer::supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 3
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 1
                    break;
158 9
                case 'fill-in':
159 1
                    $definition = new FillInInteractionDefinition($name, $description, $type, $moreInfo);
160 1
                    break;
161 8
                case 'likert':
162 1
                    $definition = new LikertInteractionDefinition($name, $description, $type, $moreInfo);
163
164 1
                    if (isset($data['scale'])) {
165 1
                        $definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
166
                    }
167 1
                    break;
168 7
                case 'long-fill-in':
169
                    $definition = new LongFillInInteractionDefinition($name, $description, $type, $moreInfo);
170
                    break;
171 7
                case 'matching':
172 1
                    $definition = new MatchingInteractionDefinition($name, $description, $type, $moreInfo);
173
174 1
                    if (isset($data['source'])) {
175 1
                        $definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
176
                    }
177
178 1
                    if (isset($data['target'])) {
179 1
                        $definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
180
                    }
181 1
                    break;
182 6
                case 'numeric':
183 1
                    $definition = new NumericInteractionDefinition($name, $description, $type, $moreInfo);
184 1
                    break;
185 5
                case 'other':
186 2
                    $definition = new OtherInteractionDefinition($name, $description, $type, $moreInfo);
187 2
                    break;
188 3
                case 'performance':
189 1
                    $definition = new PerformanceInteractionDefinition($name, $description, $type, $moreInfo);
190
191 1
                    if (isset($data['steps'])) {
192 1
                        $definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
193
                    }
194 1
                    break;
195 2
                case 'sequencing':
196 1
                    $definition = new SequencingInteractionDefinition($name, $description, $type, $moreInfo);
197
198 1
                    if (isset($data['choices'])) {
199 1
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
200
                    }
201 1
                    break;
202 1
                case 'true-false':
203 1
                    $definition = new TrueFalseInteractionDefinition($name, $description, $type, $moreInfo);
204 1
                    break;
205
                default:
206
                    throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
207
            }
208
209 10
            if (isset($data['correctResponsesPattern'])) {
210 1
                $definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
211
            }
212
213 10
            return $definition;
214
        }
215
216 13
        return new Definition($name, $description, $type, $moreInfo);
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 145
    public function supportsDenormalization($data, $type, $format = null)
223
    {
224
        $supportedDefinitionClasses = array(
225 145
            'Xabbuh\XApi\Model\Definition',
226
            'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition',
227
            'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition',
228
            'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition',
229
            'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition',
230
            'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition',
231
            'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition',
232
            'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition',
233
            'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition',
234
            'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition',
235
            'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition',
236
        );
237
238 145
        return in_array($type, $supportedDefinitionClasses, true);
239
    }
240
}
241