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
|
|
|
|