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

Breadcrumb

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

function AbstractLoader::newConstraint

Creates a new constraint instance for the given constraint name.

Parameters

string $name The constraint name. Either a constraint relative: to the default constraint namespace, or a fully qualified class name. Alternatively, the constraint may be preceded by a namespace alias and a colon. The namespace alias must have been defined using {@link addNamespaceAlias()}.

mixed $options The constraint options:

Throws

MappingException If the namespace prefix is undefined

2 calls to AbstractLoader::newConstraint()
XmlFileLoader::parseConstraints in vendor/symfony/validator/Mapping/Loader/XmlFileLoader.php
Parses a collection of "constraint" XML nodes.
YamlFileLoader::parseNodes in vendor/symfony/validator/Mapping/Loader/YamlFileLoader.php
Parses a collection of YAML nodes.

File

vendor/symfony/validator/Mapping/Loader/AbstractLoader.php, line 71

Class

AbstractLoader
Base loader for validation metadata.

Namespace

Symfony\Component\Validator\Mapping\Loader

Code

protected function newConstraint(string $name, mixed $options = null) : Constraint {
    if (str_contains($name, '\\') && class_exists($name)) {
        $className = $name;
    }
    elseif (str_contains($name, ':')) {
        [
            $prefix,
            $className,
        ] = explode(':', $name, 2);
        if (!isset($this->namespaces[$prefix])) {
            throw new MappingException(\sprintf('Undefined namespace prefix "%s".', $prefix));
        }
        $className = $this->namespaces[$prefix] . $className;
    }
    else {
        $className = self::DEFAULT_NAMESPACE . $name;
    }
    if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
        if (null === $options) {
            return new $className();
        }
        if (!\is_array($options)) {
            return new $className($options);
        }
        if (1 === \count($options) && isset($options['value'])) {
            return new $className($options['value']);
        }
        return new $className(...$options);
    }
    return new $className($options);
}

API Navigation

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