Completed
Push — master ( 6dd36b...4e0b95 )
by Christian
02:36
created

StatementNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 71
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 26 5
A supportsNormalization() 0 4 1
A denormalize() 0 19 4
A supportsDenormalization() 0 4 1
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 8
    public function normalize($object, $format = null, array $context = array())
27
    {
28 8
        if (!$object instanceof Statement) {
29
            return null;
30
        }
31
32
        $data = array(
33 8
            'actor' => $this->normalizeAttribute($object->getActor()),
34 8
            'verb' => $this->normalizeAttribute($object->getVerb()),
35 8
            'object' => $this->normalizeAttribute($object->getObject()),
36
        );
37
38 8
        if (null !== $id = $object->getId()) {
39 8
            $data['id'] = $id;
40
        }
41
42 8
        if (null !== $authority = $object->getAuthority()) {
43 2
            $data['authority'] = $this->normalizeAttribute($authority);
44
        }
45
46 8
        if (null !== $result = $object->getResult()) {
47 1
            $data['result'] = $this->normalizeAttribute($result);
48
        }
49
50 8
        return $data;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 9
    public function supportsNormalization($data, $format = null)
57
    {
58 9
        return $data instanceof Statement;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function denormalize($data, $class, $format = null, array $context = array())
65
    {
66 9
        $id = isset($data['id']) ? $data['id'] : null;
67 9
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
68 9
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
69 9
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
70 9
        $result = null;
71 9
        $authority = null;
72
73 9
        if (isset($data['result'])) {
74 1
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
75
        }
76
77 9
        if (isset($data['authority'])) {
78 2
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
79
        }
80
81 9
        return new Statement($id, $actor, $verb, $object, $result, $authority);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 10
    public function supportsDenormalization($data, $type, $format = null)
88
    {
89 10
        return 'Xabbuh\XApi\Model\Statement' === $type;
90
    }
91
}
92