You are here

class SchemataSchemaNormalizer in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/Normalizer/SchemataSchemaNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemataSchemaNormalizer
  2. 8.2 src/Normalizer/SchemataSchemaNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemataSchemaNormalizer

Applies JSONAPI Extras attribute overrides to entity schemas.

Hierarchy

Expanded class hierarchy of SchemataSchemaNormalizer

File

src/Normalizer/SchemataSchemaNormalizer.php, line 11

Namespace

Drupal\jsonapi_extras\Normalizer
View source
class SchemataSchemaNormalizer extends SchemataJsonSchemaSchemataSchemaNormalizer {

  /**
   * The JSON API resource type repository.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository
   */
  protected $resourceTypeRepository;

  /**
   * Constructs a SchemataSchemaNormalizer object.
   *
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository
   *   A resource repository.
   */
  public function __construct(ResourceTypeRepository $resource_type_repository) {
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {
    $normalized = parent::normalize($entity, $format, $context);

    // Load the resource type for this entity type and bundle.
    $resource_type = $this->resourceTypeRepository
      ->get($entity
      ->getEntityTypeId(), $entity
      ->getBundleId());
    if (!$resource_type) {
      return $normalized;
    }

    // Alter the attributes according to the resource config.
    foreach ([
      'attributes',
      'relationships',
    ] as $property_type) {
      foreach ($normalized['properties'][$property_type]['properties'] as $fieldname => $schema) {
        $properties =& $normalized['properties'][$property_type]['properties'];
        unset($properties[$fieldname]);
        if (!$resource_type
          ->isFieldEnabled($fieldname)) {

          // If the field is disabled, do nothing after removal.
          continue;
        }
        else {

          // Otherwise, substitute the public name.
          $public_name = $resource_type
            ->getPublicName($fieldname);
          $properties[$public_name] = $schema;
        }
      }
    }
    return $normalized;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
JsonApiNormalizerBase::$describedFormat protected property The formats that the Normalizer can handle.
JsonApiNormalizerBase::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format
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::$resourceTypeRepository protected property The JSON API resource type repository.
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. Overrides SchemataSchemaNormalizer::normalize
SchemataSchemaNormalizer::normalizeJsonapiProperties protected function Normalize an array of data definitions.
SchemataSchemaNormalizer::__construct public function Constructs a SchemataSchemaNormalizer object.