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()
1 call to SchemataSchemaNormalizer::normalize()
SchemataSchemaNormalizer::normalize in schemata_json_schema/src/Normalizer/hal/SchemataSchemaNormalizer.php
Normalizes an object into a set of arrays/scalars.
1 method overrides SchemataSchemaNormalizer::normalize()
SchemataSchemaNormalizer::normalize in schemata_json_schema/src/Normalizer/hal/SchemataSchemaNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

schemata_json_schema/src/Normalizer/json/SchemataSchemaNormalizer.php, line 24

Class

SchemataSchemaNormalizer
Primary normalizer for SchemaInterface objects.

Namespace

Drupal\schemata_json_schema\Normalizer\json

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.
  $normalized = [
    '$schema' => 'http://json-schema.org/draft-04/schema#',
    'id' => $generated_url
      ->getGeneratedUrl(),
    'type' => 'object',
  ];
  $normalized = array_merge($normalized, $entity
    ->getMetadata());

  // Stash schema request parameters.
  $context['entityTypeId'] = $entity
    ->getEntityTypeId();
  $context['bundleId'] = $entity
    ->getBundleId();

  // Retrieve 'properties' and possibly 'required' nested arrays.
  $properties = $this
    ->normalizeProperties($this
    ->getProperties($entity, $format, $context), $format, $context);
  $normalized = NestedArray::mergeDeep($normalized, $properties);
  return $normalized;
}