|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xabbuh\XApi\Serializer\Normalizer; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\Exception\LogicException; |
|
6
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
7
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
|
8
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
9
|
|
|
use Xabbuh\XApi\Model\Definition; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Denormalizes PHP arrays to {@link Definition} instances. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Christian Flothmann <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class DefinitionNormalizer implements DenormalizerInterface, SerializerAwareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private $serializer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
324 |
|
public function setSerializer(SerializerInterface $serializer) |
|
24
|
|
|
{ |
|
25
|
324 |
|
$this->serializer = $serializer; |
|
26
|
324 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
13 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
|
32
|
|
|
{ |
|
33
|
13 |
|
if (!$this->serializer instanceof DenormalizerInterface) { |
|
34
|
|
|
throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
13 |
|
$name = null; |
|
38
|
13 |
|
$description = null; |
|
39
|
13 |
|
$type = isset($data['type']) ? $data['type'] : null; |
|
40
|
13 |
|
$moreInfo = isset($data['moreInfo']) ? $data['moreInfo'] : null; |
|
41
|
|
|
|
|
42
|
13 |
|
if (isset($data['name'])) { |
|
43
|
5 |
|
$name = $this->serializer->denormalize($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
13 |
|
if (isset($data['description'])) { |
|
47
|
3 |
|
$description = $this->serializer->denormalize($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
13 |
|
return new Definition($name, $description, $type, $moreInfo); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
132 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
57
|
|
|
{ |
|
58
|
132 |
|
return 'Xabbuh\XApi\Model\Definition' === $type; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|