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

InteractionComponentNormalizer::normalize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
crap 3.0261
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Serializer\Normalizer;
13
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
use Xabbuh\XApi\Model\Interaction\InteractionComponent;
17
18
/**
19
 * Denormalizes xAPI statement activity {@link InteractionComponent interaction components}.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class InteractionComponentNormalizer extends Normalizer implements DenormalizerInterface, NormalizerInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 8
    public function normalize($object, $format = null, array $context = array())
29
    {
30 8
        if (!$object instanceof InteractionComponent) {
31
            return;
32
        }
33
34
        $data = array(
35 8
            'id' => $object->getId(),
36
        );
37
38 8
        if (null !== $description = $object->getDescription()) {
39 1
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
40
        }
41
42 8
        return $data;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 121
    public function supportsNormalization($data, $format = null)
49
    {
50 121
        return $data instanceof InteractionComponent;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 8
    public function denormalize($data, $class, $format = null, array $context = array())
57
    {
58 8
        $description = null;
59
60 8
        if (isset($data['description'])) {
61 1
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
62
        }
63
64 8
        return new InteractionComponent($data['id'], $description);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 127
    public function supportsDenormalization($data, $type, $format = null)
71
    {
72 127
        return 'Xabbuh\XApi\Model\Interaction\InteractionComponent' === $type;
73
    }
74
}
75