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\Account; |
15
|
|
|
use Xabbuh\XApi\Model\Actor; |
16
|
|
|
use Xabbuh\XApi\Model\Agent; |
17
|
|
|
use Xabbuh\XApi\Model\Group; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Christian Flothmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class ActorNormalizer extends Normalizer |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
10 |
|
public function normalize($object, $format = null, array $context = array()) |
28
|
|
|
{ |
29
|
10 |
|
if (!$object instanceof Actor) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
10 |
|
$data = array(); |
34
|
|
|
|
35
|
10 |
|
if (null !== $mbox = $object->getMbox()) { |
36
|
10 |
|
$data['mbox'] = $mbox; |
37
|
|
|
} |
38
|
|
|
|
39
|
10 |
|
if (null !== $mboxSha1Sum = $object->getMboxSha1Sum()) { |
40
|
|
|
$data['mbox_sha1sum'] = $mboxSha1Sum; |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
if (null !== $openId = $object->getOpenId()) { |
44
|
1 |
|
$data['openid'] = $openId; |
45
|
|
|
} |
46
|
|
|
|
47
|
10 |
|
if (null !== $account = $object->getAccount()) { |
48
|
3 |
|
$data['account'] = $this->normalizeAttribute($account); |
49
|
|
|
} |
50
|
|
|
|
51
|
10 |
|
if (null !== $name = $object->getName()) { |
52
|
2 |
|
$data['name'] = $name; |
53
|
|
|
} |
54
|
|
|
|
55
|
10 |
|
if ($object instanceof Group) { |
56
|
2 |
|
$data['member'] = array(); |
57
|
|
|
|
58
|
2 |
|
foreach ($object->getMembers() as $member) { |
59
|
2 |
|
$data['member'][] = $this->normalize($member); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$data['objectType'] = 'Group'; |
63
|
|
|
} else { |
64
|
10 |
|
$data['objectType'] = 'Agent'; |
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
11 |
|
public function supportsNormalization($data, $format = null) |
74
|
|
|
{ |
75
|
11 |
|
return $data instanceof Actor; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
12 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
82
|
|
|
{ |
83
|
12 |
|
$mbox = isset($data['mbox']) ? $data['mbox'] : null; |
84
|
12 |
|
$mboxSha1Sum = isset($data['mboxSha1Sum']) ? $data['mboxSha1Sum'] : null; |
85
|
12 |
|
$openId = isset($data['openid']) ? $data['openid'] : null; |
86
|
12 |
|
$name = isset($data['name']) ? $data['name'] : null; |
87
|
12 |
|
$account = $this->denormalizeAccount($data, $format, $context); |
88
|
|
|
|
89
|
11 |
|
if (isset($data['objectType']) && 'Group' === $data['objectType']) { |
90
|
1 |
|
return $this->denormalizeGroup($mbox, $mboxSha1Sum, $openId, $account, $name, $data, $format, $context); |
91
|
|
|
} |
92
|
|
|
|
93
|
11 |
|
return new Agent($mbox, $mboxSha1Sum, $openId, $account, $name); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
13 |
|
public function supportsDenormalization($data, $type, $format = null) |
100
|
|
|
{ |
101
|
13 |
|
return 'Xabbuh\XApi\Model\Actor' === $type; |
102
|
|
|
} |
103
|
|
|
|
104
|
12 |
|
private function denormalizeAccount($data, $format = null, array $context = array()) |
105
|
|
|
{ |
106
|
12 |
|
if (!isset($data['account'])) { |
107
|
11 |
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
return $this->denormalizeData($data['account'], 'Xabbuh\XApi\Mode\Account', $format,$context); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
private function denormalizeGroup($mbox, $mboxSha1Sum, $openId, Account $account = null, $name, $data, $format = null, array $context = array()) |
114
|
|
|
{ |
115
|
1 |
|
$members = array(); |
116
|
|
|
|
117
|
1 |
|
if (isset($data['member'])) { |
118
|
1 |
|
foreach ($data['member'] as $member) { |
119
|
1 |
|
$members[] = $this->denormalize($member, 'Xabbuh\XApi\Model\Agent', $format, $context); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new Group($mbox, $mboxSha1Sum, $openId, $account, $name, $members); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|