2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Constraints\All;
|
|
|
|
use Symfony\Component\Validator\Constraints\Valid;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
|
|
|
use Symfony\Component\Validator\Mapping\MemberMetadata;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class GraphWalker
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $context;
|
|
|
|
protected $validatorFactory;
|
|
|
|
protected $metadataFactory;
|
|
|
|
|
2010-09-24 15:34:58 +01:00
|
|
|
public function __construct($root, ClassMetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $factory)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-09-24 15:34:58 +01:00
|
|
|
$this->context = new ValidationContext($root, $this, $metadataFactory);
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validatorFactory = $factory;
|
|
|
|
$this->metadataFactory = $metadataFactory;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getViolations()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return $this->context->getViolations();
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function walkClass(ClassMetadata $metadata, $object, $group, $propertyPath)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->context->setCurrentClass($metadata->getClassName());
|
|
|
|
|
|
|
|
foreach ($metadata->findConstraints($group) as $constraint) {
|
|
|
|
$this->walkConstraint($constraint, $object, $group, $propertyPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($object !== null) {
|
|
|
|
foreach ($metadata->getConstrainedProperties() as $property) {
|
|
|
|
$localPropertyPath = empty($propertyPath) ? $property : $propertyPath.'.'.$property;
|
|
|
|
|
|
|
|
$this->walkProperty($metadata, $property, $object, $group, $localPropertyPath);
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function walkProperty(ClassMetadata $metadata, $property, $object, $group, $propertyPath)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($metadata->getMemberMetadatas($property) as $member) {
|
|
|
|
$this->walkMember($member, $member->getValue($object), $group, $propertyPath);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function walkPropertyValue(ClassMetadata $metadata, $property, $value, $group, $propertyPath)
|
|
|
|
{
|
|
|
|
foreach ($metadata->getMemberMetadatas($property) as $member) {
|
|
|
|
$this->walkMember($member, $value, $group, $propertyPath);
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
protected function walkMember(MemberMetadata $metadata, $value, $group, $propertyPath)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->context->setCurrentProperty($metadata->getPropertyName());
|
|
|
|
|
|
|
|
foreach ($metadata->findConstraints($group) as $constraint) {
|
|
|
|
$this->walkConstraint($constraint, $value, $group, $propertyPath);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function walkConstraint(Constraint $constraint, $value, $group, $propertyPath)
|
|
|
|
{
|
|
|
|
$validator = $this->validatorFactory->getInstance($constraint);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->context->setPropertyPath($propertyPath);
|
|
|
|
$this->context->setGroup($group);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$validator->initialize($this->context);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$validator->isValid($value, $constraint)) {
|
|
|
|
$this->context->addViolation(
|
|
|
|
$validator->getMessageTemplate(),
|
|
|
|
$validator->getMessageParameters(),
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
}
|