You are here

class SchemataSchemaNormalizer 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
  2. 8 schemata_json_schema/src/Normalizer/json/SchemataSchemaNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\SchemataSchemaNormalizer
  3. 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\json\JsonNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of SchemataSchemaNormalizer

1 file declares its use of SchemataSchemaNormalizer
SchemataSchemaNormalizer.php in schemata_json_schema/src/Normalizer/hal/SchemataSchemaNormalizer.php
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
serializer.normalizer.schema.schema_json.json in schemata_json_schema/schemata_json_schema.services.yml
Drupal\schemata_json_schema\Normalizer\json\SchemataSchemaNormalizer

File

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

Namespace

Drupal\schemata_json_schema\Normalizer\json
View source
class SchemataSchemaNormalizer extends JsonNormalizerBase {

  /**
   * 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.
    $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;
  }

  /**
   * 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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
JsonNormalizerBase::$describedFormat protected property The formats that the Normalizer can handle. 7
JsonNormalizerBase::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format 7
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::denormalize public function Denormalizes data back into an object of the given class.
NormalizerBase::normalizeProperties protected function Normalize an array of data definitions.
NormalizerBase::requiredProperty protected function Determine if the given property is a required element of the schema.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
SchemataSchemaNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
SchemataSchemaNormalizer::getProperties protected static function Identify properties of the data definition to normalize.
SchemataSchemaNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. 1