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

Breadcrumb

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

class AttributeLoader

Same name in this branch
  1. 11.1.x vendor/symfony/serializer/Mapping/Loader/AttributeLoader.php \Symfony\Component\Serializer\Mapping\Loader\AttributeLoader

Loads validation metadata using PHP attributes.

@author Bernhard Schussek <bschussek@gmail.com> @author Alexander M. Turek <me@derrabus.de> @author Alexandre Daubois <alex.daubois@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\Mapping\Loader\AttributeLoader implements \Symfony\Component\Validator\Mapping\Loader\LoaderInterface

Expanded class hierarchy of AttributeLoader

1 file declares its use of AttributeLoader
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/symfony/validator/Mapping/Loader/AttributeLoader.php, line 28

Namespace

Symfony\Component\Validator\Mapping\Loader
View source
class AttributeLoader implements LoaderInterface {
    public function loadClassMetadata(ClassMetadata $metadata) : bool {
        $reflClass = $metadata->getReflectionClass();
        $className = $reflClass->name;
        $success = false;
        foreach ($this->getAttributes($reflClass) as $constraint) {
            if ($constraint instanceof GroupSequence) {
                $metadata->setGroupSequence($constraint->groups);
            }
            elseif ($constraint instanceof GroupSequenceProvider) {
                $metadata->setGroupProvider($constraint->provider);
                $metadata->setGroupSequenceProvider(true);
            }
            elseif ($constraint instanceof Constraint) {
                $metadata->addConstraint($constraint);
            }
            $success = true;
        }
        foreach ($reflClass->getProperties() as $property) {
            if ($property->getDeclaringClass()->name === $className) {
                foreach ($this->getAttributes($property) as $constraint) {
                    if ($constraint instanceof Constraint) {
                        $metadata->addPropertyConstraint($property->name, $constraint);
                    }
                    $success = true;
                }
            }
        }
        foreach ($reflClass->getMethods() as $method) {
            if ($method->getDeclaringClass()->name === $className) {
                foreach ($this->getAttributes($method) as $constraint) {
                    if ($constraint instanceof Callback) {
                        $constraint->callback = $method->getName();
                        $metadata->addConstraint($constraint);
                    }
                    elseif ($constraint instanceof Constraint) {
                        if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
                            $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
                        }
                        else {
                            throw new MappingException(\sprintf('The constraint on "%s::%s()" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
                        }
                    }
                    $success = true;
                }
            }
        }
        return $success;
    }
    private function getAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionProperty $reflection) : iterable {
        foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) {
            (yield $attribute->newInstance());
        }
        foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) {
            (yield $attribute->newInstance());
        }
        foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
            (yield $attribute->newInstance());
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AttributeLoader::getAttributes private function
AttributeLoader::loadClassMetadata public function Loads validation metadata into a {@link ClassMetadata} instance. Overrides LoaderInterface::loadClassMetadata

API Navigation

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