ActorNormalizer::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 Symfony\Component\Serializer\Exception\InvalidArgumentException;
15
use Xabbuh\XApi\Model\Actor;
16
use Xabbuh\XApi\Model\Agent;
17
use Xabbuh\XApi\Model\Group;
18
use Xabbuh\XApi\Model\InverseFunctionalIdentifier;
19
use Xabbuh\XApi\Model\IRI;
20
21
/**
22
 * Normalizes and denormalizes xAPI statement actors.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
final class ActorNormalizer extends Normalizer
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function normalize($object, $format = null, array $context = array())
32
    {
33
        if (!$object instanceof Actor) {
34
            return null;
35
        }
36
37
        $data = array();
38
39
        $this->normalizeInverseFunctionalIdentifier($object->getInverseFunctionalIdentifier(), $data, $format, $context);
40
41
        if (null !== $name = $object->getName()) {
42
            $data['name'] = $name;
43
        }
44
45
        if ($object instanceof Group) {
46
            $members = array();
47
48
            foreach ($object->getMembers() as $member) {
49
                $members[] = $this->normalize($member);
50
            }
51
52
            if (count($members) > 0) {
53
                $data['member'] = $members;
54
            }
55
56
            $data['objectType'] = 'Group';
57
        } else {
58
            $data['objectType'] = 'Agent';
59
        }
60
61
        return $data;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function supportsNormalization($data, $format = null)
68
    {
69
        return $data instanceof Actor;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function denormalize($data, $class, $format = null, array $context = array())
76
    {
77
        $inverseFunctionalIdentifier = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context);
78
        $name = isset($data['name']) ? $data['name'] : null;
79
80
        if (isset($data['objectType']) && 'Group' === $data['objectType']) {
81
            return $this->denormalizeGroup($inverseFunctionalIdentifier, $name, $data, $format, $context);
82
        }
83
84
        if (null === $inverseFunctionalIdentifier) {
85
            throw new InvalidArgumentException('Missing inverse functional identifier for agent.');
86
        }
87
88
        return new Agent($inverseFunctionalIdentifier, $name);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function supportsDenormalization($data, $type, $format = null)
95
    {
96
        return 'Xabbuh\XApi\Model\Actor' === $type || 'Xabbuh\XApi\Model\Agent' === $type || 'Xabbuh\XApi\Model\Group' === $type;
97
    }
98
99
    private function normalizeInverseFunctionalIdentifier(InverseFunctionalIdentifier $iri = null, &$data, $format = null, array $context = array())
100
    {
101
        if (null === $iri) {
102
            return;
103
        }
104
105
        if (null !== $mbox = $iri->getMbox()) {
106
            $data['mbox'] = $mbox->getValue();
107
        }
108
109
        if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) {
110
            $data['mbox_sha1sum'] = $mboxSha1Sum;
111
        }
112
113
        if (null !== $openId = $iri->getOpenId()) {
114
            $data['openid'] = $openId;
115
        }
116
117
        if (null !== $account = $iri->getAccount()) {
118
            $data['account'] = $this->normalizeAttribute($account, $format, $context);
119
        }
120
    }
121
122
    private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array())
123
    {
124
        if (isset($data['mbox'])) {
125
            return InverseFunctionalIdentifier::withMbox(IRI::fromString($data['mbox']));
126
        }
127
128
        if (isset($data['mbox_sha1sum'])) {
129
            return InverseFunctionalIdentifier::withMboxSha1Sum($data['mbox_sha1sum']);
130
        }
131
132
        if (isset($data['openid'])) {
133
            return InverseFunctionalIdentifier::withOpenId($data['openid']);
134
        }
135
136
        if (isset($data['account'])) {
137
            return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context));
138
        }
139
    }
140
141
    private function denormalizeAccount($data, $format = null, array $context = array())
142
    {
143
        if (!isset($data['account'])) {
144
            return null;
145
        }
146
147
        return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context);
148
    }
149
150
    private function denormalizeGroup(InverseFunctionalIdentifier $iri = null, $name, $data, $format = null, array $context = array())
151
    {
152
        $members = array();
153
154
        if (isset($data['member'])) {
155
            foreach ($data['member'] as $member) {
156
                $members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context);
157
            }
158
        }
159
160
        return new Group($iri, $name, $members);
161
    }
162
}
163