Completed
Pull Request — master (#1)
by Christian
03:29
created

StatementNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 26 5
A supportsNormalization() 0 4 1
A denormalize() 0 19 4
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\Statement;
15
16
/**
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class StatementNormalizer extends Normalizer
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function normalize($object, $format = null, array $context = array())
25
    {
26
        if (!$object instanceof Statement) {
27
            return null;
28
        }
29
30
        $data = array(
31
            'actor' => $this->normalizeAttribute($object->getActor()),
32
            'verb' => $this->normalizeAttribute($object->getVerb()),
33
            'object' => $this->normalizeAttribute($object->getObject()),
34
        );
35
36
        if (null !== $id = $object->getId()) {
37
            $data['id'] = $id;
38
        }
39
40
        if (null !== $authority = $object->getAuthority()) {
41
            $data['authority'] = $this->normalizeAttribute($authority);
42
        }
43
44
        if (null !== $result = $object->getResult()) {
45
            $data['result'] = $this->normalizeAttribute($result);
46
        }
47
48
        return $data;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function supportsNormalization($data, $format = null)
55
    {
56
        return $data instanceof Statement;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function denormalize($data, $class, $format = null, array $context = array())
63
    {
64
        $id = isset($data['id']) ? $data['id'] : null;
65
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
66
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
67
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
68
        $result = null;
69
        $authority = null;
70
71
        if (isset($data['result'])) {
72
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
73
        }
74
75
        if (isset($data['authority'])) {
76
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
77
        }
78
79
        return new Statement($id, $actor, $verb, $object, $result, $authority);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function supportsDenormalization($data, $type, $format = null)
86
    {
87
        return 'Xabbuh\XApi\Model\Statement' === $type;
88
    }
89
}
90