annotate core/modules/jsonapi/src/Serializer/Serializer.php @ 19:fa3358dc1485 tip

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