You are here

class AnnotationNormalizer in JSON-RPC 8

Same name and namespace in other branches
  1. 2.x modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php \Drupal\jsonrpc_discovery\Normalizer\AnnotationNormalizer

The normalizer class for annotated objects.

Hierarchy

Expanded class hierarchy of AnnotationNormalizer

1 file declares its use of AnnotationNormalizer
DiscoveryController.php in modules/jsonrpc_discovery/src/Controller/DiscoveryController.php
1 string reference to 'AnnotationNormalizer'
jsonrpc_discovery.services.yml in modules/jsonrpc_discovery/jsonrpc_discovery.services.yml
modules/jsonrpc_discovery/jsonrpc_discovery.services.yml
1 service uses AnnotationNormalizer
serializer.normalizer.jsonrpc_annotation in modules/jsonrpc_discovery/jsonrpc_discovery.services.yml
Drupal\jsonrpc_discovery\Normalizer\AnnotationNormalizer

File

modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php, line 15

Namespace

Drupal\jsonrpc_discovery\Normalizer
View source
class AnnotationNormalizer extends NormalizerBase {
  const DEPTH_KEY = __CLASS__ . '_depth';

  /**
   * The serializer service.
   *
   * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface
   */
  protected $serializer;

  /**
   * {@inheritdoc}
   */
  protected $format = 'json';

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = [
    JsonRpcMethod::class,
    JsonRpcParameterDefinition::class,
  ];

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    $attributes = [];
    foreach ($object as $key => $value) {
      switch ($key) {
        case 'id':
        case 'call':
        case 'access':
          break;
        default:
          $child = $value instanceof AnnotationInterface ? $value
            ->get() : $value;
          if (isset($context[static::DEPTH_KEY]) && $child instanceof AnnotationInterface || is_array($child) && Inspector::assertAllObjects($child, AnnotationInterface::class)) {
            if ($context[static::DEPTH_KEY] === 0) {
              break;
            }
            $context[static::DEPTH_KEY] -= 1;
          }
          $attributes[$key] = $this->serializer
            ->normalize($child, $format, $context);
      }
    }
    $normalized = [
      'type' => static::getAnnotationType($object),
      'id' => $object
        ->getId(),
      'attributes' => array_filter($attributes),
    ];
    if ($object instanceof JsonRpcMethod) {
      $self = Url::fromRoute('jsonrpc.method_resource', [
        'method_id' => $object
          ->id(),
      ])
        ->setAbsolute()
        ->toString(TRUE);
      $collection = Url::fromRoute('jsonrpc.method_collection')
        ->setAbsolute()
        ->toString(TRUE);
      $this
        ->addCacheableDependency($context, $self);
      $this
        ->addCacheableDependency($context, $collection);
      $normalized['links'] = [
        'self' => $self
          ->getGeneratedUrl(),
        'collection' => $collection
          ->getGeneratedUrl(),
      ];
    }
    if ($object instanceof JsonRpcParameterDefinition) {
      $normalized['schema'] = $object
        ->getSchema();
    }
    return $normalized;
  }

  /**
   * Extract the annotation type.
   *
   * @param mixed $annotation
   *   The annotated object.
   *
   * @return string
   *   The type.
   */
  protected static function getAnnotationType($annotation) {
    switch (get_class($annotation)) {
      case JsonRpcMethod::class:
        return 'JsonRpcMethod';
      case JsonRpcParameterDefinition::class:
        return 'JsonRpcParameterDefinition';
      default:
        return get_class($annotation);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationNormalizer::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
AnnotationNormalizer::$serializer protected property The serializer service.
AnnotationNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
AnnotationNormalizer::DEPTH_KEY constant
AnnotationNormalizer::getAnnotationType protected static function Extract the annotation type.
AnnotationNormalizer::normalize public function
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1