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

StatementResultNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 20 4
A supportsNormalization() 0 4 1
A denormalize() 0 6 2
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\StatementResult;
15
16
/**
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class StatementResultNormalizer extends Normalizer
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function normalize($object, $format = null, array $context = array())
25
    {
26
        if (!$object instanceof StatementResult) {
27
            return null;
28
        }
29
30
        $data = array(
31
            'statements' => array(),
32
        );
33
34
        foreach ($object->getStatements() as $statement) {
35
            $data['statements'][] = $this->normalizeAttribute($statement, $format, $context);
36
        }
37
38
        if (null !== $moreUrlPath = $object->getMoreUrlPath()) {
39
            $data['more'] = $moreUrlPath;
40
        }
41
42
        return $data;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsNormalization($data, $format = null)
49
    {
50
        return $data instanceof StatementResult;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function denormalize($data, $class, $format = null, array $context = array())
57
    {
58
        $statements = $this->denormalizeData($data['statements'], 'Xabbuh\XApi\Model\Statement[]');
59
60
        return new StatementResult($statements, isset($data['more']) ? $data['more'] : null);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supportsDenormalization($data, $type, $format = null)
67
    {
68
        return 'Xabbuh\XApi\Model\StatementResult' === $type;
69
    }
70
}
71