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

ObjectNormalizer::normalize()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

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