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