trait SchematicNormalizerTrait
Trait for normalizers which can also provide JSON Schema.
To implement this trait, convert the existing normalizer's ::normalize() method to ::doNormalize().
Due to trait inheritance rules, this trait cannot be used with normalizers which call parent::normalize() during normalization (will result in infinite recursion). Instead, use SchematicNormalizerHelperTrait and conditionally call ::getNormalizationSchema() in ::normalize(). See DateTimeIso8601Normalizer::normalize() for an example.
Hierarchy
- trait \Drupal\serialization\Normalizer\SchematicNormalizerTrait uses \Drupal\serialization\Normalizer\SchematicNormalizerHelperTrait, \Drupal\serialization\Normalizer\SchematicNormalizerFallbackTrait
5 files declare their use of SchematicNormalizerTrait
- FieldItemNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ FieldItemNormalizer.php - FieldNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ FieldNormalizer.php - JsonApiDocumentTopLevelNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ JsonApiDocumentTopLevelNormalizer.php - RelationshipNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ RelationshipNormalizer.php - ResourceObjectNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ ResourceObjectNormalizer.php
File
-
core/
modules/ serialization/ src/ Normalizer/ SchematicNormalizerTrait.php, line 19
Namespace
Drupal\serialization\NormalizerView source
trait SchematicNormalizerTrait {
use SchematicNormalizerHelperTrait;
use SchematicNormalizerFallbackTrait;
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
if ($format === 'json_schema') {
return $this->getNormalizationSchema($object, $context);
}
return $this->doNormalize($object, $format, $context);
}
/**
* Normalizes an object into a set of arrays/scalars.
*
* @param mixed $object
* Object to normalize.
* @param string|null $format
* Format the normalization result will be encoded as.
* @param array $context
* Context options for the normalizer.
*
* @return array|string|int|float|bool|\ArrayObject|null
* The normalization. An \ArrayObject is used to make sure an empty object
* is encoded as an object not an array.
*/
protected abstract function doNormalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null;
/**
* {@inheritdoc}
*/
protected function getNormalizationSchema(mixed $object, array $context = []) : array {
return $this->getJsonSchemaForMethod($this, 'doNormalize', [
'$comment' => static::generateNoSchemaAvailableMessage($object),
]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
JsonSchemaReflectionTrait::getJsonSchemaForMethod | protected | function | Get a JSON Schema based on method reflection. | ||
SchematicNormalizerFallbackTrait::generateNoSchemaAvailableMessage | public static | function | |||
SchematicNormalizerHelperTrait::checkFormat | protected | function | |||
SchematicNormalizerTrait::doNormalize | abstract protected | function | Normalizes an object into a set of arrays/scalars. | 10 | |
SchematicNormalizerTrait::getNormalizationSchema | protected | function | Retrieve JSON Schema for the normalization. | Overrides SchematicNormalizerHelperTrait::getNormalizationSchema | 9 |
SchematicNormalizerTrait::normalize | public | function | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.