Commit eaf0af2a authored by alexkappa's avatar alexkappa

Ported ConstraintValidatorFactory.php to Silex/Pimple

parent a23c221e
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Uses a service container to create constraint validators.
*
* A constraint validator should be tagged as "validator.constraint_validator"
* in the service container and include an "alias" attribute:
*
* <service id="some_doctrine_validator">
* <argument type="service" id="doctrine.orm.some_entity_manager" />
* <tag name="validator.constraint_validator" alias="some_alias" />
* </service>
*
* A constraint may then return this alias in its validatedBy() method:
*
* public function validatedBy()
* {
* return 'some_alias';
* }
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
protected $container;
protected $validators;
/**
* Constructor.
*
* @param array $validators An array of validators
*/
public function __construct($container, array $validators = array())
{
$this->container = $container;
$this->validators = $validators;
}
/**
* Returns the validator for the supplied constraint.
*
* @param Constraint $constraint A constraint
*
* @return ConstraintValidator A validator for the supplied constraint
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();
if (!isset($this->validators[$name])) {
$this->validators[$name] = new $name();
} elseif (is_string($this->validators[$name])) {
$this->validators[$name] = $this->container[$this->validators[$name]];
}
return $this->validators[$name];
}
}
\ No newline at end of file
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