[Validator] Custom built constraints can now be used in the loaders

This commit is contained in:
Bernhard Schussek 2010-06-29 13:31:46 +02:00
parent 5701d6533c
commit a747987625
9 changed files with 38 additions and 5 deletions

View File

@ -22,4 +22,25 @@ abstract class FileLoader implements LoaderInterface
$this->file = $file;
}
/**
* Creates a new constraint instance for the given constraint name
*
* @param string $name The constraint name. Either a constraint relative
* to the default constraint namespace, or a fully
* qualified class name
* @param array $options The constraint options
*
* @return Constraint
*/
protected function newConstraint($name, $options)
{
if (strpos($name, '\\') !== false && class_exists($name)) {
$className = (string)$name;
} else {
$className = 'Symfony\\Components\\Validator\\Constraints\\'.$name;
}
return new $className($options);
}
}

View File

@ -63,8 +63,6 @@ class XmlFileLoader extends FileLoader
$constraints = array();
foreach ($nodes as $node) {
$className = 'Symfony\\Components\\Validator\\Constraints\\'.$node['name'];
if (count($node) > 0) {
if (count($node->value) > 0) {
$options = $this->parseValues($node->value);
@ -81,7 +79,7 @@ class XmlFileLoader extends FileLoader
$options = null;
}
$constraints[] = new $className($options);
$constraints[] = $this->newConstraint($node['name'], $options);
}
return $constraints;

View File

@ -68,14 +68,13 @@ class YamlFileLoader extends FileLoader
foreach ($nodes as $name => $childNodes) {
if (is_numeric($name) && is_array($childNodes) && count($childNodes) == 1) {
$className = 'Symfony\\Components\\Validator\\Constraints\\'.key($childNodes);
$options = current($childNodes);
if (is_array($options)) {
$options = $this->parseNodes($options);
}
$values[] = new $className($options);
$values[] = $this->newConstraint(key($childNodes), $options);
} else {
if (is_array($childNodes)) {
$childNodes = $this->parseNodes($childNodes);

View File

@ -8,6 +8,7 @@ require_once __DIR__.'/EntityInterface.php';
/**
* @Validation({
* @NotNull,
* @Symfony\Tests\Components\Validator\Fixtures\ConstraintA,
* @Min(3),
* @Choice({"A", "B"}),
* @All({@NotNull, @Min(3)}),

View File

@ -3,6 +3,7 @@
namespace Symfony\Tests\Components\Validator\Mapping\Loader;
require_once __DIR__.'/../../Fixtures/Entity.php';
require_once __DIR__.'/../../Fixtures/ConstraintA.php';
use Symfony\Components\Validator\Constraints\All;
use Symfony\Components\Validator\Constraints\Collection;
@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
use Symfony\Components\Validator\Constraints\Choice;
use Symfony\Components\Validator\Mapping\ClassMetadata;
use Symfony\Components\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
{
@ -39,6 +41,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
$expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
$expected->addConstraint(new NotNull());
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new Min(3));
$expected->addConstraint(new Choice(array('A', 'B')));
$expected->addConstraint(new All(array(new NotNull(), new Min(3))));

View File

@ -3,6 +3,7 @@
namespace Symfony\Tests\Components\Validator\Mapping\Loader;
require_once __DIR__.'/../../Fixtures/Entity.php';
require_once __DIR__.'/../../Fixtures/ConstraintA.php';
use Symfony\Components\Validator\Constraints\All;
use Symfony\Components\Validator\Constraints\Collection;
@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
use Symfony\Components\Validator\Constraints\Choice;
use Symfony\Components\Validator\Mapping\ClassMetadata;
use Symfony\Components\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
@ -39,6 +41,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
$expected->addConstraint(new NotNull());
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new Min(3));
$expected->addConstraint(new Choice(array('A', 'B')));
$expected->addConstraint(new All(array(new NotNull(), new Min(3))));

View File

@ -3,6 +3,7 @@
namespace Symfony\Tests\Components\Validator\Mapping\Loader;
require_once __DIR__.'/../../Fixtures/Entity.php';
require_once __DIR__.'/../../Fixtures/ConstraintA.php';
use Symfony\Components\Validator\Constraints\All;
use Symfony\Components\Validator\Constraints\Collection;
@ -11,6 +12,7 @@ use Symfony\Components\Validator\Constraints\Min;
use Symfony\Components\Validator\Constraints\Choice;
use Symfony\Components\Validator\Mapping\ClassMetadata;
use Symfony\Components\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
@ -39,6 +41,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$expected = new ClassMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
$expected->addConstraint(new NotNull());
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new Min(3));
$expected->addConstraint(new Choice(array('A', 'B')));
$expected->addConstraint(new All(array(new NotNull(), new Min(3))));

View File

@ -10,6 +10,9 @@
<!-- Constraint without value -->
<constraint name="NotNull" />
<!-- Custom constraint -->
<constraint name="Symfony\Tests\Components\Validator\Fixtures\ConstraintA" />
<!-- Constraint with single value -->
<constraint name="Min">3</constraint>

View File

@ -2,6 +2,8 @@ Symfony\Tests\Components\Validator\Fixtures\Entity:
constraints:
# Constraint without value
- NotNull: ~
# Custom constraint
- Symfony\Tests\Components\Validator\Fixtures\ConstraintA: ~
# Constraint with single value
- Min: 3
# Constraint with multiple values