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

ObjectNormalizer::denormalizeActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
crap 2
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 6
    public function normalize($object, $format = null, array $context = array())
31
    {
32 6
        if ($object instanceof Activity) {
33
            $activityData = array(
34 6
                'objectType' => 'Activity',
35 6
                'id' => $object->getId(),
36
            );
37
38 6
            if (null !== $definition = $object->getDefinition()) {
39 2
                $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context);
40
            }
41
42 6
            return $activityData;
43
        }
44
45
        if ($object instanceof StatementReference) {
46
            return array(
47
                'objectType' => 'StatementRef',
48
                'id' => $object->getStatementId(),
49
            );
50
        }
51
52
        if ($object instanceof SubStatement) {
53
            $subStatement = new Statement($object->getId(), $object->getActor(), $object->getVerb(), $object->getObject(), $object->getResult());
54
            $subStatementData = $this->normalizeAttribute($subStatement, $format, $context);
55
            $subStatementData['objectType'] = 'SubStatement';
56
57
            return $subStatementData;
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 48
    public function supportsNormalization($data, $format = null)
67
    {
68 48
        return $data instanceof Object;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 7
    public function denormalize($data, $class, $format = null, array $context = array())
75
    {
76 7
        if (!isset($data['objectType']) || 'Activity' === $data['objectType']) {
77 7
            return $this->denormalizeActivity($data, $format, $context);
78
        }
79
80
        if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) {
81
            return $this->denormalizeSubStatement($data, $format, $context);
82
        }
83
84
        if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) {
85
            return new StatementReference($data['id']);
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 49
    public function supportsDenormalization($data, $type, $format = null)
95
    {
96 49
        return 'Xabbuh\XApi\Model\Object' === $type;
97
    }
98
99 7
    private function denormalizeActivity(array  $data, $format = null, array $context = array())
100
    {
101 7
        $definition = null;
102
103 7
        if (isset($data['definition'])) {
104 2
            $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context);
105
        }
106
107 7
        return new Activity($data['id'], $definition);
108
    }
109
110
    private function denormalizeSubStatement(array  $data, $format = null, array $context = array())
111
    {
112
        $statementData = array(
113
            'actor' => $data['actor'],
114
            'verb' => $data['verb'],
115
            'object' => $data['object'],
116
        );
117
118
        if (isset($data['result'])) {
119
            $statementData['result'] = $data['result'];
120
        }
121
122
        /** @var Statement $statement */
123
        $statement = $this->denormalizeData($statementData, 'Xabbuh\XApi\Model\Statement', $format, $context);
124
125
        return new SubStatement(null, $statement->getActor(), $statement->getVerb(), $statement->getObject(), $statement->getResult());
126
    }
127
}
128