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-10-02 11:42:31 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
if ($group === Constraint::DEFAULT_GROUP && $metadata->hasGroupSequence()) {
|
|
|
|
$groups = $metadata->getGroupSequence();
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$this->walkClassForGroup($metadata, $object, $group, $propertyPath, Constraint::DEFAULT_GROUP);
|
|
|
|
|
|
|
|
if (count($this->getViolations()) > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->walkClassForGroup($metadata, $object, $group, $propertyPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function walkClassForGroup(ClassMetadata $metadata, $object, $group, $propertyPath, $propagatedGroup = null)
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($metadata->findConstraints($group) as $constraint) {
|
|
|
|
$this->walkConstraint($constraint, $object, $group, $propertyPath);
|
|
|
|
}
|
|
|
|
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null !== $object) {
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($metadata->getConstrainedProperties() as $property) {
|
|
|
|
$localPropertyPath = empty($propertyPath) ? $property : $propertyPath.'.'.$property;
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$this->walkProperty($metadata, $property, $object, $group, $localPropertyPath, $propagatedGroup);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
public function walkProperty(ClassMetadata $metadata, $property, $object, $group, $propertyPath, $propagatedGroup = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($metadata->getMemberMetadatas($property) as $member) {
|
2010-11-16 23:51:45 +00:00
|
|
|
$this->walkMember($member, $member->getValue($object), $group, $propertyPath, $propagatedGroup);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
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-11-16 23:51:45 +00:00
|
|
|
protected function walkMember(MemberMetadata $metadata, $value, $group, $propertyPath, $propagatedGroup = null)
|
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-11-16 22:41:46 +00:00
|
|
|
|
|
|
|
if ($metadata->isCascaded()) {
|
2010-11-16 23:51:45 +00:00
|
|
|
$this->walkReference($value, $propagatedGroup ?: $group, $propertyPath);
|
2010-11-16 22:41:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function walkReference($value, $group, $propertyPath)
|
|
|
|
{
|
|
|
|
if (null !== $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $key => $element) {
|
|
|
|
$this->walkReference($element, $group, $propertyPath.'['.$key.']');
|
|
|
|
}
|
|
|
|
} else if (!is_object($value)) {
|
|
|
|
throw new UnexpectedTypeException($value, 'object or array');
|
|
|
|
} else {
|
|
|
|
$metadata = $this->metadataFactory->getClassMetadata(get_class($value));
|
|
|
|
$this->walkClass($metadata, $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
|
|
|
}
|
|
|
|
}
|