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

Breadcrumb

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

function PropertyInfoLoader::loadClassMetadata

Overrides LoaderInterface::loadClassMetadata

File

vendor/symfony/validator/Mapping/Loader/PropertyInfoLoader.php, line 52

Class

PropertyInfoLoader
Guesses and loads the appropriate constraints using PropertyInfo.

Namespace

Symfony\Component\Validator\Mapping\Loader

Code

public function loadClassMetadata(ClassMetadata $metadata) : bool {
    $className = $metadata->getClassName();
    if (!($properties = $this->listExtractor
        ->getProperties($className))) {
        return false;
    }
    $loaded = false;
    $enabledForClass = $this->isAutoMappingEnabledForClass($metadata, $this->classValidatorRegexp);
    foreach ($properties as $property) {
        if (false === $this->accessExtractor
            ->isWritable($className, $property)) {
            continue;
        }
        if (!property_exists($className, $property)) {
            continue;
        }
        $types = $this->getPropertyTypes($className, $property);
        if (null === $types) {
            continue;
        }
        $enabledForProperty = $enabledForClass;
        $hasTypeConstraint = false;
        $hasNotNullConstraint = false;
        $hasNotBlankConstraint = false;
        $allConstraint = null;
        foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) {
            // Enabling or disabling auto-mapping explicitly always takes precedence
            if (AutoMappingStrategy::DISABLED === $propertyMetadata->getAutoMappingStrategy()) {
                continue 2;
            }
            if (AutoMappingStrategy::ENABLED === $propertyMetadata->getAutoMappingStrategy()) {
                $enabledForProperty = true;
            }
            foreach ($propertyMetadata->getConstraints() as $constraint) {
                if ($constraint instanceof Type) {
                    $hasTypeConstraint = true;
                }
                elseif ($constraint instanceof NotNull) {
                    $hasNotNullConstraint = true;
                }
                elseif ($constraint instanceof NotBlank) {
                    $hasNotBlankConstraint = true;
                }
                elseif ($constraint instanceof All) {
                    $allConstraint = $constraint;
                }
            }
        }
        if (!$enabledForProperty) {
            continue;
        }
        $loaded = true;
        // BC layer for PropertyTypeExtractorInterface::getTypes().
        // Can be removed as soon as PropertyTypeExtractorInterface::getTypes() is removed (8.0).
        if (\is_array($types)) {
            $builtinTypes = [];
            $nullable = false;
            $scalar = true;
            foreach ($types as $type) {
                $builtinTypes[] = $type->getBuiltinType();
                if ($scalar && !\in_array($type->getBuiltinType(), [
                    'int',
                    'float',
                    'string',
                    'bool',
                ], true)) {
                    $scalar = false;
                }
                if (!$nullable && $type->isNullable()) {
                    $nullable = true;
                }
            }
            if (!$hasTypeConstraint) {
                if (1 === \count($builtinTypes)) {
                    if ($types[0]->isCollection() && \count($collectionValueType = $types[0]->getCollectionValueTypes()) > 0) {
                        [
                            $collectionValueType,
                        ] = $collectionValueType;
                        $this->handleAllConstraintLegacy($property, $allConstraint, $collectionValueType, $metadata);
                    }
                    $metadata->addPropertyConstraint($property, $this->getTypeConstraintLegacy($builtinTypes[0], $types[0]));
                }
                elseif ($scalar) {
                    $metadata->addPropertyConstraint($property, new Type([
                        'type' => 'scalar',
                    ]));
                }
            }
        }
        else {
            if ($hasTypeConstraint) {
                continue;
            }
            $type = $types;
            // BC layer for type-info < 7.2
            if (!class_exists(NullableType::class)) {
                $nullable = false;
                if ($type instanceof UnionType && $type->isNullable()) {
                    $nullable = true;
                    $type = $type->asNonNullable();
                }
            }
            else {
                $nullable = $type->isNullable();
                if ($type instanceof NullableType) {
                    $type = $type->getWrappedType();
                }
            }
            if ($type instanceof NullableType) {
                $type = $type->getWrappedType();
            }
            if ($type instanceof CollectionType) {
                $this->handleAllConstraint($property, $allConstraint, $type->getCollectionValueType(), $metadata);
            }
            if (null !== ($typeConstraint = $this->getTypeConstraint($type))) {
                $metadata->addPropertyConstraint($property, $typeConstraint);
            }
        }
        if (!$nullable && !$hasNotBlankConstraint && !$hasNotNullConstraint) {
            $metadata->addPropertyConstraint($property, new NotNull());
        }
    }
    return $loaded;
}

API Navigation

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