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

Breadcrumb

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

function AttributeLoader::loadClassMetadata

Same name in this branch
  1. 11.1.x vendor/symfony/validator/Mapping/Loader/AttributeLoader.php \Symfony\Component\Validator\Mapping\Loader\AttributeLoader::loadClassMetadata()

Overrides LoaderInterface::loadClassMetadata

File

vendor/symfony/serializer/Mapping/Loader/AttributeLoader.php, line 50

Class

AttributeLoader
Loader for PHP attributes.

Namespace

Symfony\Component\Serializer\Mapping\Loader

Code

public function loadClassMetadata(ClassMetadataInterface $classMetadata) : bool {
    $reflectionClass = $classMetadata->getReflectionClass();
    $className = $reflectionClass->name;
    $loaded = false;
    $classGroups = [];
    $classContextAttribute = null;
    $attributesMetadata = $classMetadata->getAttributesMetadata();
    foreach ($this->loadAttributes($reflectionClass) as $attribute) {
        match (true) {    $attribute instanceof DiscriminatorMap => $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping($attribute->getTypeProperty(), $attribute->getMapping())),
            $attribute instanceof Groups => $classGroups = $attribute->getGroups(),
            $attribute instanceof Context => $classContextAttribute = $attribute,
            default => null,
        
        };
    }
    foreach ($reflectionClass->getProperties() as $property) {
        if (!isset($attributesMetadata[$property->name])) {
            $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
            $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
        }
        $attributeMetadata = $attributesMetadata[$property->name];
        if ($property->getDeclaringClass()->name === $className) {
            if ($classContextAttribute) {
                $this->setAttributeContextsForGroups($classContextAttribute, $attributeMetadata);
            }
            foreach ($classGroups as $group) {
                $attributeMetadata->addGroup($group);
            }
            foreach ($this->loadAttributes($property) as $attribute) {
                $loaded = true;
                if ($attribute instanceof Groups) {
                    foreach ($attribute->getGroups() as $group) {
                        $attributeMetadata->addGroup($group);
                    }
                    continue;
                }
                match (true) {    $attribute instanceof MaxDepth => $attributeMetadata->setMaxDepth($attribute->getMaxDepth()),
                    $attribute instanceof SerializedName => $attributeMetadata->setSerializedName($attribute->getSerializedName()),
                    $attribute instanceof SerializedPath => $attributeMetadata->setSerializedPath($attribute->getSerializedPath()),
                    $attribute instanceof Ignore => $attributeMetadata->setIgnore(true),
                    $attribute instanceof Context => $this->setAttributeContextsForGroups($attribute, $attributeMetadata),
                    default => null,
                
                };
            }
        }
    }
    foreach ($reflectionClass->getMethods() as $method) {
        if ($method->getDeclaringClass()->name !== $className) {
            continue;
        }
        if (0 === stripos($method->name, 'get') && $method->getNumberOfRequiredParameters()) {
            continue;
            
            /*  matches the BC behavior in `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes` */
        }
        $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
        if ($accessorOrMutator && !ctype_lower($matches[2][0])) {
            $attributeName = lcfirst($matches[2]);
            if (isset($attributesMetadata[$attributeName])) {
                $attributeMetadata = $attributesMetadata[$attributeName];
            }
            else {
                $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
                $classMetadata->addAttributeMetadata($attributeMetadata);
            }
        }
        foreach ($this->loadAttributes($method) as $attribute) {
            if ($attribute instanceof Groups) {
                if (!$accessorOrMutator) {
                    throw new MappingException(\sprintf('Groups on "%s::%s()" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                }
                foreach ($attribute->getGroups() as $group) {
                    $attributeMetadata->addGroup($group);
                }
            }
            elseif ($attribute instanceof MaxDepth) {
                if (!$accessorOrMutator) {
                    throw new MappingException(\sprintf('MaxDepth on "%s::%s()" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                }
                $attributeMetadata->setMaxDepth($attribute->getMaxDepth());
            }
            elseif ($attribute instanceof SerializedName) {
                if (!$accessorOrMutator) {
                    throw new MappingException(\sprintf('SerializedName on "%s::%s()" cannot be added. SerializedName can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                }
                $attributeMetadata->setSerializedName($attribute->getSerializedName());
            }
            elseif ($attribute instanceof SerializedPath) {
                if (!$accessorOrMutator) {
                    throw new MappingException(\sprintf('SerializedPath on "%s::%s()" cannot be added. SerializedPath can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                }
                $attributeMetadata->setSerializedPath($attribute->getSerializedPath());
            }
            elseif ($attribute instanceof Ignore) {
                if ($accessorOrMutator) {
                    $attributeMetadata->setIgnore(true);
                }
            }
            elseif ($attribute instanceof Context) {
                if (!$accessorOrMutator) {
                    throw new MappingException(\sprintf('Context on "%s::%s()" cannot be added. Context can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                }
                $this->setAttributeContextsForGroups($attribute, $attributeMetadata);
            }
            $loaded = true;
        }
    }
    return $loaded;
}

API Navigation

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