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 Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
16
|
|
|
use Xabbuh\XApi\Model\Interaction\InteractionComponent; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Denormalizes xAPI statement activity {@link InteractionComponent interaction components}. |
20
|
|
|
* |
21
|
|
|
* @author Christian Flothmann <[email protected]> |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
final class InteractionComponentNormalizer extends Normalizer implements DenormalizerInterface, NormalizerInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function normalize($object, $format = null, array $context = array()) |
29
|
|
|
{ |
30
|
|
|
if (!$object instanceof InteractionComponent) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$data = array( |
35
|
|
|
'id' => $object->getId(), |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if (null !== $description = $object->getDescription()) { |
39
|
|
|
$data['description'] = $this->normalizeAttribute($description, $format, $context); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function supportsNormalization($data, $format = null) |
49
|
|
|
{ |
50
|
|
|
return $data instanceof InteractionComponent; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
57
|
|
|
{ |
58
|
|
|
$description = null; |
59
|
|
|
|
60
|
|
|
if (isset($data['description'])) { |
61
|
|
|
$description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new InteractionComponent($data['id'], $description); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
71
|
|
|
{ |
72
|
|
|
return 'Xabbuh\XApi\Model\Interaction\InteractionComponent' === $type; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.