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

Breadcrumb

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

function GetSetMethodNormalizer::isAllowedAttribute

Overrides AbstractNormalizer::isAllowedAttribute

1 call to GetSetMethodNormalizer::isAllowedAttribute()
GetSetMethodNormalizer::extractAttributes in vendor/symfony/serializer/Normalizer/GetSetMethodNormalizer.php
Extracts attributes to normalize from the class of the given object, format and context.

File

vendor/symfony/serializer/Normalizer/GetSetMethodNormalizer.php, line 163

Class

GetSetMethodNormalizer
Converts between objects with getter and setter methods and arrays.

Namespace

Symfony\Component\Serializer\Normalizer

Code

protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []) : bool {
    if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
        return false;
    }
    $class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
    if (!isset(self::$reflectionCache[$class])) {
        self::$reflectionCache[$class] = new \ReflectionClass($class);
    }
    $reflection = self::$reflectionCache[$class];
    if ($context['_read_attributes'] ?? true) {
        foreach ([
            'get',
            'is',
            'has',
        ] as $getterPrefix) {
            $getter = $getterPrefix . $attribute;
            $reflectionMethod = $reflection->hasMethod($getter) ? $reflection->getMethod($getter) : null;
            if ($reflectionMethod && $this->isGetMethod($reflectionMethod)) {
                return true;
            }
        }
        return false;
    }
    $setter = 'set' . $attribute;
    if ($reflection->hasMethod($setter) && $this->isSetMethod($reflection->getMethod($setter))) {
        return true;
    }
    $constructor = $reflection->getConstructor();
    if ($constructor && $constructor->isPublic()) {
        foreach ($constructor->getParameters() as $parameter) {
            if ($parameter->getName() === $attribute) {
                return true;
            }
        }
    }
    return false;
}
RSS feed
Powered by Drupal