function JsonApiDocumentTopLevelNormalizer::getSchemasForDataCollection

Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer::getSchemasForDataCollection()

Retrieve an array of schemas for the resource types in a data object.

Parameters

\Drupal\jsonapi\JsonApiResource\Data $data: JSON:API data value objects.

array $context: Normalization context.

Return value

array Schemas for all types represented in the collection.

1 call to JsonApiDocumentTopLevelNormalizer::getSchemasForDataCollection()
JsonApiDocumentTopLevelNormalizer::getNormalizationSchema in core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php
Retrieve JSON Schema for the normalization.

File

core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php, line 451

Class

JsonApiDocumentTopLevelNormalizer
Normalizes the top-level document according to the JSON:API specification.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getSchemasForDataCollection(Data $data, array $context) : array {
  $schemas = [];
  if ($data->count() === 0) {
    return [
      // We lack sufficient information about if the data would be a
      // collection or a single resource, so allow either.
[
        'type' => [
          'array',
          'null',
        ],
      ],
    ];
  }
  $members = $data->toArray();
  assert($this->serializer instanceof JsonSchemaProviderSerializerInterface);
  // Per the spec, data must either be comprised of a single instance or
  // collection of resource objects OR resource identifiers, but not both.
  foreach ($members as $member) {
    $resourceType = $member->getResourceType();
    if (array_key_exists($resourceType->getTypeName(), $schemas)) {
      continue;
    }
    $schemas[$resourceType->getTypeName()] = $member instanceof ResourceIdentifier ? [
      'allOf' => [
        [
          '$ref' => JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#/definitions/resourceIdentifier',
        ],
      ],
      'properties' => [
        'type' => [
          'const' => $resourceType->getTypeName(),
        ],
      ],
    ] : $this->serializer
      ->getJsonSchema($member, $context);
  }
  return array_values($schemas);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.