Completed
Push — master ( 6dd36b...4e0b95 )
by Christian
02:36
created

ActorNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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\Account;
15
use Xabbuh\XApi\Model\Actor;
16
use Xabbuh\XApi\Model\Agent;
17
use Xabbuh\XApi\Model\Group;
18
19
/**
20
 * Normalizes and denormalizes xAPI statement actors.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
final class ActorNormalizer extends Normalizer
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 10
    public function normalize($object, $format = null, array $context = array())
30
    {
31 10
        if (!$object instanceof Actor) {
32
            return null;
33
        }
34
35 10
        $data = array();
36
37 10
        if (null !== $mbox = $object->getMbox()) {
38 10
            $data['mbox'] = $mbox;
39
        }
40
41 10
        if (null !== $mboxSha1Sum = $object->getMboxSha1Sum()) {
42
            $data['mbox_sha1sum'] = $mboxSha1Sum;
43
        }
44
45 10
        if (null !== $openId = $object->getOpenId()) {
46 1
            $data['openid'] = $openId;
47
        }
48
49 10
        if (null !== $account = $object->getAccount()) {
50 3
            $data['account'] = $this->normalizeAttribute($account);
51
        }
52
53 10
        if (null !== $name = $object->getName()) {
54 2
            $data['name'] = $name;
55
        }
56
57 10
        if ($object instanceof Group) {
58 2
            $data['member'] = array();
59
60 2
            foreach ($object->getMembers() as $member) {
61 2
                $data['member'][] = $this->normalize($member);
62
            }
63
64 2
            $data['objectType'] = 'Group';
65
        } else {
66 10
            $data['objectType'] = 'Agent';
67
        }
68
69 10
        return $data;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 11
    public function supportsNormalization($data, $format = null)
76
    {
77 11
        return $data instanceof Actor;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 12
    public function denormalize($data, $class, $format = null, array $context = array())
84
    {
85 12
        $mbox = isset($data['mbox']) ? $data['mbox'] : null;
86 12
        $mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null;
87 12
        $openId = isset($data['openid']) ? $data['openid'] : null;
88 12
        $name = isset($data['name']) ? $data['name'] : null;
89 12
        $account = $this->denormalizeAccount($data, $format, $context);
90
91 12
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
92 2
            return $this->denormalizeGroup($mbox, $mboxSha1Sum, $openId, $account, $name, $data, $format, $context);
93
        }
94
95 12
        return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 13
    public function supportsDenormalization($data, $type, $format = null)
102
    {
103 13
        return 'Xabbuh\XApi\Model\Actor' === $type;
104
    }
105
106 12
    private function denormalizeAccount($data, $format = null, array $context = array())
107
    {
108 12
        if (!isset($data['account'])) {
109 12
            return null;
110
        }
111
112 3
        return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format,$context);
113
    }
114
115 2
    private function denormalizeGroup($mbox, $mboxSha1Sum, $openId, Account $account = null, $name, $data, $format = null, array $context = array())
116
    {
117 2
        $members = array();
118
119 2
        if (isset($data['member'])) {
120 2
            foreach ($data['member'] as $member) {
121 2
                $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context);
122
            }
123
        }
124
125 2
        return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members);
126
    }
127
}
128