class AnnotationNormalizer in JSON-RPC 8
Same name and namespace in other branches
- 2.x modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php \Drupal\jsonrpc_discovery\Normalizer\AnnotationNormalizer
The normalizer class for annotated objects.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\jsonrpc_discovery\Normalizer\AnnotationNormalizer
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
File
- modules/
jsonrpc_discovery/ src/ Normalizer/ AnnotationNormalizer.php, line 15
Namespace
Drupal\jsonrpc_discovery\NormalizerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnnotationNormalizer:: |
protected | property |
List of formats which supports (de-)normalization. Overrides NormalizerBase:: |
|
AnnotationNormalizer:: |
protected | property | The serializer service. | |
AnnotationNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
|
AnnotationNormalizer:: |
constant | |||
AnnotationNormalizer:: |
protected static | function | Extract the annotation type. | |
AnnotationNormalizer:: |
public | function | ||
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | 2 |
NormalizerBase:: |
public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |