Файл: symfony-2.7/src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php
Строк: 142
<?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 SymfonyComponentFormExtensionValidator;
use SymfonyComponentFormExceptionUnexpectedTypeException;
use SymfonyComponentFormExtensionValidatorConstraintsForm;
use SymfonyComponentFormAbstractExtension;
use SymfonyComponentValidatorConstraintsValid;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorValidatorValidatorInterface;
use SymfonyComponentValidatorValidatorInterface as LegacyValidatorInterface;
/**
* Extension supporting the Symfony Validator component in forms.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValidatorExtension extends AbstractExtension
{
private $validator;
/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
*
* @throws UnexpectedTypeException If $validator is invalid
*/
public function __construct($validator)
{
// 2.5 API
if ($validator instanceof ValidatorInterface) {
$metadata = $validator->getMetadataFor('SymfonyComponentFormForm');
// 2.4 API
} elseif ($validator instanceof LegacyValidatorInterface) {
$metadata = $validator->getMetadataFactory()->getMetadataFor('SymfonyComponentFormForm');
} else {
throw new UnexpectedTypeException($validator, 'SymfonyComponentValidatorValidatorValidatorInterface or SymfonyComponentValidatorValidatorInterface');
}
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
/** @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());
$this->validator = $validator;
}
public function loadTypeGuesser()
{
// 2.4 API
if ($this->validator instanceof LegacyValidatorInterface) {
return new ValidatorTypeGuesser($this->validator->getMetadataFactory());
}
// 2.5 API - ValidatorInterface extends MetadataFactoryInterface
return new ValidatorTypeGuesser($this->validator);
}
protected function loadTypeExtensions()
{
return array(
new TypeFormTypeValidatorExtension($this->validator),
new TypeRepeatedTypeValidatorExtension(),
new TypeSubmitTypeValidatorExtension(),
);
}
}