class SchemataSchemaNormalizer in Schemata 8
Same name in this branch
- 8 schemata_json_schema/src/Normalizer/jsonapi/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer
- 8 schemata_json_schema/src/Normalizer/json/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\SchemataSchemaNormalizer
- 8 schemata_json_schema/src/Normalizer/hal/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\SchemataSchemaNormalizer
Primary normalizer for SchemaInterface objects.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\JsonApiNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\JsonApiNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
Expanded class hierarchy of SchemataSchemaNormalizer
1 string reference to 'SchemataSchemaNormalizer'
- schemata_json_schema.services.yml in schemata_json_schema/
schemata_json_schema.services.yml - schemata_json_schema/schemata_json_schema.services.yml
1 service uses SchemataSchemaNormalizer
File
- schemata_json_schema/
src/ Normalizer/ jsonapi/ SchemataSchemaNormalizer.php, line 14
Namespace
Drupal\schemata_json_schema\Normalizer\jsonapiView source
class SchemataSchemaNormalizer extends JsonApiNormalizerBase {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = 'Drupal\\schemata\\Schema\\SchemaInterface';
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
/* @var $entity \Drupal\schemata\Schema\SchemaInterface */
$generated_url = SchemaUrl::fromSchema($this->format, $this->describedFormat, $entity)
->toString(TRUE);
// Create the array of normalized fields, starting with the URI.
/** @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository */
$resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
$resource_type = $resource_type_repository
->get($entity
->getEntityTypeId(), $entity
->getBundleId() ?: $entity
->getEntityTypeId());
$normalized = [
'$schema' => 'http://json-schema.org/draft-06/schema#',
'title' => 'JSON:API Schema',
'description' => 'This is a schema for responses in the JSON:API format. For more, see http://jsonapi.org',
'id' => $generated_url
->getGeneratedUrl(),
'type' => 'object',
'required' => [
'data',
],
'properties' => [
'data' => [
'description' => '\\"Resource objects\\" appear in a JSON:API document to represent resources.',
'type' => 'object',
'required' => [
'type',
'id',
],
'properties' => [
'type' => [
'type' => 'string',
'title' => 'type',
'description' => t('Resource type'),
'enum' => [
$resource_type
->getTypeName(),
],
],
'id' => [
'type' => 'string',
'title' => t('Resource ID'),
'format' => 'uuid',
'maxLength' => 128,
],
'attributes' => [
'description' => 'Members of the attributes object (\'attributes\\") represent information about the resource object in which it\'s defined . ',
'type' => 'object',
'additionalProperties' => FALSE,
],
'relationships' => [
'description' => 'Members of the relationships object(\'relationships\\") represent references from the resource object in which it\'s defined to other resource objects . ',
'type' => 'object',
'additionalProperties' => FALSE,
],
'links' => [
'type' => 'object',
'additionalProperties' => [
'description' => 'A link **MUST** be represented as either: a string containing the link\'s URL or a link object . ',
'type' => 'object',
'required' => [
'href',
],
'properties' => [
'href' => [
'description' => 'A string containing the link\'s URL . ',
'type' => 'string',
'format' => 'uri - reference',
],
'meta' => [
'description' => 'Non-standard meta-information that can not be represented as an attribute or relationship.',
'type' => 'object',
'additionalProperties' => TRUE,
],
],
],
],
'meta' => [
'description' => 'Non-standard meta-information that can not be represented as an attribute or relationship.',
'type' => 'object',
'additionalProperties' => TRUE,
],
],
'additionalProperties' => FALSE,
],
'meta' => [
'description' => 'Non-standard meta-information that can not be represented as an attribute or relationship.',
'type' => 'object',
'additionalProperties' => TRUE,
],
'links' => [
'type' => 'object',
'additionalProperties' => [
'description' => 'A link **MUST** be represented as either: a string containing the link\'s URL or a link object . ',
'type' => 'object',
'required' => [
'href',
],
'properties' => [
'href' => [
'description' => 'A string containing the link\'s URL . ',
'type' => 'string',
'format' => 'uri - reference',
],
'meta' => [
'description' => 'Non-standard meta-information that can not be represented as an attribute or relationship.',
'type' => 'object',
'additionalProperties' => TRUE,
],
],
],
],
'jsonapi' => [
'description' => 'An object describing the server\'s implementation',
'type' => 'object',
'properties' => [
'version' => [
'type' => 'string',
],
'meta' => [
'description' => 'Non-standard meta-information that can not be represented as an attribute or relationship.',
'type' => 'object',
'additionalProperties' => TRUE,
],
],
'additionalProperties' => FALSE,
],
],
'additionalProperties' => TRUE,
];
// Stash schema request parameters.
$context['entityTypeId'] = $entity
->getEntityTypeId();
$context['bundleId'] = $entity
->getBundleId();
$context['resourceType'] = $resource_type;
// Retrieve 'properties' and possibly 'required' nested arrays.
$schema_overrides = [
'properties' => [
'data' => $this
->normalizeJsonapiProperties($this
->getProperties($entity, $format, $context), $format, $context),
],
];
return NestedArray::mergeDeep($normalized, $entity
->getMetadata(), $schema_overrides);
}
/**
* Identify properties of the data definition to normalize.
*
* This allow subclasses of the normalizer to build white or blacklisting
* functionality on what will be included in the serialized schema. The JSON
* Schema serializer already has logic to drop any properties that are empty
* values after processing, but this allows cleaner, centralized logic.
*
* @param \Drupal\schemata\Schema\SchemaInterface $entity
* The Schema object whose properties the serializer will present.
* @param string $format
* The serializer format. Defaults to NULL.
* @param array $context
* The current serializer context.
*
* @return \Drupal\Core\TypedData\DataDefinitionInterface[]
* The DataDefinitions to be processed.
*/
protected static function getProperties(SchemaInterface $entity, $format = NULL, array $context = []) {
return $entity
->getProperties();
}
/**
* Normalize an array of data definitions.
*
* This normalization process gets an array of properties and an array of
* properties that are required by name. This is needed by the
* SchemataSchemaNormalizer, otherwise it would have been placed in
* DataDefinitionNormalizer.
*
* @param \Drupal\Core\TypedData\DataDefinitionInterface[] $items
* An array of data definition properties to be normalized.
* @param string $format
* Format identifier of the current serialization process.
* @param array $context
* Operating context of the serializer.
*
* @return array
* Array containing one or two nested arrays.
* - properties: The array of all normalized properties.
* - required: The array of required properties by name.
*/
protected function normalizeJsonapiProperties(array $items, $format, array $context = []) {
$normalized = [];
$resource_type = $context['resourceType'];
assert($resource_type instanceof ResourceType);
assert($this->serializer instanceof NormalizerInterface);
foreach ($items as $name => $property) {
if (!$resource_type
->isFieldEnabled($name)) {
continue;
}
$context['name'] = $resource_type
->getPublicName($name);
$item = $this->serializer
->normalize($property, $format, $context);
if (!empty($item)) {
$normalized = NestedArray::mergeDeep($normalized, $item);
}
}
return $normalized;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
JsonApiNormalizerBase:: |
protected | property | The formats that the Normalizer can handle. | |
JsonApiNormalizerBase:: |
protected | property |
The formats that the Normalizer can handle. Overrides NormalizerBase:: |
|
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function |
Checks if the provided format is supported by this normalizer. Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Denormalizes data back into an object of the given class. | |
NormalizerBase:: |
protected | function | Normalize an array of data definitions. | |
NormalizerBase:: |
protected | function | Determine if the given property is a required element of the schema. | |
NormalizerBase:: |
public | function |
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |
SchemataSchemaNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
|
SchemataSchemaNormalizer:: |
protected static | function | Identify properties of the data definition to normalize. | |
SchemataSchemaNormalizer:: |
public | function | Normalizes an object into a set of arrays/scalars. | |
SchemataSchemaNormalizer:: |
protected | function | Normalize an array of data definitions. |