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
|
|
|
/**
|
2012-01-31 17:42:14 +00:00
|
|
|
* Stores the state of the current node in the validation graph.
|
2011-01-16 04:41:23 +00:00
|
|
|
*
|
2012-01-31 20:35:48 +00:00
|
|
|
* This class is immutable by design.
|
|
|
|
*
|
|
|
|
* It is used by the GraphWalker to initialize validation of different items
|
|
|
|
* and keep track of the violations.
|
2011-01-16 04:41:23 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2012-01-31 17:42:14 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2011-01-19 14:02:00 +00:00
|
|
|
class ExecutionContext
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
private $globalContext;
|
|
|
|
private $propertyPath;
|
|
|
|
private $value;
|
|
|
|
private $group;
|
|
|
|
private $class;
|
|
|
|
private $property;
|
|
|
|
|
|
|
|
public function __construct(GlobalExecutionContext $globalContext, $value, $propertyPath, $group, $class = null, $property = null)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext = $globalContext;
|
|
|
|
$this->value = $value;
|
|
|
|
$this->propertyPath = $propertyPath;
|
|
|
|
$this->group = $group;
|
|
|
|
$this->class = $class;
|
|
|
|
$this->property = $property;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __clone()
|
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext = clone $this->globalContext;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
2012-01-31 20:35:48 +00:00
|
|
|
* Adds a violation at the current node of the validation graph.
|
|
|
|
*
|
|
|
|
* @param string $message The error message.
|
|
|
|
* @param array $params The parameters parsed into the error message.
|
|
|
|
* @param mixed $invalidValue The invalid, validated value.
|
|
|
|
*
|
2011-07-20 09:37:57 +01:00
|
|
|
* @api
|
|
|
|
*/
|
2012-01-31 17:42:14 +00:00
|
|
|
public function addViolation($message, array $params = array(), $invalidValue = null)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext->addViolation(new ConstraintViolation(
|
2010-09-24 15:34:58 +01:00
|
|
|
$message,
|
|
|
|
$params,
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext->getRoot(),
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->propertyPath,
|
2012-01-31 17:42:14 +00:00
|
|
|
// check using func_num_args() to allow passing null values
|
|
|
|
func_num_args() === 3 ? $invalidValue : $this->value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:35:48 +00:00
|
|
|
/**
|
|
|
|
* Adds a violation at the validation graph node with the given property
|
|
|
|
* path.
|
|
|
|
*
|
|
|
|
* @param string $propertyPath The property path for the violation.
|
|
|
|
* @param string $message The error message.
|
|
|
|
* @param array $params The parameters parsed into the error message.
|
|
|
|
* @param mixed $invalidValue The invalid, validated value.
|
|
|
|
*/
|
2012-01-31 17:42:14 +00:00
|
|
|
public function addViolationAt($propertyPath, $message, array $params = array(), $invalidValue = null)
|
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext->addViolation(new ConstraintViolation(
|
2012-01-31 17:42:14 +00:00
|
|
|
$message,
|
|
|
|
$params,
|
2012-01-31 20:35:48 +00:00
|
|
|
$this->globalContext->getRoot(),
|
2012-01-31 17:42:14 +00:00
|
|
|
$propertyPath,
|
|
|
|
// check using func_num_args() to allow passing null values
|
|
|
|
func_num_args() === 4 ? $invalidValue : $this->value
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:35:48 +00:00
|
|
|
/**
|
|
|
|
* Adds a violation at the child of the current validation graph node with
|
|
|
|
* the given property path.
|
|
|
|
*
|
|
|
|
* @param string $childPropertyPath The property path of the child node.
|
|
|
|
* @param string $message The error message.
|
|
|
|
* @param array $params The parameters parsed into the error message.
|
|
|
|
* @param mixed $invalidValue The invalid, validated value.
|
|
|
|
*/
|
|
|
|
public function addNestedViolationAt($childPropertyPath, $message, array $params = array(), $invalidValue = null)
|
|
|
|
{
|
|
|
|
$propertyPath = $this->propertyPath;
|
|
|
|
|
|
|
|
if ('' !== $propertyPath && '' !== $childPropertyPath && '[' !== $childPropertyPath[0]) {
|
|
|
|
$propertyPath .= '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->globalContext->addViolation(new ConstraintViolation(
|
|
|
|
$message,
|
|
|
|
$params,
|
|
|
|
$this->globalContext->getRoot(),
|
|
|
|
$propertyPath . $childPropertyPath,
|
|
|
|
// check using func_num_args() to allow passing null values
|
|
|
|
func_num_args() === 4 ? $invalidValue : $this->value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @return ConstraintViolationList
|
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 getViolations()
|
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
return $this->globalContext->getViolations();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoot()
|
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
return $this->globalContext->getRoot();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPropertyPath()
|
|
|
|
{
|
|
|
|
return $this->propertyPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCurrentClass()
|
|
|
|
{
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCurrentProperty()
|
|
|
|
{
|
|
|
|
return $this->property;
|
|
|
|
}
|
|
|
|
|
2012-01-31 17:42:14 +00:00
|
|
|
public function getCurrentValue()
|
|
|
|
{
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getGroup()
|
|
|
|
{
|
|
|
|
return $this->group;
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @return GraphWalker
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getGraphWalker()
|
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
return $this->globalContext->getGraphWalker();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @return ClassMetadataFactoryInterface
|
|
|
|
*/
|
2011-02-15 09:36:58 +00:00
|
|
|
public function getMetadataFactory()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
return $this->globalContext->getMetadataFactory();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|