StatementResultNormalizer::supportsNormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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