src/SerializerDecorator.php | 63 ++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/src/SerializerDecorator.php b/src/SerializerDecorator.php index 440652e..a57dd01 100644 --- a/src/SerializerDecorator.php +++ b/src/SerializerDecorator.php @@ -9,6 +9,9 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Drupal\jsonapi\Serializer\Serializer; +/** + * A decorated JSON API serializer, with lazily initialized fallback serializer. + */ class SerializerDecorator implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface { /** @@ -16,7 +19,7 @@ class SerializerDecorator implements SerializerInterface, NormalizerInterface, D * * @var \Drupal\jsonapi\Serializer\Serializer */ - protected $serializer; + protected $decoratedSerializer; /** * Whether the lazy dependency has been initialized. @@ -25,8 +28,14 @@ class SerializerDecorator implements SerializerInterface, NormalizerInterface, D */ protected $isInitialized = FALSE; + /** + * Constructs a SerializerDecorator. + * + * @param \Drupal\jsonapi\Serializer\Serializer $serializer + * The decorated JSON API serializer. + */ public function __construct(Serializer $serializer) { - $this->serializer = $serializer; + $this->decoratedSerializer = $serializer; } /** @@ -37,89 +46,95 @@ class SerializerDecorator implements SerializerInterface, NormalizerInterface, D protected function lazilyInitialize() { if (!$this->isInitialized) { $core_serializer = \Drupal::service('serializer'); - $this->serializer->setFallbackNormalizer($core_serializer); + $this->decoratedSerializer->setFallbackNormalizer($core_serializer); $this->isInitialized = TRUE; } } /** + * Relays a method call to the decorated service. + * + * @param string $method_name + * The method to invoke on the decorated serializer. + * @param array $args + * The arguments to pass to the invoked method on the decorated serializer. + * + * @return mixed + * The return value. + */ + protected function relay($method_name, array $args) { + $this->lazilyInitialize(); + return call_user_func_array([$this->decoratedSerializer, $method_name], $args); + } + + /** * {@inheritdoc} */ public function decode($data, $format, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->decode($data, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function denormalize($data, $class, $format = NULL, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->denormalize($data, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function deserialize($data, $type, $format, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->deserialize($data, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function encode($data, $format, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->encode($data, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function normalize($object, $format = NULL, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->normalize($object, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function supportsDecoding($format) { - $this->lazilyInitialize(); - return $this->serializer->supportsDecoding($format); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function serialize($data, $format, array $context = []) { - $this->lazilyInitialize(); - return $this->serializer->serialize($data, $format, $context); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function supportsDenormalization($data, $type, $format = NULL) { - $this->lazilyInitialize(); - return $this->serializer->supportsDenormalization($data, $type, $format); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function supportsEncoding($format) { - $this->lazilyInitialize(); - return $this->serializer->supportsEncoding($format); + return $this->relay(__FUNCTION__, func_get_args()); } /** * {@inheritdoc} */ public function supportsNormalization($data, $format = NULL) { - $this->lazilyInitialize(); - return $this->serializer->supportsNormalization($data, $format); + return $this->relay(__FUNCTION__, func_get_args()); } -} \ No newline at end of file +}