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