AttachmentNormalizer::denormalize()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 8
nop 4
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\Attachment;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\IRL;
17
18
/**
19
 * Denormalizes PHP arrays to {@link Attachment} objects.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class AttachmentNormalizer extends Normalizer
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function normalize($object, $format = null, array $context = array())
29
    {
30
        if (!$object instanceof Attachment) {
31
            return;
32
        }
33
34
        $data = array(
35
            'usageType' => $object->getUsageType()->getValue(),
36
            'contentType' => $object->getContentType(),
37
            'length' => $object->getLength(),
38
            'sha2' => $object->getSha2(),
39
            'display' => $this->normalizeAttribute($object->getDisplay(), $format, $context),
40
        );
41
42
        if (null !== $description = $object->getDescription()) {
43
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
44
        }
45
46
        if (null !== $fileUrl = $object->getFileUrl()) {
47
            $data['fileUrl'] = $fileUrl->getValue();
48
        }
49
50
        return $data;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supportsNormalization($data, $format = null)
57
    {
58
        return $data instanceof Attachment;
59
    }
60
61
    public function denormalize($data, $class, $format = null, array $context = array())
62
    {
63
        $display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
64
        $description = null;
65
        $fileUrl = null;
66
        $content = null;
67
68
        if (isset($data['description'])) {
69
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
70
        }
71
72
        if (isset($data['fileUrl'])) {
73
            $fileUrl = IRL::fromString($data['fileUrl']);
74
        }
75
76
        if (isset($context['xapi_attachments'][$data['sha2']])) {
77
            $content = $context['xapi_attachments'][$data['sha2']]['content'];
78
        }
79
80
        return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl, $content);
81
    }
82
83
    public function supportsDenormalization($data, $type, $format = null)
84
    {
85
        return 'Xabbuh\XApi\Model\Attachment' === $type;
86
    }
87
}
88