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\Exception\LogicException; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
16
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
17
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
18
|
|
|
use Xabbuh\XApi\Model\Attachment; |
19
|
|
|
use Xabbuh\XApi\Model\IRI; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Denormalizes PHP arrays to {@link Attachment} objects. |
23
|
|
|
* |
24
|
|
|
* @author Christian Flothmann <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class AttachmentNormalizer implements DenormalizerInterface, SerializerAwareInterface |
27
|
|
|
{ |
28
|
|
|
private $serializer; |
29
|
|
|
|
30
|
368 |
|
public function setSerializer(SerializerInterface $serializer) |
31
|
|
|
{ |
32
|
368 |
|
$this->serializer = $serializer; |
33
|
368 |
|
} |
34
|
|
|
|
35
|
5 |
|
public function denormalize($data, $class, $format = null, array $context = array()) |
36
|
|
|
{ |
37
|
5 |
|
if (!$this->serializer instanceof DenormalizerInterface) { |
38
|
|
|
throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
$display = $this->serializer->denormalize($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
42
|
5 |
|
$description = null; |
43
|
5 |
|
$fileUrl = null; |
44
|
|
|
|
45
|
5 |
|
if (isset($data['description'])) { |
46
|
|
|
$description = $this->serializer->denormalize($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
if (isset($data['fileUrl'])) { |
50
|
1 |
|
$fileUrl = $this->serializer->denormalize($data['fileUrl'], 'Xabbuh\XApi\Model\IRL', $format, $context); |
51
|
|
|
} |
52
|
|
|
|
53
|
5 |
|
return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl); |
54
|
|
|
} |
55
|
|
|
|
56
|
154 |
|
public function supportsDenormalization($data, $type, $format = null) |
57
|
|
|
{ |
58
|
154 |
|
return 'Xabbuh\XApi\Model\Attachment' === $type; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|