Completed
Push — master ( 8865c9...169b65 )
by Christian
03:22
created

AttachmentNormalizer::setSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\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 5
    public function normalize($object, $format = null, array $context = array())
29
    {
30 5
        if (!$object instanceof Attachment) {
31
            return;
32
        }
33
34
        $data = array(
35 5
            'usageType' => $object->getUsageType()->getValue(),
36 5
            'contentType' => $object->getContentType(),
37 5
            'length' => $object->getLength(),
38 5
            'sha2' => $object->getSha2(),
39 5
            'display' => $this->normalizeAttribute($object->getDisplay(), $format, $context),
40
        );
41
42 5
        if (null !== $description = $object->getDescription()) {
43
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
44
        }
45
46 5
        if (null !== $fileUrl = $object->getFileUrl()) {
47 1
            $data['fileUrl'] = $fileUrl->getValue();
48
        }
49
50 5
        return $data;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 150
    public function supportsNormalization($data, $format = null)
57
    {
58 150
        return $data instanceof Attachment;
59
    }
60
61 5
    public function denormalize($data, $class, $format = null, array $context = array())
62
    {
63 5
        $display = $this->denormalizeData($data['display'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
64 5
        $description = null;
65 5
        $fileUrl = null;
66
67 5
        if (isset($data['description'])) {
68
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
69
        }
70
71 5
        if (isset($data['fileUrl'])) {
72 1
            $fileUrl = IRL::fromString($data['fileUrl']);
73
        }
74
75 5
        return new Attachment(IRI::fromString($data['usageType']), $data['contentType'], $data['length'], $data['sha2'], $display, $description, $fileUrl);
76
    }
77
78 154
    public function supportsDenormalization($data, $type, $format = null)
79
    {
80 154
        return 'Xabbuh\XApi\Model\Attachment' === $type;
81
    }
82
}
83