Completed
Push — master ( 671fb0...2bdcea )
by Christian
03:47
created

StatementNormalizer::denormalize()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 29
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 18
nc 32
nop 4
crap 6
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 Xabbuh\XApi\Model\Statement;
15
16
/**
17
 * Normalizes and denormalizes xAPI statements.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class StatementNormalizer extends Normalizer
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 9
    public function normalize($object, $format = null, array $context = array())
27
    {
28 9
        if (!$object instanceof Statement) {
29
            return null;
30
        }
31
32
        $data = array(
33 9
            'actor' => $this->normalizeAttribute($object->getActor()),
34 9
            'verb' => $this->normalizeAttribute($object->getVerb()),
35 9
            'object' => $this->normalizeAttribute($object->getObject()),
36
        );
37
38 9
        if (null !== $id = $object->getId()) {
39 9
            $data['id'] = $id;
40
        }
41
42 9
        if (null !== $authority = $object->getAuthority()) {
43 2
            $data['authority'] = $this->normalizeAttribute($authority);
44
        }
45
46 9
        if (null !== $result = $object->getResult()) {
47 1
            $data['result'] = $this->normalizeAttribute($result);
48
        }
49
50 9
        if (null !== $result = $object->getCreated()) {
51 1
            $data['timestamp'] = $this->normalizeAttribute($result);
52
        }
53
54 9
        if (null !== $result = $object->getStored()) {
55
            $data['stored'] = $this->normalizeAttribute($result);
56
        }
57
58 9
        return $data;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 49
    public function supportsNormalization($data, $format = null)
65
    {
66 49
        return $data instanceof Statement;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 10
    public function denormalize($data, $class, $format = null, array $context = array())
73
    {
74 10
        $id = isset($data['id']) ? $data['id'] : null;
75 10
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
76 10
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
77 10
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
78 10
        $result = null;
79 10
        $authority = null;
80 10
        $created = null;
81 10
        $stored = null;
82
83 10
        if (isset($data['result'])) {
84 1
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
85
        }
86
87 10
        if (isset($data['authority'])) {
88 2
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
89
        }
90
91 10
        if (isset($data['timestamp'])) {
92 1
            $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context);
93
        }
94
95 10
        if (isset($data['stored'])) {
96
            $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context);
97
        }
98
99 10
        return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 50
    public function supportsDenormalization($data, $type, $format = null)
106
    {
107 50
        return 'Xabbuh\XApi\Model\Statement' === $type;
108
    }
109
}
110