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