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\Activity; |
15
|
|
|
use Xabbuh\XApi\Model\Object; |
16
|
|
|
use Xabbuh\XApi\Model\Statement; |
17
|
|
|
use Xabbuh\XApi\Model\StatementReference; |
18
|
|
|
use Xabbuh\XApi\Model\SubStatement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Christian Flothmann <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class ObjectNormalizer extends Normalizer |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function normalize($object, $format = null, array $context = array()) |
29
|
|
|
{ |
30
|
|
|
if ($object instanceof Activity) { |
31
|
|
|
$activityData = array( |
32
|
|
|
'objectType' => 'Activity', |
33
|
|
|
'id' => $object->getId(), |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
if (null !== $definition = $object->getDefinition()) { |
37
|
|
|
$activityData['definition'] = $this->normalizeAttribute($definition, $format, $context); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $activityData; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($object instanceof StatementReference) { |
44
|
|
|
return array( |
45
|
|
|
'objectType' => 'StatementRef', |
46
|
|
|
'id' => $object->getStatementId(), |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($object instanceof SubStatement) { |
51
|
|
|
$subStatement = new Statement($object->getId(), $object->getActor(), $object->getVerb(), $object->getObject(), $object->getResult()); |
52
|
|
|
$subStatementData = $this->normalizeAttribute($subStatement, $format, $context); |
53
|
|
|
$subStatementData['objectType'] = 'SubStatement'; |
54
|
|
|
|
55
|
|
|
return $subStatementData; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function supportsNormalization($data, $format = null) |
65
|
|
|
{ |
66
|
|
|
return $data instanceof Object; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
73
|
|
|
{ |
74
|
|
|
if (isset($data['objectType']) && 'Activity' === $data['objectType']) { |
75
|
|
|
$definition = null; |
76
|
|
|
|
77
|
|
|
if (isset($data['definition'])) { |
78
|
|
|
$definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Activity($data['id'], $definition); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) { |
85
|
|
|
$statementData = array( |
86
|
|
|
'actor' => $data['actor'], |
87
|
|
|
'verb' => $data['verb'], |
88
|
|
|
'object' => $data['object'], |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
if (isset($data['result'])) { |
92
|
|
|
$statementData['result'] = $data['result']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @var Statement $statement */ |
96
|
|
|
$statement = $this->denormalizeData($statementData, 'Xabbuh\XApi\Model\Statement', $format, $context); |
97
|
|
|
|
98
|
|
|
return new SubStatement(null, $statement->getActor(), $statement->getVerb(), $statement->getObject(), $statement->getResult()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) { |
102
|
|
|
return new StatementReference($data['id']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
112
|
|
|
{ |
113
|
|
|
return 'Xabbuh\XApi\Model\Object' === $type; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|