Commit fd72c6ab authored by alexkappa's avatar alexkappa

Incorporated changes suggested by @Igorow. Separating $this->validators...

Incorporated changes suggested by @Igorow. Separating $this->validators (instances) and $this->serviceNames.
parent fa15389b
...@@ -20,41 +20,69 @@ use Symfony\Component\Validator\ConstraintValidator; ...@@ -20,41 +20,69 @@ use Symfony\Component\Validator\ConstraintValidator;
* Uses a service container to create constraint validators with dependencies. * Uses a service container to create constraint validators with dependencies.
* *
* @author Kris Wallsmith <kris@symfony.com> * @author Kris Wallsmith <kris@symfony.com>
* @author Alex Kalyvitis <alex.kalyvitis@gmail.com>
*/ */
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{ {
/**
* @var Silex\Application
*/
protected $container; protected $container;
/**
* @var array
*/
protected $serviceNames;
/**
* @var array
*/
protected $validators; protected $validators;
/** /**
* Constructor. * Constructor
* *
* @param Silex\Application $container A DI container * @param Silex\Application $container DI container
* @param array $validators An array of validators * @param array $serviceNames Validator service names
*/ */
public function __construct(Application $container, array $validators = array()) public function __construct(Application $container, array $serviceNames = array())
{ {
$this->container = $container; $this->container = $container;
$this->validators = $validators; $this->serviceNames = $serviceNames;
$this->validators = array();
} }
/** /**
* Returns the validator for the supplied constraint. * Returns the validator for the supplied constraint.
* *
* @param Constraint $constraint A constraint * @param Constraint $constraint A constraint
*
* @return ConstraintValidator A validator for the supplied constraint * @return ConstraintValidator A validator for the supplied constraint
*/ */
public function getInstance(Constraint $constraint) public function getInstance(Constraint $constraint)
{ {
$name = $constraint->validatedBy(); $name = $constraint->validatedBy();
if (!isset($this->validators[$name])) { if (isset($this->validators[$name])) {
$validator = new $name(); return $this->validators[$name];
} elseif (is_string($this->validators[$name])) { }
$validator = $this->container[$this->validators[$name]];
$this->validators[$name] = $this->createValidator($name);
return $this->validators[$name];
}
/**
* Returns the validator instance
*
* @param string $name
* @return Symfony\Component\Validator\ConstraintValidator
*/
private function createValidator($name)
{
if (isset($this->serviceNames[$name])) {
return $this->container[$this->serviceNames[$name]];
} }
return $validator; return new $name();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment