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

ObjectNormalizer::denormalize()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9.2733

Importance

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