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
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
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\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.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
class Validator implements ValidatorInterface
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $metadataFactory;
|
|
|
|
protected $validatorFactory;
|
2011-07-06 11:34:41 +01:00
|
|
|
protected $validatorInitializers;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ClassMetadataFactoryInterface $metadataFactory,
|
2011-07-06 11:34:41 +01:00
|
|
|
ConstraintValidatorFactoryInterface $validatorFactory,
|
|
|
|
array $validatorInitializers = array()
|
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;
|
2011-07-06 11:34:41 +01:00
|
|
|
$this->validatorInitializers = $validatorInitializers;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-04-22 16:41:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getMetadataFactory()
|
|
|
|
{
|
|
|
|
return $this->metadataFactory;
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
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}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
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}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
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}
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
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
|
|
|
{
|
2011-07-06 11:34:41 +01:00
|
|
|
$walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory, $this->validatorInitializers);
|
2011-03-31 13:02:00 +01: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
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|