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\LoaderCode
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);
}