Passed
Branch master (69e5ee)
by Timon
05:22
created

QueryNormalizer::supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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