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\Statement; |
15
|
|
|
use Xabbuh\XApi\Model\StatementId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Normalizes and denormalizes xAPI statements. |
19
|
|
|
* |
20
|
|
|
* @author Christian Flothmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class StatementNormalizer extends Normalizer |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
14 |
|
public function normalize($object, $format = null, array $context = array()) |
28
|
|
|
{ |
29
|
14 |
|
if (!$object instanceof Statement) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$data = array( |
34
|
14 |
|
'actor' => $this->normalizeAttribute($object->getActor(), $format, $context), |
35
|
14 |
|
'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context), |
36
|
14 |
|
'object' => $this->normalizeAttribute($object->getObject(), $format, $context), |
37
|
|
|
); |
38
|
|
|
|
39
|
14 |
|
if (null !== $id = $object->getId()) { |
40
|
13 |
|
$data['id'] = $id->getValue(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
14 |
|
if (null !== $authority = $object->getAuthority()) { |
44
|
3 |
|
$data['authority'] = $this->normalizeAttribute($authority, $format, $context); |
45
|
|
|
} |
46
|
|
|
|
47
|
14 |
|
if (null !== $result = $object->getResult()) { |
48
|
2 |
|
$data['result'] = $this->normalizeAttribute($result, $format, $context); |
49
|
|
|
} |
50
|
|
|
|
51
|
14 |
|
if (null !== $result = $object->getCreated()) { |
52
|
2 |
|
$data['timestamp'] = $this->normalizeAttribute($result, $format, $context); |
53
|
|
|
} |
54
|
|
|
|
55
|
14 |
|
if (null !== $result = $object->getStored()) { |
56
|
1 |
|
$data['stored'] = $this->normalizeAttribute($result, $format, $context); |
57
|
|
|
} |
58
|
|
|
|
59
|
14 |
|
if (null !== $object->getContext()) { |
60
|
1 |
|
$data['context'] = $this->normalizeAttribute($object->getContext(), $format, $context); |
61
|
|
|
} |
62
|
|
|
|
63
|
14 |
|
if (null !== $attachments = $object->getAttachments()) { |
|
|
|
|
64
|
2 |
|
$data['attachments'] = $this->normalizeAttribute($attachments, $format, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
14 |
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
97 |
|
public function supportsNormalization($data, $format = null) |
74
|
|
|
{ |
75
|
97 |
|
return $data instanceof Statement; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
15 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
82
|
|
|
{ |
83
|
15 |
|
$id = isset($data['id']) ? StatementId::fromString($data['id']) : null; |
84
|
15 |
|
$actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context); |
85
|
15 |
|
$verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context); |
86
|
15 |
|
$object = $this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context); |
87
|
15 |
|
$result = null; |
88
|
15 |
|
$authority = null; |
89
|
15 |
|
$created = null; |
90
|
15 |
|
$stored = null; |
91
|
15 |
|
$statementContext = null; |
92
|
15 |
|
$attachments = null; |
93
|
|
|
|
94
|
15 |
|
if (isset($data['result'])) { |
95
|
2 |
|
$result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context); |
96
|
|
|
} |
97
|
|
|
|
98
|
15 |
|
if (isset($data['authority'])) { |
99
|
3 |
|
$authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context); |
100
|
|
|
} |
101
|
|
|
|
102
|
15 |
|
if (isset($data['timestamp'])) { |
103
|
2 |
|
$created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context); |
104
|
|
|
} |
105
|
|
|
|
106
|
15 |
|
if (isset($data['stored'])) { |
107
|
1 |
|
$stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context); |
108
|
|
|
} |
109
|
|
|
|
110
|
15 |
|
if (isset($data['context'])) { |
111
|
1 |
|
$statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context); |
112
|
|
|
} |
113
|
|
|
|
114
|
15 |
|
if (isset($data['attachments'])) { |
115
|
2 |
|
$attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context); |
116
|
|
|
} |
117
|
|
|
|
118
|
15 |
|
return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
98 |
|
public function supportsDenormalization($data, $type, $format = null) |
125
|
|
|
{ |
126
|
98 |
|
return 'Xabbuh\XApi\Model\Statement' === $type; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.