Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractNormalizer.php

function AbstractNormalizer::getAllowedAttributes

Gets attributes to normalize using groups.

Parameters

bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}:

Return value

string[]|AttributeMetadataInterface[]|bool

Throws

LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided

4 calls to AbstractNormalizer::getAllowedAttributes()
AbstractObjectNormalizer::denormalize in vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php
Denormalizes data back into an object of the given class.
AbstractObjectNormalizer::getAttributes in vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php
Gets and caches attributes for the given object, format and context.
ObjectNormalizer::getAllowedAttributes in vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
Gets attributes to normalize using groups.
ObjectNormalizer::getAllowedAttributes in vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
Gets attributes to normalize using groups.
1 method overrides AbstractNormalizer::getAllowedAttributes()
ObjectNormalizer::getAllowedAttributes in vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
Gets attributes to normalize using groups.

File

vendor/symfony/serializer/Normalizer/AbstractNormalizer.php, line 214

Class

AbstractNormalizer
Normalizer implementation.

Namespace

Symfony\Component\Serializer\Normalizer

Code

protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false) : array|bool {
    $allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES];
    if (!$this->classMetadataFactory) {
        if (!$allowExtraAttributes) {
            throw new LogicException(\sprintf('A class metadata factory must be provided in the constructor when setting "%s" to false.', self::ALLOW_EXTRA_ATTRIBUTES));
        }
        return false;
    }
    $groups = $this->getGroups($context);
    $allowedAttributes = [];
    $ignoreUsed = false;
    foreach ($this->classMetadataFactory
        ->getMetadataFor($classOrObject)
        ->getAttributesMetadata() as $attributeMetadata) {
        if ($ignore = $attributeMetadata->isIgnored()) {
            $ignoreUsed = true;
        }
        // If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
        if (!$ignore && ([] === $groups || \in_array('*', $groups, true) || array_intersect($attributeMetadata->getGroups(), $groups)) && $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)) {
            $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
        }
    }
    if (!$ignoreUsed && [] === $groups && $allowExtraAttributes) {
        // Backward Compatibility with the code using this method written before the introduction of @Ignore
        return false;
    }
    return $allowedAttributes;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal