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

StatementNormalizer::denormalize()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 12
nc 8
nop 4
crap 4
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 8
    public function normalize($object, $format = null, array $context = array())
25
    {
26 8
        if (!$object instanceof Statement) {
27
            return null;
28
        }
29
30
        $data = array(
31 8
            'actor' => $this->normalizeAttribute($object->getActor()),
32 8
            'verb' => $this->normalizeAttribute($object->getVerb()),
33 8
            'object' => $this->normalizeAttribute($object->getObject()),
34
        );
35
36 8
        if (null !== $id = $object->getId()) {
37 8
            $data['id'] = $id;
38
        }
39
40 8
        if (null !== $authority = $object->getAuthority()) {
41 2
            $data['authority'] = $this->normalizeAttribute($authority);
42
        }
43
44 8
        if (null !== $result = $object->getResult()) {
45 1
            $data['result'] = $this->normalizeAttribute($result);
46
        }
47
48 8
        return $data;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 9
    public function supportsNormalization($data, $format = null)
55
    {
56 9
        return $data instanceof Statement;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 9
    public function denormalize($data, $class, $format = null, array $context = array())
63
    {
64 9
        $id = isset($data['id']) ? $data['id'] : null;
65 9
        $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context);
66 9
        $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context);
67 9
        $object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context);
68 9
        $result = null;
69 9
        $authority = null;
70
71 9
        if (isset($data['result'])) {
72 1
            $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context);
73
        }
74
75 9
        if (isset($data['authority'])) {
76 2
            $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context);
77
        }
78
79 7
        return new Statement($id, $actor, $verb, $object, $result, $authority);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 10
    public function supportsDenormalization($data, $type, $format = null)
86
    {
87 10
        return 'Xabbuh\XApi\Model\Statement' === $type;
88
    }
89
}
90