- md5
- d4b95d770ec26da436162732aab3a853
- sha1
- 6f37716341f1ad62ab5b725384e9e8c5cabb61cb
- sha256
- bba8eb03e93e37291f31524b988924a44c7402b6b67c387de5b07b605e8bcac9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\ValidatorException;
/**
* Uses a service container to create constraint validators.
*
* @author Kris Wallsmith <[email protected]>
*/
class ContainerConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
private ContainerInterface $container;
private array $validators;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->validators = [];
}
/**
* @throws ValidatorException When the validator class does not exist
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint): ConstraintValidatorInterface
{
$name = $constraint->validatedBy();
if (!isset($this->validators[$name])) {
if ($this->container->has($name)) {
$this->validators[$name] = $this->container->get($name);
} else {
if (!class_exists($name)) {
throw new ValidatorException(\sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_debug_type($constraint)));
}
$this->validators[$name] = new $name();
}
}
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
}
return $this->validators[$name];
}
}
The same content is in 70 releases, Joomla 5.4.0-rc1 to 6.2.1-rc1. See them all, or every version of this file.