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\Result; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Normalizes and denormalizes xAPI statement results. |
18
|
|
|
* |
19
|
|
|
* @author Christian Flothmann <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ResultNormalizer extends Normalizer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function normalize($object, $format = null, array $context = array()) |
27
|
|
|
{ |
28
|
|
|
if (!$object instanceof Result) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$data = array(); |
33
|
|
|
|
34
|
|
|
if (null !== $object->getScore()) { |
35
|
|
|
$data['score'] = $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (null !== $success = $object->getSuccess()) { |
39
|
|
|
$data['success'] = $success; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (null !== $completion = $object->getCompletion()) { |
43
|
|
|
$data['completion'] = $completion; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (null !== $response = $object->getResponse()) { |
47
|
|
|
$data['response'] = $response; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (null !== $duration = $object->getDuration()) { |
51
|
|
|
$data['duration'] = $duration; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
if (null !== $extensions = $object->getExtensions()) { |
|
|
|
|
55
|
|
|
$data['extensions'] = $this->normalizeAttribute($extensions, 'Xabbuh\XApi\Model\Extensions', $context); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($data)) { |
59
|
|
|
return new \stdClass(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function supportsNormalization($data, $format = null) |
69
|
|
|
{ |
70
|
|
|
return $data instanceof Result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
77
|
|
|
{ |
78
|
|
|
$score = isset($data['score']) ? $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context) : null; |
79
|
|
|
$success = isset($data['success']) ? $data['success'] : null; |
80
|
|
|
$completion = isset($data['completion']) ? $data['completion'] : null; |
81
|
|
|
$response = isset($data['response']) ? $data['response'] : null; |
82
|
|
|
$duration = isset($data['duration']) ? $data['duration'] : null; |
83
|
|
|
$extensions = isset($data['extensions']) ? $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context) : null; |
84
|
|
|
|
85
|
|
|
return new Result($score, $success, $completion, $response, $duration, $extensions); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
92
|
|
|
{ |
93
|
|
|
return 'Xabbuh\XApi\Model\Result' === $type; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.