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

StatementResultNormalizer::normalize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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