AccountNormalizer::supportsDenormalization()   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 3
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\Account;
15
use Xabbuh\XApi\Model\IRL;
16
17
/**
18
 * Normalizes and denormalizes xAPI statement accounts.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
final class AccountNormalizer extends Normalizer
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function normalize($object, $format = null, array $context = array())
28
    {
29
        if (!$object instanceof Account) {
30
            return null;
31
        }
32
33
        return array(
34
            'name' => $object->getName(),
35
            'homePage' => $object->getHomePage()->getValue(),
36
        );
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supportsNormalization($data, $format = null)
43
    {
44
        return $data instanceof Account;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function denormalize($data, $class, $format = null, array $context = array())
51
    {
52
        $name = '';
53
        $homePage = '';
54
55
        if (isset($data['name'])) {
56
            $name = $data['name'];
57
        }
58
59
        if (isset($data['homePage'])) {
60
            $homePage = $data['homePage'];
61
        }
62
63
        return new Account($name, IRL::fromString($homePage));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function supportsDenormalization($data, $type, $format = null)
70
    {
71
        return 'Xabbuh\XApi\Model\Account' === $type;
72
    }
73
}
74