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;
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2012-11-22 14:58:46 +00:00
|
|
|
* Default implementation of {@ConstraintViolationInterface}.
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
2012-11-22 14:58:46 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2012-11-22 14:58:46 +00:00
|
|
|
class ConstraintViolation implements ConstraintViolationInterface
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-11-27 21:42:05 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $message;
|
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $messageTemplate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $messageParameters;
|
|
|
|
|
|
|
|
/**
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var int|null
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
|
|
|
private $messagePluralization;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $root;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $propertyPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $invalidValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $code;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
|
|
|
* Creates a new constraint violation.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param string $message The violation message.
|
|
|
|
* @param string $messageTemplate The raw violation message.
|
|
|
|
* @param array $messageParameters The parameters to substitute
|
|
|
|
* in the raw message.
|
|
|
|
* @param mixed $root The value originally passed
|
|
|
|
* to the validator.
|
|
|
|
* @param string $propertyPath The property path from the
|
|
|
|
* root value to the invalid
|
|
|
|
* value.
|
|
|
|
* @param mixed $invalidValue The invalid value causing the
|
|
|
|
* violation.
|
|
|
|
* @param int|null $messagePluralization The pluralization parameter.
|
|
|
|
* @param mixed $code The error code of the
|
|
|
|
* violation, if any.
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
2012-11-27 21:42:05 +00:00
|
|
|
public function __construct($message, $messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2012-11-27 21:42:05 +00:00
|
|
|
$this->message = $message;
|
2010-09-24 15:34:58 +01:00
|
|
|
$this->messageTemplate = $messageTemplate;
|
|
|
|
$this->messageParameters = $messageParameters;
|
2011-04-14 10:48:15 +01:00
|
|
|
$this->messagePluralization = $messagePluralization;
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->root = $root;
|
|
|
|
$this->propertyPath = $propertyPath;
|
|
|
|
$this->invalidValue = $invalidValue;
|
2012-05-21 16:13:00 +01:00
|
|
|
$this->code = $code;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
/**
|
2012-11-22 14:58:46 +00:00
|
|
|
* Converts the violation into a string for debugging purposes.
|
|
|
|
*
|
|
|
|
* @return string The violation as string.
|
2012-02-01 12:53:45 +00:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2014-01-24 17:40:09 +00:00
|
|
|
if (is_object($this->root)) {
|
2014-04-10 16:39:39 +01:00
|
|
|
$class = 'Object('.get_class($this->root).')';
|
2014-01-24 17:40:09 +00:00
|
|
|
} elseif (is_array($this->root)) {
|
2014-04-10 16:39:39 +01:00
|
|
|
$class = 'Array';
|
2014-01-24 17:40:09 +00:00
|
|
|
} else {
|
|
|
|
$class = (string) $this->root;
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$propertyPath = (string) $this->propertyPath;
|
2012-05-21 16:13:00 +01:00
|
|
|
$code = $this->code;
|
2012-02-01 12:53:45 +00:00
|
|
|
|
|
|
|
if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
|
|
|
|
$class .= '.';
|
|
|
|
}
|
|
|
|
|
2012-05-21 16:13:00 +01:00
|
|
|
if (!empty($code)) {
|
2013-04-02 10:39:57 +01:00
|
|
|
$code = ' (code '.$code.')';
|
2012-05-21 16:13:00 +01:00
|
|
|
}
|
|
|
|
|
2013-04-02 10:39:57 +01:00
|
|
|
return $class.$propertyPath.":\n ".$this->getMessage().$code;
|
2012-02-01 12:53:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-09-24 15:34:58 +01:00
|
|
|
public function getMessageTemplate()
|
|
|
|
{
|
|
|
|
return $this->messageTemplate;
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-09-24 15:34:58 +01:00
|
|
|
public function getMessageParameters()
|
|
|
|
{
|
|
|
|
return $this->messageParameters;
|
|
|
|
}
|
|
|
|
|
2011-04-14 10:48:15 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-04-14 10:48:15 +01:00
|
|
|
*/
|
|
|
|
public function getMessagePluralization()
|
|
|
|
{
|
|
|
|
return $this->messagePluralization;
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getMessage()
|
|
|
|
{
|
2012-11-27 21:42:05 +00:00
|
|
|
return $this->message;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->root;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getPropertyPath()
|
|
|
|
{
|
|
|
|
return $this->propertyPath;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getInvalidValue()
|
|
|
|
{
|
|
|
|
return $this->invalidValue;
|
|
|
|
}
|
2012-05-21 16:13:00 +01:00
|
|
|
|
2012-11-22 14:58:46 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2012-11-22 14:58:46 +00:00
|
|
|
*/
|
2012-05-21 16:13:00 +01:00
|
|
|
public function getCode()
|
|
|
|
{
|
|
|
|
return $this->code;
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|