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