Вход Регистрация
Файл: symfony-2.7/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php
Строк: 171
<?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 SymfonyBundleFrameworkBundleValidator;

use 
SymfonyComponentDependencyInjectionContainerInterface;
use 
SymfonyComponentValidatorConstraint;
use 
SymfonyComponentValidatorConstraintValidatorFactoryInterface;
use 
SymfonyComponentValidatorConstraintValidatorInterface;
use 
SymfonyComponentValidatorExceptionUnexpectedTypeException;

/**
 * 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 ContainerInterface $container  The service container
     * @param array              $validators An array of validators
     */
    
public function __construct(ContainerInterface $container, array $validators = array())
    {
        
$this->container $container;
        
$this->validators $validators;
    }

    
/**
     * Returns the validator for the supplied constraint.
     *
     * @param Constraint $constraint A constraint
     *
     * @return ConstraintValidatorInterface A validator for the supplied constraint
     *
     * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
     */
    
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->get($this->validators[$name]);
        }

        if (!
$this->validators[$name] instanceof ConstraintValidatorInterface) {
            throw new 
UnexpectedTypeException($this->validators[$name], 'SymfonyComponentValidatorConstraintValidatorInterface');
        }

        return 
$this->validators[$name];
    }
}
Онлайн: 1
Реклама