Passed
Pull Request — master (#34)
by Marc
05:44
created

QueryNormalizer::denormalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 4
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\Serializer;
5
6
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
7
use Symfony\Component\Serializer\Exception\LogicException;
8
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
use TBolier\RethinkQL\Query\OptionsInterface;
11
12
class QueryNormalizer extends AbstractNormalizer
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 22
    public function normalize($object, $format = null, array $context = [])
18
    {
19 22
        if ($this->isCircularReference($object, $context)) {
20 1
            return $this->handleCircularReference($object);
21
        }
22
23 21
        if (!$object instanceof \JsonSerializable && !$object instanceof \stdClass) {
24 1
            throw new InvalidArgumentException(sprintf('The '.get_class($object).' must implement "%s".', \JsonSerializable::class));
25
        }
26
27 20
        if (!$this->serializer instanceof NormalizerInterface) {
28 1
            throw new LogicException('Cannot normalize object because injected serializer is not a normalizer');
29
        }
30
31 19
        if ($object instanceof \stdClass) {
32 1
            return (array) $object;
33
        }
34
35 18
        if ($object instanceof OptionsInterface) {
36 17
            return (object) $object->jsonSerialize();
37
        }
38
39 17
        return $this->serializer->normalize($object->jsonSerialize(), $format, $context);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 17
    public function supportsNormalization($data, $format = null)
46
    {
47 17
        return \is_object($data) && $format === 'json';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function supportsDenormalization($data, $type, $format = null)
54
    {
55 1
        return false;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function denormalize($data, $class, $format = null, array $context = [])
62
    {
63 1
        throw new LogicException(sprintf('Cannot denormalize with "%s".', __CLASS__));
64
    }
65
}
66