Completed
Pull Request — master (#1)
by Christian
02:39
created

ObjectNormalizer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 32 5
A supportsNormalization() 0 4 1
D denormalize() 0 35 9
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\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
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class ObjectNormalizer extends Normalizer
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function normalize($object, $format = null, array $context = array())
29
    {
30
        if ($object instanceof Activity) {
31
            $activityData = array(
32
                'objectType' => 'Activity',
33
                'id' => $object->getId(),
34
            );
35
36
            if (null !== $definition = $object->getDefinition()) {
37
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
38
            }
39
40
            return $activityData;
41
        }
42
43
        if ($object instanceof StatementReference) {
44
            return array(
45
                'objectType' => 'StatementRef',
46
                'id' => $object->getStatementId(),
47
            );
48
        }
49
50
        if ($object instanceof SubStatement) {
51
            $subStatement = new Statement($object->getId(), $object->getActor(), $object->getVerb(), $object->getObject(), $object->getResult());
52
            $subStatementData = $this->normalizeAttribute($subStatement, $format, $context);
53
            $subStatementData['objectType'] = 'SubStatement';
54
55
            return $subStatementData;
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function supportsNormalization($data, $format = null)
65
    {
66
        return $data instanceof Object;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function denormalize($data, $class, $format = null, array $context = array())
73
    {
74
        if (isset($data['objectType']) && 'Activity' === $data['objectType']) {
75
            $definition = null;
76
77
            if (isset($data['definition'])) {
78
                $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
79
            }
80
81
            return new Activity($data['id'], $definition);
82
        }
83
84
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
85
            $statementData = array(
86
                'actor' => $data['actor'],
87
                'verb' => $data['verb'],
88
                'object' => $data['object'],
89
            );
90
91
            if (isset($data['result'])) {
92
                $statementData['result'] = $data['result'];
93
            }
94
95
            /** @var Statement $statement */
96
            $statement = $this->denormalizeData($statementData, 'Xabbuh\XApi\Model\Statement', $format, $context);
97
98
            return new SubStatement(null, $statement->getActor(), $statement->getVerb(), $statement->getObject(), $statement->getResult());
99
        }
100
101
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
102
            return new StatementReference($data['id']);
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function supportsDenormalization($data, $type, $format = null)
112
    {
113
        return 'Xabbuh\XApi\Model\Object' === $type;
114
    }
115
}
116