You are here

public function SchemataSchemaNormalizer::normalize in Schemata 8

Same name in this branch
  1. 8 schemata_json_schema/src/Normalizer/jsonapi/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer::normalize()
  2. 8 schemata_json_schema/src/Normalizer/json/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\SchemataSchemaNormalizer::normalize()
  3. 8 schemata_json_schema/src/Normalizer/hal/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\SchemataSchemaNormalizer::normalize()

File

schemata_json_schema/src/Normalizer/jsonapi/SchemataSchemaNormalizer.php, line 26

Class

SchemataSchemaNormalizer
Primary normalizer for SchemaInterface objects.

Namespace

Drupal\schemata_json_schema\Normalizer\jsonapi

Code

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);
}