Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FrameworkBundle] added a DIC tag-based constraint validator factory
- Loading branch information
1 parent
5d4c80f
commit e1f8423
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Validator; | ||
|
||
/* | ||
* 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\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory; | ||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
class ConstraintValidatorFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLoadTaggedServiceIdsSetsValidators() | ||
{ | ||
$service = 'validator_constraint_service'; | ||
$alias = 'validator_constraint_alias'; | ||
$validator = new \stdClass(); | ||
|
||
// mock ContainerBuilder b/c it implements TaggedContainerInterface | ||
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerBuilder'); | ||
$container | ||
->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('validator.constraint_validator') | ||
->will($this->returnValue(array( | ||
$service => array(array('alias' => $alias)), | ||
))); | ||
$container | ||
->expects($this->once()) | ||
->method('get') | ||
->with($service) | ||
->will($this->returnValue($validator)); | ||
|
||
$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint'); | ||
$constraint | ||
->expects($this->once()) | ||
->method('validatedBy') | ||
->will($this->returnValue($alias)); | ||
|
||
$factory = new ConstraintValidatorFactory($container); | ||
$factory->loadTaggedServiceIds($container); | ||
$this->assertSame($validator, $factory->getInstance($constraint)); | ||
} | ||
|
||
public function testGetInstanceException() | ||
{ | ||
$this->setExpectedException('InvalidArgumentException'); | ||
|
||
$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint'); | ||
$constraint | ||
->expects($this->once()) | ||
->method('validatedBy') | ||
->will($this->returnValue('foo')); | ||
|
||
$factory = new ConstraintValidatorFactory(new Container()); | ||
$factory->getInstance($constraint); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Validator; | ||
|
||
/* | ||
* 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\DependencyInjection\TaggedContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; | ||
|
||
/** | ||
* Uses a service container to create constraint validators. | ||
* | ||
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com> | ||
*/ | ||
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface | ||
{ | ||
protected $container; | ||
protected $validators = array(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container The service container | ||
*/ | ||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Loads ids for services tagged as constraint validators. | ||
* | ||
* @param TaggedContainerInterface $container The tagged service container | ||
*/ | ||
public function loadTaggedServiceIds(TaggedContainerInterface $container) | ||
{ | ||
foreach ($container->findTaggedServiceIds('validator.constraint_validator') as $id => $attributes) { | ||
if (isset($attributes[0]['alias'])) { | ||
$this->validators[$attributes[0]['alias']] = $id; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the validator for the supplied constraint. | ||
* | ||
* @param Constraint $constraint A constraint | ||
* | ||
* @return Symfony\Component\Validator\ConstraintValidator A validator for the supplied constraint | ||
* | ||
* @throws InvalidArgumentException If no validator for the supplied constraint is found | ||
*/ | ||
public function getInstance(Constraint $constraint) | ||
{ | ||
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
throw new \InvalidArgumentException(sprintf('There is no "%s" constraint validator.', $name)); | ||
} | ||
|
||
if (is_string($this->validators[$name])) { | ||
$this->validators[$name] = $this->container->get($this->validators[$name]); | ||
} | ||
|
||
return $this->validators[$name]; | ||
} | ||
} |