Completed
Pull Request — master (#1)
by Christian
02:56
created

Normalizer::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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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 Symfony\Component\Serializer\Exception\LogicException;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17
use Symfony\Component\Serializer\SerializerAwareInterface;
18
use Symfony\Component\Serializer\SerializerInterface;
19
20
/**
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
abstract class Normalizer implements DenormalizerInterface, NormalizerInterface, SerializerAwareInterface
24
{
25
    private $serializer;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 24
    public function setSerializer(SerializerInterface $serializer)
31
    {
32 24
        $this->serializer = $serializer;
33 24
    }
34
35 9
    protected function normalizeAttribute($value, $format = null, array $context = array())
36
    {
37 9
        if (!$this->serializer instanceof NormalizerInterface) {
38
            throw new LogicException('Cannot normalize attribute because the injected serializer is not a normalizer');
39
        }
40
41 9
        return $this->serializer->normalize($value, $format, $context);
42
    }
43
44 10
    protected function denormalizeData($data, $type, $format = null, array $context = array())
45
    {
46 10
        if (!$this->serializer instanceof DenormalizerInterface) {
47
            throw new LogicException('Cannot denormalize because the injected serializer is not a denormalizer');
48
        }
49
50 10
        return $this->serializer->denormalize($data, $type, $format, $context);
51
    }
52
}
53