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\Symfony\Normalizer; |
13
|
|
|
|
14
|
|
|
use Xabbuh\XApi\Model\Context; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Normalizes and denormalizes xAPI statement contexts. |
18
|
|
|
* |
19
|
|
|
* @author Christian Flothmann <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ContextNormalizer extends Normalizer |
22
|
|
|
{ |
23
|
|
|
public function normalize($object, $format = null, array $context = array()) |
24
|
|
|
{ |
25
|
|
|
if (!$object instanceof Context) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$data = array(); |
30
|
|
|
|
31
|
|
|
if (null !== $registration = $object->getRegistration()) { |
32
|
|
|
$data['registration'] = $registration; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (null !== $instructor = $object->getInstructor()) { |
36
|
|
|
$data['instructor'] = $this->normalizeAttribute($instructor, $format, $context); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (null !== $team = $object->getTeam()) { |
40
|
|
|
$data['team'] = $this->normalizeAttribute($team, $format, $context); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (null !== $contextActivities = $object->getContextActivities()) { |
44
|
|
|
$data['contextActivities'] = $this->normalizeAttribute($contextActivities, $format, $context); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (null !== $revision = $object->getRevision()) { |
48
|
|
|
$data['revision'] = $revision; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (null !== $platform = $object->getPlatform()) { |
52
|
|
|
$data['platform'] = $platform; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (null !== $language = $object->getLanguage()) { |
56
|
|
|
$data['language'] = $language; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (null !== $statement = $object->getStatement()) { |
60
|
|
|
$data['statement'] = $this->normalizeAttribute($statement, $format, $context); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
if (null !== $extensions = $object->getExtensions()) { |
|
|
|
|
64
|
|
|
$data['extensions'] = $this->normalizeAttribute($extensions, $format, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (empty($data)) { |
68
|
|
|
return new \stdClass(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function supportsNormalization($data, $format = null) |
75
|
|
|
{ |
76
|
|
|
return $data instanceof Context; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
80
|
|
|
{ |
81
|
|
|
$statementContext = new Context(); |
82
|
|
|
|
83
|
|
|
if (isset($data['registration'])) { |
84
|
|
|
$statementContext = $statementContext->withRegistration($data['registration']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (isset($data['instructor'])) { |
88
|
|
|
$statementContext = $statementContext->withInstructor($this->denormalizeData($data['instructor'], 'Xabbuh\XApi\Model\Actor', $format, $context)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (isset($data['team'])) { |
92
|
|
|
$statementContext = $statementContext->withTeam($this->denormalizeData($data['team'], 'Xabbuh\XApi\Model\Group', $format, $context)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (isset($data['contextActivities'])) { |
96
|
|
|
$statementContext = $statementContext->withContextActivities($this->denormalizeData($data['contextActivities'], 'Xabbuh\XApi\Model\ContextActivities', $format, $context)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (isset($data['revision'])) { |
100
|
|
|
$statementContext = $statementContext->withRevision($data['revision']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (isset($data['platform'])) { |
104
|
|
|
$statementContext = $statementContext->withPlatform($data['platform']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (isset($data['language'])) { |
108
|
|
|
$statementContext = $statementContext->withLanguage($data['language']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (isset($data['statement'])) { |
112
|
|
|
$statementContext = $statementContext->withStatement($this->denormalizeData($data['statement'], 'Xabbuh\XApi\Model\StatementReference', $format, $context)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
if (isset($data['extensions'])) { |
|
|
|
|
116
|
|
|
$statementContext = $statementContext->withExtensions($this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $statementContext; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
123
|
|
|
{ |
124
|
|
|
return 'Xabbuh\XApi\Model\Context' === $type; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.