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