[Validator] Merged validate(), validateObject() and validateObjects() to simplify usage
This commit is contained in:
parent
51197f68a3
commit
08172bfe7b
|
@ -25,7 +25,7 @@ class Valid extends Constraint
|
|||
{
|
||||
public $traverse = true;
|
||||
|
||||
public $deep = false;
|
||||
public $deep = true;
|
||||
|
||||
public function __construct($options = null)
|
||||
{
|
||||
|
|
|
@ -143,7 +143,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution
|
|||
->getValidator()
|
||||
->inContext($this)
|
||||
->atPath($subPath)
|
||||
->validateObject($value, $groups)
|
||||
->validate($value, null, $groups)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -50,21 +50,11 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
$this->validator = $this->createValidator($this->metadataFactory);
|
||||
}
|
||||
|
||||
protected function validate($value, $constraints, $groups = null)
|
||||
protected function validate($value, $constraints = null, $groups = null)
|
||||
{
|
||||
return $this->validator->validate($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
protected function validateObject($object, $groups = null)
|
||||
{
|
||||
return $this->validator->validateObject($object, $groups);
|
||||
}
|
||||
|
||||
protected function validateObjects($objects, $groups = null, $deep = false)
|
||||
{
|
||||
return $this->validator->validateObjects($objects, $groups, $deep);
|
||||
}
|
||||
|
||||
protected function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
return $this->validator->validateProperty($object, $propertyName, $groups);
|
||||
|
@ -88,7 +78,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
'groups' => array('Group 1', 'Group 2'),
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validateObject($entity, array('Group 1', 'Group 2'));
|
||||
$violations = $this->validator->validate($entity, new Valid(), array('Group 1', 'Group 2'));
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -119,7 +109,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
)));
|
||||
|
||||
$sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3'));
|
||||
$violations = $this->validator->validateObject($entity, $sequence);
|
||||
$violations = $this->validator->validate($entity, new Valid(), $sequence);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -149,7 +139,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
)));
|
||||
|
||||
$sequence = new GroupSequence(array('Group 1', 'Entity'));
|
||||
$violations = $this->validator->validateObject($entity, $sequence);
|
||||
$violations = $this->validator->validate($entity, new Valid(), $sequence);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -167,7 +157,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
->getValidator()
|
||||
// Since the validator is not context aware, the group must
|
||||
// be passed explicitly
|
||||
->validateObject($value->reference, 'Group')
|
||||
->validate($value->reference, new Valid(), 'Group')
|
||||
;
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
|
@ -208,7 +198,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validateObject($entity, 'Group');
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -226,7 +216,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
->getValidator()
|
||||
->inContext($context)
|
||||
->atPath('subpath')
|
||||
->validateObject($value->reference)
|
||||
->validate($value->reference)
|
||||
;
|
||||
};
|
||||
|
||||
|
@ -252,7 +242,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validateObject($entity, 'Group');
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -277,7 +267,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
->getValidator()
|
||||
->inContext($context)
|
||||
->atPath('subpath')
|
||||
->validateObjects(array('key' => $value->reference))
|
||||
->validate(array('key' => $value->reference))
|
||||
;
|
||||
};
|
||||
|
||||
|
@ -303,7 +293,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validateObject($entity, 'Group');
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -317,44 +307,6 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testValidateAcceptsValid()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::ENTITY_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->metadata, $context->getMetadata());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity, $context->getValue());
|
||||
$test->assertSame($entity, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
// This is the same as when calling validateObject()
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testTraverseTraversableByDefault()
|
||||
{
|
||||
$test = $this;
|
||||
|
@ -380,7 +332,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($traversable, 'Group');
|
||||
$violations = $this->validate($traversable, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -403,7 +355,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
|
||||
$this->metadata->addConstraint(new Traverse());
|
||||
|
||||
$this->validator->validateObject($entity);
|
||||
$this->validator->validate($entity);
|
||||
}
|
||||
|
||||
public function testAddCustomizedViolation()
|
||||
|
@ -421,7 +373,7 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
|||
|
||||
$this->metadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validator->validateObject($entity);
|
||||
$violations = $this->validator->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
|
@ -47,21 +48,19 @@ abstract class AbstractLegacyApiTest extends AbstractValidatorTest
|
|||
$this->validator = $this->createValidator($this->metadataFactory);
|
||||
}
|
||||
|
||||
protected function validate($value, $constraints, $groups = null)
|
||||
protected function validate($value, $constraints = null, $groups = null)
|
||||
{
|
||||
if (null === $constraints) {
|
||||
$constraints = new Valid();
|
||||
}
|
||||
|
||||
if ($constraints instanceof Valid) {
|
||||
return $this->validator->validate($value, $groups, $constraints->traverse, $constraints->deep);
|
||||
}
|
||||
|
||||
return $this->validator->validateValue($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
protected function validateObject($object, $groups = null)
|
||||
{
|
||||
return $this->validator->validate($object, $groups);
|
||||
}
|
||||
|
||||
protected function validateObjects($objects, $groups = null, $deep = false)
|
||||
{
|
||||
return $this->validator->validate($objects, $groups, true, $deep);
|
||||
}
|
||||
|
||||
protected function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
return $this->validator->validateProperty($object, $propertyName, $groups);
|
||||
|
@ -226,7 +225,7 @@ abstract class AbstractLegacyApiTest extends AbstractValidatorTest
|
|||
|
||||
$this->metadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validator->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
|
|
@ -63,11 +63,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$this->referenceMetadata = null;
|
||||
}
|
||||
|
||||
abstract protected function validate($value, $constraints, $groups = null);
|
||||
|
||||
abstract protected function validateObject($object, $groups = null);
|
||||
|
||||
abstract protected function validateObjects($objects, $groups = null, $deep = false);
|
||||
abstract protected function validate($value, $constraints = null, $groups = null);
|
||||
|
||||
abstract protected function validateProperty($object, $propertyName, $groups = null);
|
||||
|
||||
|
@ -131,7 +127,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -171,7 +167,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -211,7 +207,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -249,7 +245,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObjects($array, 'Group');
|
||||
$violations = $this->validate($array, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -287,7 +283,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObjects($array, 'Group');
|
||||
$violations = $this->validate($array, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -325,7 +321,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObjects($traversable, 'Group');
|
||||
$violations = $this->validate($traversable, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -365,7 +361,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObjects($traversable, 'Group', true);
|
||||
$violations = $this->validate($traversable, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -404,7 +400,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -446,7 +442,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -488,7 +484,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -509,7 +505,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(0, $violations);
|
||||
|
@ -525,7 +521,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
|
||||
$this->validateObject($entity);
|
||||
$this->validate($entity);
|
||||
}
|
||||
|
||||
public function testArrayReference()
|
||||
|
@ -553,7 +549,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -593,7 +589,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -621,7 +617,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -641,7 +637,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -654,7 +650,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(0, $violations);
|
||||
|
@ -667,7 +663,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(0, $violations);
|
||||
|
@ -698,7 +694,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -727,7 +723,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(0, $violations);
|
||||
|
@ -745,28 +741,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'traverse' => false,
|
||||
)));
|
||||
|
||||
$this->validateObject($entity, 'Default', '');
|
||||
}
|
||||
|
||||
public function testNoRecursiveTraversableTraversal()
|
||||
{
|
||||
$entity = new Entity();
|
||||
$entity->reference = new \ArrayIterator(array(
|
||||
2 => new \ArrayIterator(array('key' => new Reference())),
|
||||
));
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator'));
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(0, $violations);
|
||||
$this->validate($entity);
|
||||
}
|
||||
|
||||
public function testEnableRecursiveTraversableTraversal()
|
||||
|
@ -798,7 +773,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
$violations = $this->validate($entity, null, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -954,7 +929,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$this->metadata->addPropertyConstraint('reference2', new Valid());
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -974,7 +949,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$this->metadata->addPropertyConstraint('reference2', new Valid());
|
||||
$this->referenceMetadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
$violations = $this->validate($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(2, $violations);
|
||||
|
@ -997,7 +972,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group 2',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group 2');
|
||||
$violations = $this->validate($entity, null, 'Group 2');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1020,7 +995,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
'groups' => 'Group 2',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, array('Group 1', 'Group 2'));
|
||||
$violations = $this->validate($entity, null, array('Group 1', 'Group 2'));
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(2, $violations);
|
||||
|
@ -1053,7 +1028,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3', 'Entity'));
|
||||
$this->metadata->setGroupSequence($sequence);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Default');
|
||||
$violations = $this->validate($entity, null, 'Default');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1087,7 +1062,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity');
|
||||
$this->metadata->setGroupSequence($sequence);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Default');
|
||||
$violations = $this->validate($entity, null, 'Default');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1119,7 +1094,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$sequence = new GroupSequence(array('Group 1', 'Entity'));
|
||||
$this->metadata->setGroupSequence($sequence);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Default');
|
||||
$violations = $this->validate($entity, null, 'Default');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1149,7 +1124,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$sequence = new GroupSequence(array('Group 1', 'Entity'));
|
||||
$this->metadata->setGroupSequence($sequence);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Other Group');
|
||||
$violations = $this->validate($entity, null, 'Other Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1185,7 +1160,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadataFactory->addMetadata($metadata);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Default');
|
||||
$violations = $this->validate($entity, null, 'Default');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
@ -1221,7 +1196,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$this->metadataFactory->addMetadata($metadata);
|
||||
|
||||
$violations = $this->validateObject($entity, 'Default');
|
||||
$violations = $this->validate($entity, null, 'Default');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Validator;
|
|||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\Traverse;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
@ -65,9 +66,11 @@ class ContextualValidator implements ContextualValidatorInterface
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function validate($value, $constraints, $groups = null)
|
||||
public function validate($value, $constraints = null, $groups = null)
|
||||
{
|
||||
if (!is_array($constraints)) {
|
||||
if (null === $constraints) {
|
||||
$constraints = array(new Valid());
|
||||
} elseif (!is_array($constraints)) {
|
||||
$constraints = array($constraints);
|
||||
}
|
||||
|
||||
|
@ -87,55 +90,6 @@ class ContextualValidator implements ContextualValidatorInterface
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function validateObject($object, $groups = null)
|
||||
{
|
||||
$classMetadata = $this->metadataFactory->getMetadataFor($object);
|
||||
|
||||
if (!$classMetadata instanceof ClassMetadataInterface) {
|
||||
throw new ValidatorException(sprintf(
|
||||
'The metadata factory should return instances of '.
|
||||
'"\Symfony\Component\Validator\Mapping\ClassMetadataInterface", '.
|
||||
'got: "%s".',
|
||||
is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)
|
||||
));
|
||||
}
|
||||
|
||||
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
||||
|
||||
$node = new ClassNode(
|
||||
$object,
|
||||
$classMetadata,
|
||||
$this->defaultPropertyPath,
|
||||
$groups
|
||||
);
|
||||
|
||||
$this->nodeTraverser->traverse(array($node), $this->context);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function validateObjects($objects, $groups = null)
|
||||
{
|
||||
if (!is_array($objects) && !$objects instanceof \Traversable) {
|
||||
throw new UnexpectedTypeException($objects, 'array or \Traversable');
|
||||
}
|
||||
|
||||
$traversalStrategy = TraversalStrategy::TRAVERSE;
|
||||
$groups = $groups ? $this->normalizeGroups($groups) : $this->defaultGroups;
|
||||
|
||||
$node = new CollectionNode(
|
||||
$objects,
|
||||
$this->defaultPropertyPath,
|
||||
$groups,
|
||||
null,
|
||||
$traversalStrategy
|
||||
);
|
||||
|
||||
$this->nodeTraverser->traverse(array($node), $this->context);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
$classMetadata = $this->metadataFactory->getMetadataFor($object);
|
||||
|
|
|
@ -30,28 +30,16 @@ interface ContextualValidatorInterface
|
|||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* If no constraint is passed, the constraint
|
||||
* {@link \Symfony\Component\Validator\Constraints\Valid} is assumed.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ContextualValidatorInterface This validator
|
||||
*/
|
||||
public function validate($value, $constraints, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a value.
|
||||
*
|
||||
* The accepted values depend on the {@link MetadataFactoryInterface}
|
||||
* implementation.
|
||||
*
|
||||
* @param mixed $object The value to validate
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ContextualValidatorInterface This validator
|
||||
*/
|
||||
public function validateObject($object, $groups = null);
|
||||
|
||||
public function validateObjects($objects, $groups = null);
|
||||
public function validate($value, $constraints = null, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a property of a value against its current value.
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Symfony\Component\Validator\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\GroupSequence;
|
||||
use Symfony\Component\Validator\Constraints\Traverse;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
|
||||
|
@ -24,18 +25,20 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface
|
|||
{
|
||||
public function validate($value, $groups = null, $traverse = false, $deep = false)
|
||||
{
|
||||
$numArgs = func_num_args();
|
||||
|
||||
// Use new signature if constraints are given in the second argument
|
||||
if (func_num_args() <= 3 && ($groups instanceof Constraint || (is_array($groups) && current($groups) instanceof Constraint))) {
|
||||
return parent::validate($value, $groups, $traverse);
|
||||
if (self::testConstraints($groups) && ($numArgs < 2 || 3 === $numArgs && self::testGroups($traverse))) {
|
||||
// Rename to avoid total confusion ;)
|
||||
$constraints = $groups;
|
||||
$groups = $traverse;
|
||||
|
||||
return parent::validate($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
if (is_array($value) || ($traverse && $value instanceof \Traversable)) {
|
||||
$constraint = new Valid(array('deep' => $deep));
|
||||
$constraint = new Valid(array('traverse' => $traverse, 'deep' => $deep));
|
||||
|
||||
return parent::validate($value, $constraint, $groups);
|
||||
}
|
||||
|
||||
return $this->validateObject($value, $groups);
|
||||
return parent::validate($value, $constraint, $groups);
|
||||
}
|
||||
|
||||
public function validateValue($value, $constraints, $groups = null)
|
||||
|
@ -47,4 +50,14 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface
|
|||
{
|
||||
return $this->metadataFactory;
|
||||
}
|
||||
|
||||
private static function testConstraints($constraints)
|
||||
{
|
||||
return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && current($constraints) instanceof Constraint);
|
||||
}
|
||||
|
||||
private static function testGroups($groups)
|
||||
{
|
||||
return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (is_string(current($groups)) || current($groups) instanceof GroupSequence));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,27 +84,13 @@ class Validator implements ValidatorInterface
|
|||
return $this->metadataFactory->hasMetadataFor($object);
|
||||
}
|
||||
|
||||
public function validate($value, $constraints, $groups = null)
|
||||
public function validate($value, $constraints = null, $groups = null)
|
||||
{
|
||||
return $this->startContext($value)
|
||||
->validate($value, $constraints, $groups)
|
||||
->getViolations();
|
||||
}
|
||||
|
||||
public function validateObject($object, $groups = null)
|
||||
{
|
||||
return $this->startContext($object)
|
||||
->validateObject($object, $groups)
|
||||
->getViolations();
|
||||
}
|
||||
|
||||
public function validateObjects($objects, $groups = null)
|
||||
{
|
||||
return $this->startContext($objects)
|
||||
->validateObjects($objects, $groups)
|
||||
->getViolations();
|
||||
}
|
||||
|
||||
public function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
return $this->startContext($object)
|
||||
|
|
|
@ -24,6 +24,9 @@ interface ValidatorInterface
|
|||
/**
|
||||
* Validates a value against a constraint or a list of constraints.
|
||||
*
|
||||
* If no constraint is passed, the constraint
|
||||
* {@link \Symfony\Component\Validator\Constraints\Valid} is assumed.
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
* @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
|
@ -31,23 +34,7 @@ interface ValidatorInterface
|
|||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validate($value, $constraints, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a value.
|
||||
*
|
||||
* The accepted values depend on the {@link MetadataFactoryInterface}
|
||||
* implementation.
|
||||
*
|
||||
* @param mixed $object The value to validate
|
||||
* @param array|null $groups The validation groups to validate.
|
||||
*
|
||||
* @return ConstraintViolationListInterface A list of constraint violations. If the
|
||||
* list is empty, validation succeeded.
|
||||
*/
|
||||
public function validateObject($object, $groups = null);
|
||||
|
||||
public function validateObjects($objects, $groups = null);
|
||||
public function validate($value, $constraints = null, $groups = null);
|
||||
|
||||
/**
|
||||
* Validates a property of a value against its current value.
|
||||
|
|
Reference in New Issue