Completed
Pull Request — master (#10)
by Christian
03:42
created

StatementNormalizer::denormalize()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

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