diff --git a/src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php b/src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php
index c2b7e10787..c6650842d7 100644
--- a/src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php
+++ b/src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php
@@ -19,13 +19,13 @@ namespace Symfony\Component\Validator\Constraints;
class GroupSequenceProvider
{
/**
- * The name of the provider class
- * @var string
+ * True if the group sequence provider should be used
+ * @var boolean
*/
- public $class;
+ public $active;
public function __construct(array $options)
{
- $this->class = $options['value'];
+ $this->active = (bool)$options['value'];
}
}
diff --git a/src/Symfony/Component/Validator/GraphWalker.php b/src/Symfony/Component/Validator/GraphWalker.php
index 5008748abb..489605b1d1 100644
--- a/src/Symfony/Component/Validator/GraphWalker.php
+++ b/src/Symfony/Component/Validator/GraphWalker.php
@@ -17,6 +17,7 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\MemberMetadata;
+use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* Responsible for walking over and initializing validation on different
@@ -68,10 +69,11 @@ class GraphWalker
}
if ($group === Constraint::DEFAULT_GROUP && ($metadata->hasGroupSequence() || $metadata->hasGroupSequenceProvider())) {
- $groups = $metadata->getGroupSequence();
- if ($groupSequenceProvider = $metadata->getGroupSequenceProvider()) {
- $groups = $groupSequenceProvider->getValidationGroups($object);
+ if ($metadata->hasGroupSequence()) {
+ $groups = $metadata->getGroupSequence();
+ } else {
+ $groups = $object->getValidationGroups();
}
foreach ($groups as $group) {
diff --git a/src/Symfony/Component/Validator/GroupSequenceProviderInterface.php b/src/Symfony/Component/Validator/GroupSequenceProviderInterface.php
index 5aceffc7e5..0265691a6d 100644
--- a/src/Symfony/Component/Validator/GroupSequenceProviderInterface.php
+++ b/src/Symfony/Component/Validator/GroupSequenceProviderInterface.php
@@ -20,9 +20,7 @@ interface GroupSequenceProviderInterface
* Returns which validation groups should be used for a certain state
* of the object.
*
- * @param mixed $object The object that is validated.
- *
* @return array An array of validation groups
*/
- function getValidationGroups($object);
+ function getValidationGroups();
}
diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
index 3dac6a9c09..a59092ba7f 100644
--- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
+++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
@@ -14,7 +14,6 @@ namespace Symfony\Component\Validator\Mapping;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\GroupDefinitionException;
-use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* Represents all the configured constraints on a given class.
@@ -30,8 +29,7 @@ class ClassMetadata extends ElementMetadata
public $properties = array();
public $getters = array();
public $groupSequence = array();
- public $groupSequenceProviderClass;
- public $groupSequenceProvider;
+ public $groupSequenceProvider = false;
private $reflClass;
/**
@@ -60,7 +58,7 @@ class ClassMetadata extends ElementMetadata
return array_merge(parent::__sleep(), array(
'getters',
'groupSequence',
- 'groupSequenceProviderClass',
+ 'groupSequenceProvider',
'members',
'name',
'properties',
@@ -251,6 +249,10 @@ class ClassMetadata extends ElementMetadata
*/
public function setGroupSequence(array $groups)
{
+ if ($this->hasGroupSequenceProvider()) {
+ throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
+ }
+
if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
}
@@ -299,64 +301,30 @@ class ClassMetadata extends ElementMetadata
}
/**
- * Sets the class name of the group sequence provider.
+ * Sets whether a group sequence provider should be used
*
- * @param string $class Sequence provider class name
+ * @param boolean $active
*/
- public function setGroupSequenceProviderClass($class)
+ public function setGroupSequenceProvider($active)
{
- $this->groupSequenceProviderClass = $class;
- $this->groupSequenceProvider = null;
+ if ($this->hasGroupSequenceProvider()) {
+ throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
+ }
+
+ if (!$this->getReflectionClass()->implementsInterface('Symfony\Component\Validator\GroupSequenceProviderInterface')) {
+ throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface', $this->name));
+ }
+
+ $this->groupSequenceProvider = $active;
}
/**
- * Returns the name of the group sequence provider class.
- *
- * @return string Class name
- */
- public function getGroupSequenceProviderClass()
- {
- return $this->groupSequenceProviderClass;
- }
-
- /**
- * Returns whether a group sequence provider is set.
+ * Returns whether the class has a group sequence provider.
*
* @return boolean
*/
public function hasGroupSequenceProvider()
{
- return $this->groupSequenceProviderClass || $this->groupSequenceProvider;
- }
-
- /**
- * Returns the group sequence provider if specified.
- *
- * @return GroupSequenceProviderInterface The provider or null
- */
- public function getGroupSequenceProvider()
- {
- if (!$this->groupSequenceProvider && $this->groupSequenceProviderClass) {
- $reflClass = new \ReflectionClass($this->groupSequenceProviderClass);
- $interface = 'Symfony\Component\Validator\GroupSequenceProviderInterface';
-
- if (!$reflClass->implementsInterface($interface)) {
- throw new \InvalidArgumentException(sprintf('The class "%s" must implement interface "%s".', $this->groupSequenceProviderClass, $interface));
- }
-
- $this->groupSequenceProvider = $reflClass->newInstance();
- }
-
return $this->groupSequenceProvider;
}
-
- /**
- * Sets the group sequence provider.
- *
- * @param GroupSequenceProviderInterface $provider Group sequence provider
- */
- public function setGroupSequenceProvider(GroupSequenceProviderInterface $provider)
- {
- $this->groupSequenceProvider = $provider;
- }
}
diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php
index a16dfdc3b6..691f830c88 100644
--- a/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php
+++ b/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php
@@ -58,6 +58,9 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
// Include constraints from all implemented interfaces
foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
+ if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->getName()) {
+ continue;
+ }
$metadata->mergeConstraints($this->getClassMetadata($interface->getName()));
}
diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
index 630aa2ff31..b67c9d7bcd 100644
--- a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
+++ b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
@@ -40,7 +40,7 @@ class AnnotationLoader implements LoaderInterface
if ($constraint instanceof GroupSequence) {
$metadata->setGroupSequence($constraint->groups);
} elseif ($constraint instanceof GroupSequenceProvider) {
- $metadata->setGroupSequenceProviderClass($constraint->class);
+ $metadata->setGroupSequenceProvider($constraint->active);
} elseif ($constraint instanceof Constraint) {
$metadata->addConstraint($constraint);
}
diff --git a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php
index ae48f226ff..0e3bedeaca 100644
--- a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php
+++ b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php
@@ -44,7 +44,7 @@ class XmlFileLoader extends FileLoader
$xml = $this->classes[$metadata->getClassName()];
foreach ($xml->{'group-sequence-provider'} as $provider) {
- $metadata->setGroupSequenceProviderClass($provider['class']);
+ $metadata->setGroupSequenceProvider((bool)$provider['active']);
}
foreach ($this->parseConstraints($xml->constraint) as $constraint) {
diff --git a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
index 62750f40a5..db65f684bb 100644
--- a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
+++ b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
@@ -55,7 +55,7 @@ class YamlFileLoader extends FileLoader
$yaml = $this->classes[$metadata->getClassName()];
if (isset($yaml['group_sequence_provider'])) {
- $metadata->setGroupSequenceProviderClass($yaml['group_sequence_provider']);
+ $metadata->setGroupSequenceProvider((bool)$yaml['group_sequence_provider']);
}
if (isset($yaml['constraints'])) {
diff --git a/src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd b/src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd
index f16a736d5e..60a91be73e 100644
--- a/src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd
+++ b/src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd
@@ -66,7 +66,7 @@
Defines the name of the group sequence provider for a class.
]]>
-
+
diff --git a/tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php b/tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php
index 6b540e84c1..62aaa15ea4 100644
--- a/tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php
+++ b/tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php
@@ -6,13 +6,14 @@ require_once __DIR__.'/EntityParent.php';
require_once __DIR__.'/EntityInterface.php';
use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
* @Assert\GroupSequence({"Foo", "Entity"})
* @Assert\GroupSequenceProvider("Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider")
*/
-class Entity extends EntityParent implements EntityInterface
+class Entity extends EntityParent implements EntityInterface, GroupSequenceProviderInterface
{
/**
* @Assert\NotNull
@@ -29,6 +30,8 @@ class Entity extends EntityParent implements EntityInterface
protected $lastName;
public $reference;
+ protected $groups = array();
+
private $internal;
public function __construct($internal = null)
@@ -48,4 +51,14 @@ class Entity extends EntityParent implements EntityInterface
{
return $this->lastName;
}
+
+ public function setGroups($groups)
+ {
+ $this->groups = $groups;
+ }
+
+ public function getValidationGroups()
+ {
+ return $this->groups;
+ }
}
diff --git a/tests/Symfony/Tests/Component/Validator/Fixtures/GroupSequenceProvider.php b/tests/Symfony/Tests/Component/Validator/Fixtures/GroupSequenceProvider.php
deleted file mode 100644
index 3e9cf9c1b6..0000000000
--- a/tests/Symfony/Tests/Component/Validator/Fixtures/GroupSequenceProvider.php
+++ /dev/null
@@ -1,20 +0,0 @@
-groups = $groups;
- }
-
- public function getValidationGroups($object)
- {
- return $this->groups;
- }
-}
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php b/tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php
index 87d2ef4144..646c06e08a 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php
@@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Validator\Mapping;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
+use Symfony\Component\Validator\Exception\GroupDefinitionException;
use Symfony\Tests\Component\Validator\Fixtures\Entity;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
@@ -24,7 +25,6 @@ require_once __DIR__.'/../Fixtures/Entity.php';
require_once __DIR__.'/../Fixtures/ConstraintA.php';
require_once __DIR__.'/../Fixtures/ConstraintB.php';
require_once __DIR__.'/../Fixtures/PropertyConstraint.php';
-require_once __DIR__.'/../Fixtures/GroupSequenceProvider.php';
class ClassMetadataTest extends \PHPUnit_Framework_TestCase
{
@@ -194,16 +194,14 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
public function testGroupSequenceProvider()
{
- $this->assertNull($this->metadata->getGroupSequenceProvider());
-
- $this->metadata->setGroupSequenceProviderClass('stdClass');
+ $metadata = new ClassMetadata('stdClass');
try {
- $this->metadata->getGroupSequenceProvider();
+ $metadata->setGroupSequenceProvider(true);
$this->fail();
- } catch(\InvalidArgumentException $e) {}
+ } catch(GroupDefinitionException $e) {}
- $this->metadata->setGroupSequenceProviderClass('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider');
- $this->assertTrue($this->metadata->getGroupSequenceProvider() instanceof GroupSequenceProvider);
+ $this->metadata->setGroupSequenceProvider(true);
+ $this->assertTrue($this->metadata->hasGroupSequenceProvider());
}
}
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php
index adaeee9423..cf9f30669f 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php
@@ -73,7 +73,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
- $expected->setGroupSequenceProviderClass('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider');
+ $expected->setGroupSequenceProvider(true);
// load reflection class so that the comparison passes
$expected->getReflectionClass();
@@ -138,7 +138,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
- $expected->setGroupSequenceProviderClass('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider');
+ $expected->setGroupSequenceProvider(true);
// load reflection class so that the comparison passes
$expected->getReflectionClass();
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/XmlFileLoaderTest.php b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/XmlFileLoaderTest.php
index 035c0f786b..5aa47b001e 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/XmlFileLoaderTest.php
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/XmlFileLoaderTest.php
@@ -67,7 +67,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
- $expected->setGroupSequenceProviderClass('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider');
+ $expected->setGroupSequenceProvider(true);
$this->assertEquals($expected, $metadata);
}
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php
index de6c687b7f..a1d54e5b9c 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php
@@ -85,7 +85,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
- $expected->setGroupSequenceProviderClass('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider');
+ $expected->setGroupSequenceProvider(true);
$this->assertEquals($expected, $metadata);
}
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.xml b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.xml
index cd669e51f4..7c1eeeeb32 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.xml
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.xml
@@ -9,7 +9,7 @@
-
+
diff --git a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.yml b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.yml
index 0d0083ae59..1ea139b153 100644
--- a/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.yml
+++ b/tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.yml
@@ -2,7 +2,7 @@ namespaces:
custom: Symfony\Tests\Component\Validator\Fixtures\
Symfony\Tests\Component\Validator\Fixtures\Entity:
- group_sequence_provider: Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider
+ group_sequence_provider: true
constraints:
# Custom constraint
- Symfony\Tests\Component\Validator\Fixtures\ConstraintA: ~
diff --git a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php
index 722f31472e..3726761571 100644
--- a/tests/Symfony/Tests/Component/Validator/ValidatorTest.php
+++ b/tests/Symfony/Tests/Component/Validator/ValidatorTest.php
@@ -19,7 +19,6 @@ require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
use Symfony\Tests\Component\Validator\Fixtures\Entity;
use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
-use Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider;
use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
@@ -125,8 +124,6 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
public function testValidate_groupSequenceProvider()
{
- $groupSequenceProvider = new GroupSequenceProvider;
-
$entity = new Entity();
$metadata = new ClassMetadata(get_class($entity));
$metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
@@ -135,7 +132,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
$metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
'groups' => 'Second',
)));
- $metadata->setGroupSequenceProvider($groupSequenceProvider);
+ $metadata->setGroupSequenceProvider(true);
$this->factory->addClassMetadata($metadata);
$violations = new ConstraintViolationList();
@@ -147,7 +144,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
''
));
- $groupSequenceProvider->setGroups(array('First'));
+ $entity->setGroups(array('First'));
$result = $this->validator->validate($entity);
$this->assertEquals($violations, $result);
@@ -160,11 +157,11 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
''
));
- $groupSequenceProvider->setGroups(array('Second'));
+ $entity->setGroups(array('Second'));
$result = $this->validator->validate($entity);
$this->assertEquals($violations, $result);
- $groupSequenceProvider->setGroups(array());
+ $entity->setGroups(array());
$result = $this->validator->validate($entity);
$this->assertEquals(new ConstraintViolationList(), $result);
}