comparison core/modules/jsonapi/src/Serializer/Serializer.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\jsonapi\Serializer;
4
5 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6 use Symfony\Component\Serializer\Serializer as SymfonySerializer;
7
8 /**
9 * Overrides the Symfony serializer to cordon off our incompatible normalizers.
10 *
11 * This service is for *internal* use only. It is not suitable for *any* reuse.
12 * Backwards compatibility is in no way guaranteed and will almost certainly be
13 * broken in the future.
14 *
15 * @link https://www.drupal.org/project/jsonapi/issues/2923779#comment-12407443
16 *
17 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
18 * class may change at any time and this will break any dependencies on it.
19 *
20 * @see https://www.drupal.org/project/jsonapi/issues/3032787
21 * @see jsonapi.api.php
22 */
23 final class Serializer extends SymfonySerializer {
24
25 /**
26 * A normalizer to fall back on when JSON:API cannot normalize an object.
27 *
28 * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface
29 */
30 protected $fallbackNormalizer;
31
32 /**
33 * {@inheritdoc}
34 */
35 public function __construct(array $normalizers = [], array $encoders = []) {
36 foreach ($normalizers as $normalizer) {
37 if (strpos(get_class($normalizer), 'Drupal\jsonapi\Normalizer') !== 0) {
38 throw new \LogicException('JSON:API does not allow adding more normalizers!');
39 }
40 }
41 parent::__construct($normalizers, $encoders);
42 }
43
44 /**
45 * Adds a secondary normalizer.
46 *
47 * This normalizer will be attempted when JSON:API has no applicable
48 * normalizer.
49 *
50 * @param \Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer
51 * The secondary normalizer.
52 */
53 public function setFallbackNormalizer(NormalizerInterface $normalizer) {
54 $this->fallbackNormalizer = $normalizer;
55 }
56
57 /**
58 * {@inheritdoc}
59 */
60 public function normalize($data, $format = NULL, array $context = []) {
61 if ($this->selfSupportsNormalization($data, $format, $context)) {
62 return parent::normalize($data, $format, $context);
63 }
64 if ($this->fallbackNormalizer->supportsNormalization($data, $format, $context)) {
65 return $this->fallbackNormalizer->normalize($data, $format, $context);
66 }
67 return parent::normalize($data, $format, $context);
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function denormalize($data, $type, $format = NULL, array $context = []) {
74 if ($this->selfSupportsDenormalization($data, $type, $format, $context)) {
75 return parent::denormalize($data, $type, $format, $context);
76 }
77 return $this->fallbackNormalizer->denormalize($data, $type, $format, $context);
78 }
79
80 /**
81 * {@inheritdoc}
82 */
83 public function supportsNormalization($data, $format = NULL, array $context = []) {
84 return $this->selfSupportsNormalization($data, $format, $context) || $this->fallbackNormalizer->supportsNormalization($data, $format, $context);
85 }
86
87 /**
88 * Checks whether this class alone supports normalization.
89 *
90 * @param mixed $data
91 * Data to normalize.
92 * @param string $format
93 * The format being (de-)serialized from or into.
94 * @param array $context
95 * (optional) Options available to the normalizer.
96 *
97 * @return bool
98 * Whether this class supports normalization for the given data.
99 */
100 private function selfSupportsNormalization($data, $format = NULL, array $context = []) {
101 return parent::supportsNormalization($data, $format, $context);
102 }
103
104 /**
105 * {@inheritdoc}
106 */
107 public function supportsDenormalization($data, $type, $format = NULL, array $context = []) {
108 return $this->selfSupportsDenormalization($data, $type, $format, $context) || $this->fallbackNormalizer->supportsDenormalization($data, $type, $format, $context);
109 }
110
111 /**
112 * Checks whether this class alone supports denormalization.
113 *
114 * @param mixed $data
115 * Data to denormalize from.
116 * @param string $type
117 * The class to which the data should be denormalized.
118 * @param string $format
119 * The format being deserialized from.
120 * @param array $context
121 * (optional) Options available to the denormalizer.
122 *
123 * @return bool
124 * Whether this class supports normalization for the given data and type.
125 */
126 private function selfSupportsDenormalization($data, $type, $format = NULL, array $context = []) {
127 return parent::supportsDenormalization($data, $type, $format, $context);
128 }
129
130 }