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 TBolier\RethinkQL\Query\MessageInterface; |
12
|
|
|
use TBolier\RethinkQL\Query\OptionsInterface; |
13
|
|
|
|
14
|
|
|
class QueryNormalizer extends AbstractNormalizer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
13 |
|
public function normalize($object, $format = null, array $context = array()) |
20
|
|
|
{ |
21
|
13 |
|
if ($this->isCircularReference($object, $context)) { |
22
|
|
|
return $this->handleCircularReference($object); |
23
|
|
|
} |
24
|
|
|
|
25
|
13 |
|
if (!$object instanceof \JsonSerializable && !$object instanceof \stdClass) { |
26
|
|
|
throw new InvalidArgumentException(sprintf('The ' . get_class($object) . ' must implement "%s".', \JsonSerializable::class)); |
27
|
|
|
} |
28
|
|
|
|
29
|
13 |
|
if (!$this->serializer instanceof NormalizerInterface) { |
30
|
|
|
throw new LogicException('Cannot normalize object because injected serializer is not a normalizer'); |
31
|
|
|
} |
32
|
|
|
|
33
|
13 |
|
if ($object instanceof \stdClass) { |
34
|
|
|
return (array)$object; |
35
|
|
|
} |
36
|
|
|
|
37
|
13 |
|
if ($object instanceof OptionsInterface) { |
38
|
13 |
|
return (object)$object->jsonSerialize(); |
39
|
|
|
} |
40
|
|
|
|
41
|
13 |
|
return $this->serializer->normalize($object->jsonSerialize(), $format, $context); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
13 |
|
public function supportsNormalization($data, $format = null) |
48
|
|
|
{ |
49
|
13 |
|
return \is_object($data) && $format === 'json'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
56
|
|
|
{ |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function denormalize($data, $class, $format = null, array $context = array()) |
64
|
|
|
{ |
65
|
|
|
throw new LogicException(sprintf('Cannot denormalize with "%s".', __CLASS__)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|