[Validator] removed DependencyInjection integration
This commit is contained in:
parent
ef4f61bb9f
commit
5d4c80f27b
@ -1,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Validator\Extension;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
|
||||
/**
|
||||
* Creates ConstraintValidator instances by querying a dependency injection
|
||||
* container
|
||||
*
|
||||
* The constraint validators are expected to be services. The services should
|
||||
* have the fully qualified names of the validators as IDs. The backslashes
|
||||
* in the names should be replaced by dots.
|
||||
*
|
||||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
||||
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
||||
*/
|
||||
class DependencyInjectionValidatorFactory implements ConstraintValidatorFactoryInterface
|
||||
{
|
||||
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a contraint validator from the container service, setting it if it
|
||||
* doesn't exist yet
|
||||
*
|
||||
* Throws an exception if validator service is not instance of
|
||||
* ConstraintValidatorInterface.
|
||||
*
|
||||
* @param Constraint $constraint
|
||||
* @return ConstraintValidatorInterface
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function getInstance(Constraint $constraint)
|
||||
{
|
||||
$className = $constraint->validatedBy();
|
||||
$id = $this->getServiceIdFromClass($className);
|
||||
|
||||
if (!$this->container->has($id)) {
|
||||
$this->container->set($id, new $className());
|
||||
}
|
||||
|
||||
$validator = $this->container->get($id);
|
||||
|
||||
if (!$validator instanceof ConstraintValidatorInterface) {
|
||||
throw new \LogicException('Service "' . $id . '" is not instance of ConstraintValidatorInterface');
|
||||
}
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matching service ID for the given validator class name
|
||||
*
|
||||
* @param string $className
|
||||
* @return string
|
||||
*/
|
||||
protected function getServiceIdFromClass($className)
|
||||
{
|
||||
return str_replace('\\', '.', $className);
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\Validator;
|
||||
|
||||
require_once __DIR__.'/Fixtures/InvalidConstraint.php';
|
||||
require_once __DIR__.'/Fixtures/InvalidConstraintValidator.php';
|
||||
|
||||
use Symfony\Component\Validator\Extension\DependencyInjectionValidatorFactory;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\Constraints\ValidValidator;
|
||||
use Symfony\Tests\Component\Validator\Fixtures\InvalidConstraint;
|
||||
|
||||
class DependencyInjectionValidatorFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $container;
|
||||
protected $factory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->container = new Container();
|
||||
$this->factory = new DependencyInjectionValidatorFactory($this->container);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
unset ($this->factory);
|
||||
unset ($this->container);
|
||||
}
|
||||
|
||||
public function testGetInstanceRetunsCorrectValidatorInstance()
|
||||
{
|
||||
$constraint = new Valid();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
$this->assertTrue($validator instanceof ValidValidator);
|
||||
}
|
||||
|
||||
public function testGetInstanceAddsValidatorServiceToContainer()
|
||||
{
|
||||
$constraint = new Valid();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
$this->assertServiceExists('Symfony.Component.Validator.Constraints.ValidValidator');
|
||||
}
|
||||
|
||||
public function assertServiceExists($id)
|
||||
{
|
||||
$this->assertTrue($this->container->has($id), 'Service ' . $id . ' doesn\'t exist on container');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException LogicException
|
||||
*/
|
||||
public function testGetInstanceThrowsLogicExceptionIfValidatorNotInstanceOfValidatorInterface()
|
||||
{
|
||||
$constraint = new InvalidConstraint();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\Validator\Extension;
|
||||
|
||||
require_once __DIR__.'/../Fixtures/InvalidConstraint.php';
|
||||
require_once __DIR__.'/../Fixtures/InvalidConstraintValidator.php';
|
||||
|
||||
use Symfony\Component\Validator\Extension\DependencyInjectionValidatorFactory;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\Constraints\ValidValidator;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Tests\Component\Validator\Fixtures\InvalidConstraint;
|
||||
|
||||
class DependencyInjectionValidatorFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $container;
|
||||
protected $factory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->container = new Container();
|
||||
$this->factory = new DependencyInjectionValidatorFactory($this->container);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
unset($this->factory);
|
||||
unset($this->container);
|
||||
}
|
||||
|
||||
public function testGetInstanceRetunsCorrectValidatorInstance()
|
||||
{
|
||||
$constraint = new Valid();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
$this->assertTrue($validator instanceof ValidValidator);
|
||||
}
|
||||
|
||||
public function testGetInstanceAddsValidatorServiceToContainer()
|
||||
{
|
||||
$constraint = new Valid();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
$this->assertServiceExists('Symfony.Component.Validator.Constraints.ValidValidator');
|
||||
}
|
||||
|
||||
private function assertServiceExists($id)
|
||||
{
|
||||
$this->assertTrue($this->container->has($id), 'Service ' . $id . ' doesn\'t exist on container');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException LogicException
|
||||
*/
|
||||
public function testGetInstanceThrowsLogicExceptionIfValidatorNotInstanceOfValidatorInterface()
|
||||
{
|
||||
$constraint = new InvalidConstraint();
|
||||
$validator = $this->factory->getInstance($constraint);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user