Completed
Push — master ( ad7785...403475 )
by Christian
03:36
created

ActorNormalizer::denormalizeGroup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 5
crap 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\Normalizer;
13
14
use Xabbuh\XApi\Model\Actor;
15
use Xabbuh\XApi\Model\Agent;
16
use Xabbuh\XApi\Model\Group;
17
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
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
        $this->normalizeInverseFunctionalIdentifier($object->getInverseFunctionalIdentifier(), $data);
38
39 10
        if (null !== $name = $object->getName()) {
40
            $data['name'] = $name;
41
        }
42
43 10
        if ($object instanceof Group) {
44 2
            $members = array();
45
46 2
            foreach ($object->getMembers() as $member) {
47
                $members[] = $this->normalize($member);
48
            }
49
50 2
            if (count($members) > 0) {
51
                $data['member'] = $members;
52
            }
53
54 2
            $data['objectType'] = 'Group';
55
        } else {
56 9
            $data['objectType'] = 'Agent';
57
        }
58
59 10
        return $data;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 11
    public function supportsNormalization($data, $format = null)
66
    {
67 11
        return $data instanceof Actor;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 12
    public function denormalize($data, $class, $format = null, array $context = array())
74
    {
75 12
        $iri = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context);
76 12
        $name = isset($data['name']) ? $data['name'] : null;
77
78 12
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
79 2
            return $this->denormalizeGroup($iri, $name, $data, $format, $context);
80
        }
81
82 12
        return new Agent($iri, $name);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 13
    public function supportsDenormalization($data, $type, $format = null)
89
    {
90 13
        return 'Xabbuh\XApi\Model\Actor' === $type;
91
    }
92
93 10
    private function normalizeInverseFunctionalIdentifier(InverseFunctionalIdentifier $iri = null, &$data)
94
    {
95 10
        if (null === $iri) {
96
            return;
97
        }
98
99 10
        if (null !== $mbox = $iri->getMbox()) {
100 10
            $data['mbox'] = $mbox;
101
        }
102
103 10
        if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) {
104
            $data['mbox_sha1sum'] = $mboxSha1Sum;
105
        }
106
107 10
        if (null !== $openId = $iri->getOpenId()) {
108
            $data['openid'] = $openId;
109
        }
110
111 10
        if (null !== $account = $iri->getAccount()) {
112 1
            $data['account'] = $this->normalizeAttribute($account);
113
        }
114 10
    }
115
116 12
    private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array())
117
    {
118 12
        if (isset($data['mbox'])) {
119 12
            return InverseFunctionalIdentifier::withMbox($data['mbox']);
120
        }
121
122 1
        if (isset($data['mboxSha1Sum'])) {
123
            return InverseFunctionalIdentifier::withMboxSha1Sum($data['mboxSha1Sum']);
124
        }
125
126 1
        if (isset($data['openid'])) {
127
            return InverseFunctionalIdentifier::withOpenId($data['openid']);
128
        }
129
130 1
        if (isset($data['account'])) {
131 1
            return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context));
132
        }
133
    }
134
135 1
    private function denormalizeAccount($data, $format = null, array $context = array())
136
    {
137 1
        if (!isset($data['account'])) {
138
            return null;
139
        }
140
141 1
        return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context);
142
    }
143
144 2
    private function denormalizeGroup(InverseFunctionalIdentifier $iri = null, $name, $data, $format = null, array $context = array())
145
    {
146 2
        $members = array();
147
148 2
        if (isset($data['member'])) {
149 1
            foreach ($data['member'] as $member) {
150 1
                $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context);
151
            }
152
        }
153
154 2
        return new Group($iri, $name, $members);
155
    }
156
}
157