comparison vendor/symfony/serializer/Normalizer/JsonSerializableNormalizer.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15 use Symfony\Component\Serializer\Exception\LogicException;
16
17 /**
18 * A normalizer that uses an objects own JsonSerializable implementation.
19 *
20 * @author Fred Cox <mcfedr@gmail.com>
21 */
22 class JsonSerializableNormalizer extends AbstractNormalizer
23 {
24 /**
25 * {@inheritdoc}
26 */
27 public function normalize($object, $format = null, array $context = array())
28 {
29 if ($this->isCircularReference($object, $context)) {
30 return $this->handleCircularReference($object);
31 }
32
33 if (!$object instanceof \JsonSerializable) {
34 throw new InvalidArgumentException(sprintf('The object must implement "%s".', \JsonSerializable::class));
35 }
36
37 if (!$this->serializer instanceof NormalizerInterface) {
38 throw new LogicException('Cannot normalize object because injected serializer is not a normalizer');
39 }
40
41 return $this->serializer->normalize($object->jsonSerialize(), $format, $context);
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function supportsNormalization($data, $format = null)
48 {
49 return $data instanceof \JsonSerializable;
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".', \JsonSerializable::class));
66 }
67 }