2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator;
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Mapping\ElementMetadata;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* The default implementation of the ValidatorInterface.
|
|
|
|
*
|
|
|
|
* This service can be used to validate objects, properties and raw values
|
|
|
|
* against constraints.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
class Validator implements ValidatorInterface
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $metadataFactory;
|
|
|
|
protected $validatorFactory;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ClassMetadataFactoryInterface $metadataFactory,
|
2010-09-24 15:34:58 +01:00
|
|
|
ConstraintValidatorFactoryInterface $validatorFactory
|
2010-06-24 10:24:08 +01:00
|
|
|
)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->metadataFactory = $metadataFactory;
|
|
|
|
$this->validatorFactory = $validatorFactory;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function validate($object, $groups = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$metadata = $this->metadataFactory->getClassMetadata(get_class($object));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$walk = function(GraphWalker $walker, $group) use ($metadata, $object) {
|
2011-01-19 14:57:40 +00:00
|
|
|
return $walker->walkObject($metadata, $object, $group, '');
|
2010-06-24 10:24:08 +01:00
|
|
|
};
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
return $this->validateGraph($object, $walk, $groups);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function validateProperty($object, $property, $groups = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$metadata = $this->metadataFactory->getClassMetadata(get_class($object));
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$walk = function(GraphWalker $walker, $group) use ($metadata, $property, $object) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return $walker->walkProperty($metadata, $property, $object, $group, '');
|
|
|
|
};
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
return $this->validateGraph($object, $walk, $groups);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function validatePropertyValue($class, $property, $value, $groups = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$metadata = $this->metadataFactory->getClassMetadata($class);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
$walk = function(GraphWalker $walker, $group) use ($metadata, $property, $value) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return $walker->walkPropertyValue($metadata, $property, $value, $group, '');
|
|
|
|
};
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
return $this->validateGraph($class, $walk, $groups);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function validateValue($value, Constraint $constraint, $groups = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-11-16 23:51:45 +00:00
|
|
|
$walk = function(GraphWalker $walker, $group) use ($constraint, $value) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return $walker->walkConstraint($constraint, $value, $group, '');
|
|
|
|
};
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
return $this->validateGraph($value, $walk, $groups);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
protected function validateGraph($root, \Closure $walk, $groups = null)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-09-24 15:34:58 +01:00
|
|
|
$walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory);
|
2010-11-16 23:51:45 +00:00
|
|
|
$groups = $groups ? (array)$groups : array(Constraint::DEFAULT_GROUP);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
2010-11-16 23:51:45 +00:00
|
|
|
$walk($walker, $group);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 23:51:45 +00:00
|
|
|
return $walker->getViolations();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|