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->normalizeInverseFunctionIdentifier($object->getInverseFunctionalIdentifier(), $data); |
38
|
|
|
|
39
|
10 |
|
if (null !== $name = $object->getName()) { |
40
|
3 |
|
$data['name'] = $name; |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
if ($object instanceof Group) { |
44
|
2 |
|
$data['member'] = array(); |
45
|
|
|
|
46
|
2 |
|
foreach ($object->getMembers() as $member) { |
47
|
1 |
|
$data['member'][] = $this->normalize($member); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$data['objectType'] = 'Group'; |
51
|
|
|
} else { |
52
|
10 |
|
$data['objectType'] = 'Agent'; |
53
|
|
|
} |
54
|
|
|
|
55
|
10 |
|
return $data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
11 |
|
public function supportsNormalization($data, $format = null) |
62
|
|
|
{ |
63
|
11 |
|
return $data instanceof Actor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
12 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
70
|
|
|
{ |
71
|
12 |
|
$iri = $this->denormalizeInverseFunctionalIdentifier($data, $format, $context); |
72
|
12 |
|
$name = isset($data['name']) ? $data['name'] : null; |
73
|
|
|
|
74
|
12 |
|
if (isset($data['objectType']) && 'Group' === $data['objectType']) { |
75
|
2 |
|
return $this->denormalizeGroup($iri, $name, $data, $format, $context); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
12 |
|
return new Agent($iri, $name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
13 |
|
public function supportsDenormalization($data, $type, $format = null) |
85
|
|
|
{ |
86
|
13 |
|
return 'Xabbuh\XApi\Model\Actor' === $type; |
87
|
|
|
} |
88
|
|
|
|
89
|
10 |
|
private function normalizeInverseFunctionIdentifier(InverseFunctionalIdentifier $iri, &$data) |
90
|
|
|
{ |
91
|
10 |
|
if (null !== $mbox = $iri->getMbox()) { |
92
|
9 |
|
$data['mbox'] = $mbox; |
93
|
|
|
} |
94
|
|
|
|
95
|
10 |
|
if (null !== $mboxSha1Sum = $iri->getMboxSha1Sum()) { |
96
|
|
|
$data['mbox_sha1sum'] = $mboxSha1Sum; |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
if (null !== $openId = $iri->getOpenId()) { |
100
|
1 |
|
$data['openid'] = $openId; |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
if (null !== $account = $iri->getAccount()) { |
104
|
3 |
|
$data['account'] = $this->normalizeAttribute($account); |
105
|
|
|
} |
106
|
10 |
|
} |
107
|
|
|
|
108
|
12 |
|
private function denormalizeInverseFunctionalIdentifier($data, $format = null, array $context = array()) |
109
|
|
|
{ |
110
|
12 |
|
if (isset($data['mbox'])) { |
111
|
11 |
|
return InverseFunctionalIdentifier::withMbox($data['mbox']); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
if (isset($data['mboxSha1Sum'])) { |
115
|
|
|
return InverseFunctionalIdentifier::withMboxSha1Sum($data['mboxSha1Sum']); |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
if (isset($data['openid'])) { |
119
|
1 |
|
return InverseFunctionalIdentifier::withOpenId($data['openid']); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
if (isset($data['account'])) { |
123
|
2 |
|
return InverseFunctionalIdentifier::withAccount($this->denormalizeAccount($data, $format, $context)); |
124
|
|
|
} |
125
|
1 |
|
} |
126
|
|
|
|
127
|
2 |
|
private function denormalizeAccount($data, $format = null, array $context = array()) |
128
|
|
|
{ |
129
|
2 |
|
if (!isset($data['account'])) { |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Model\Account', $format, $context); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
private function denormalizeGroup(InverseFunctionalIdentifier $iri, $name, $data, $format = null, array $context = array()) |
137
|
|
|
{ |
138
|
1 |
|
$members = array(); |
139
|
|
|
|
140
|
1 |
|
if (isset($data['member'])) { |
141
|
1 |
|
foreach ($data['member'] as $member) { |
142
|
1 |
|
$members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return new Group($iri, $name, $members); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: