|
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\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
|
1 |
|
public function normalize($object, $format = null, array $context = array()) |
|
27
|
|
|
{ |
|
28
|
1 |
|
if (!$object instanceof Result) { |
|
29
|
|
|
return null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
$data = array(); |
|
33
|
|
|
|
|
34
|
1 |
|
if (null !== $object->getScore()) { |
|
35
|
1 |
|
$data['score'] = $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
if (null !== $success = $object->getSuccess()) { |
|
39
|
|
|
$data['success'] = $success; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
if (null !== $completion = $object->getCompletion()) { |
|
43
|
|
|
$data['completion'] = $completion; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if (null !== $response = $object->getResponse()) { |
|
47
|
|
|
$data['response'] = $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
if (null !== $duration = $object->getDuration()) { |
|
51
|
1 |
|
$data['duration'] = $duration; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $data; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function supportsNormalization($data, $format = null) |
|
61
|
|
|
{ |
|
62
|
8 |
|
return $data instanceof Result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$score = isset($data['score']) ? $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context) : null; |
|
71
|
1 |
|
$success = isset($data['success']) ? $data['success'] : null; |
|
72
|
1 |
|
$completion = isset($data['completion']) ? $data['completion'] : null; |
|
73
|
1 |
|
$response = isset($data['response']) ? $data['response'] : null; |
|
74
|
1 |
|
$duration = isset($data['duration']) ? $data['duration'] : null; |
|
75
|
|
|
|
|
76
|
1 |
|
return new Result($score, $success, $completion, $response, $duration); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
9 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
83
|
|
|
{ |
|
84
|
9 |
|
return 'Xabbuh\XApi\Model\Result' === $type; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|