You are here

class SerializerDecorator in JSON:API Extras 8.3

A decorated JSON:API serializer, with lazily initialized fallback serializer.

Hierarchy

  • class \Drupal\jsonapi_extras\SerializerDecorator implements \Symfony\Component\Serializer\SerializerInterface, \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface

Expanded class hierarchy of SerializerDecorator

File

src/SerializerDecorator.php, line 15

Namespace

Drupal\jsonapi_extras
View source
class SerializerDecorator implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface {

  /**
   * The decorated JSON:API serializer service.
   *
   * @var \Drupal\jsonapi\Serializer\Serializer
   */
  protected $decoratedSerializer;

  /**
   * Whether the lazy dependency has been initialized.
   *
   * @var bool
   */
  protected $isInitialized = FALSE;

  /**
   * Constructs a SerializerDecorator.
   *
   * @param \Drupal\jsonapi\Serializer\Serializer $serializer
   *   The decorated JSON:API serializer.
   */
  public function __construct(Serializer $serializer) {
    $this->decoratedSerializer = $serializer;
  }

  /**
   * Lazily initializes the fallback serializer for the JSON:API serializer.
   *
   * Breaks circular dependency.
   */
  protected function lazilyInitialize() {
    if (!$this->isInitialized) {
      $core_serializer = \Drupal::service('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 = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function deserialize($data, $type, $format, array $context = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function serialize($data, $format, array $context = []) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {
    return $this
      ->relay(__FUNCTION__, func_get_args());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SerializerDecorator::$decoratedSerializer protected property The decorated JSON:API serializer service.
SerializerDecorator::$isInitialized protected property Whether the lazy dependency has been initialized.
SerializerDecorator::decode public function Decodes a string into PHP data.
SerializerDecorator::denormalize public function Denormalizes data back into an object of the given class.
SerializerDecorator::deserialize public function Deserializes data into the given type.
SerializerDecorator::encode public function Encodes data into the given format.
SerializerDecorator::lazilyInitialize protected function Lazily initializes the fallback serializer for the JSON:API serializer.
SerializerDecorator::normalize public function Normalizes an object into a set of arrays/scalars.
SerializerDecorator::relay protected function Relays a method call to the decorated service.
SerializerDecorator::serialize public function Serializes data in the appropriate format.
SerializerDecorator::supportsDecoding public function Checks whether the deserializer can decode from given format.
SerializerDecorator::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer.
SerializerDecorator::supportsEncoding public function Checks whether the serializer can encode to given format.
SerializerDecorator::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer.
SerializerDecorator::__construct public function Constructs a SerializerDecorator.