Completed
Push — master ( 4a26c6...5bced3 )
by Christian
02:21
created

ObjectNormalizer   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 4
dl 0
loc 119
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 4 1
B denormalize() 0 20 10
A supportsDenormalization() 0 4 4
A denormalizeActivity() 0 10 2
C normalize() 0 43 7
A denormalizeSubStatement() 0 18 3
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\Activity;
15
use Xabbuh\XApi\Model\Object;
16
use Xabbuh\XApi\Model\Statement;
17
use Xabbuh\XApi\Model\StatementReference;
18
use Xabbuh\XApi\Model\SubStatement;
19
20
/**
21
 * Normalizes and denormalizes xAPI statement objects.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
final class ObjectNormalizer extends Normalizer
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 45
    public function normalize($object, $format = null, array $context = array())
31
    {
32 45
        if ($object instanceof Activity) {
33
            $activityData = array(
34 31
                'objectType' => 'Activity',
35 31
                'id' => $object->getId(),
36
            );
37
38 31
            if (null !== $definition = $object->getDefinition()) {
39 4
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
40
            }
41
42 31
            return $activityData;
43
        }
44
45 28
        if ($object instanceof StatementReference) {
46
            return array(
47 6
                'objectType' => 'StatementRef',
48 6
                'id' => $object->getStatementId(),
49
            );
50
        }
51
52 23
        if ($object instanceof SubStatement) {
53
            $data = array(
54 23
                'objectType' => 'SubStatement',
55 23
                'actor' => $this->normalizeAttribute($object->getActor(), $format, $context),
56 23
                'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context),
57 23
                'object' => $this->normalizeAttribute($object->getObject(), $format, $context),
58
            );
59
60 23
            if (null !== $result = $object->getResult()) {
61 1
                $data['result'] = $this->normalizeAttribute($result, $format, $context);
62
            }
63
64 23
            if (null !== $statementContext = $object->getContext()) {
65 1
                $data['context'] = $this->normalizeAttribute($statementContext, $format, $context);
66
            }
67
68 23
            return $data;
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 94
    public function supportsNormalization($data, $format = null)
78
    {
79 94
        return $data instanceof Object;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 46
    public function denormalize($data, $class, $format = null, array $context = array())
86
    {
87 46
        if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
88 32
            return $this->denormalizeActivity($data, $format, $context);
89
        }
90
91 28
        if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) {
92 9
            return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context);
93
        }
94
95 28
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
96 23
            return $this->denormalizeSubStatement($data, $format, $context);
97
        }
98
99 6
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
100 6
            return new StatementReference($data['id']);
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 95
    public function supportsDenormalization($data, $type, $format = null)
110
    {
111 95
        return 'Xabbuh\XApi\Model\Activity' === $type || 'Xabbuh\XApi\Model\Object' === $type || 'Xabbuh\XApi\Model\StatementReference' === $type || 'Xabbuh\XApi\Model\SubStatement' === $type;
112
    }
113
114 32
    private function denormalizeActivity(array  $data, $format = null, array $context = array())
115
    {
116 32
        $definition = null;
117
118 32
        if (isset($data['definition'])) {
119 4
            $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
120
        }
121
122 32
        return new Activity($data['id'], $definition);
123
    }
124
125 23
    private function denormalizeSubStatement(array  $data, $format = null, array $context = array())
126
    {
127 23
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
128 23
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
129 23
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
130 23
        $result = null;
131 23
        $statementContext = null;
132
133 23
        if (isset($data['result'])) {
134 1
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
135
        }
136
137 23
        if (isset($data['context'])) {
138 1
            $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context);
139
        }
140
141 23
        return new SubStatement(null, $actor, $verb, $object, $result, $statementContext);
142
    }
143
}
144