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
|
|
|
public function normalize($object, $format = null, array $context = []) |
15
|
|
|
{ |
16
|
|
|
if ($this->isCircularReference($object, $context)) { |
17
|
22 |
|
return $this->handleCircularReference($object); |
18
|
|
|
} |
19
|
22 |
|
|
20
|
1 |
|
if (!$object instanceof \JsonSerializable && !$object instanceof \stdClass) { |
21
|
|
|
throw new InvalidArgumentException( |
22
|
|
|
sprintf('The '.get_class($object).' must implement "%s".', \JsonSerializable::class) |
23
|
21 |
|
); |
24
|
1 |
|
} |
25
|
|
|
|
26
|
|
|
if (!$this->serializer instanceof NormalizerInterface) { |
27
|
20 |
|
throw new LogicException('Cannot normalize object because injected serializer is not a normalizer'); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
if ($object instanceof \stdClass) { |
31
|
19 |
|
return (array) $object; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
if ($object instanceof OptionsInterface) { |
35
|
18 |
|
return (object) $object->jsonSerialize(); |
36
|
17 |
|
} |
37
|
|
|
|
38
|
|
|
return $this->serializer->normalize($object->jsonSerialize(), $format, $context); |
39
|
17 |
|
} |
40
|
|
|
|
41
|
|
|
public function supportsNormalization($data, $format = null) |
42
|
|
|
{ |
43
|
|
|
return \is_object($data) && $format === 'json'; |
44
|
|
|
} |
45
|
17 |
|
|
46
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
47
|
17 |
|
{ |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
52
|
|
|
{ |
53
|
1 |
|
throw new LogicException(sprintf('Cannot denormalize with "%s".', __CLASS__)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|