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
private $message ;
2012-11-22 14:58:46 +00:00
private $messageTemplate ;
2014-02-21 17:18:23 +00:00
private $parameters ;
private $plural ;
2012-11-22 14:58:46 +00:00
private $root ;
private $propertyPath ;
private $invalidValue ;
2014-08-12 14:33:06 +01:00
private $constraint ;
2012-11-22 14:58:46 +00:00
private $code ;
2014-09-26 11:44:24 +01:00
private $cause ;
2012-11-22 14:58:46 +00:00
/**
* Creates a new constraint violation .
*
2019-08-26 10:29:06 +01:00
* @ param string | object $message The violation message as a string or a stringable object
* @ param string $messageTemplate The raw violation message
* @ param array $parameters The parameters to substitute in the
2019-08-07 16:24:11 +01:00
* raw violation message
2019-08-26 10:29:06 +01:00
* @ param mixed $root The value originally passed to the
2019-08-07 16:24:11 +01:00
* validator
2019-08-26 10:29:06 +01:00
* @ param string $propertyPath The property path from the root
2019-08-07 16:24:11 +01:00
* value to the invalid value
2019-08-26 10:29:06 +01:00
* @ param mixed $invalidValue The invalid value that caused this
2019-08-07 16:24:11 +01:00
* violation
2019-08-26 10:29:06 +01:00
* @ param int | null $plural The number for determining the plural
2019-08-07 16:24:11 +01:00
* form when translating the message
2019-08-26 10:29:06 +01:00
* @ param string | null $code The error code of the violation
* @ param mixed $cause The cause of the violation
2014-08-12 14:33:06 +01:00
*/
2019-08-08 17:57:01 +01:00
public function __construct ( $message , ? string $messageTemplate , array $parameters , $root , ? string $propertyPath , $invalidValue , int $plural = null , $code = null , Constraint $constraint = null , $cause = null )
2010-06-24 10:24:08 +01:00
{
2019-06-17 19:33:53 +01:00
if ( null === $message ) {
@ trigger_error ( sprintf ( 'Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.' , __CLASS__ ), E_USER_DEPRECATED );
$message = '' ;
}
2019-06-28 17:16:38 +01:00
if ( null !== $code && ! \is_string ( $code )) {
@ trigger_error ( sprintf ( 'Not using a string as the error code in %s() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.' , __METHOD__ ), E_USER_DEPRECATED );
}
2019-08-08 17:57:01 +01:00
if ( ! \is_string ( $message ) && ! ( \is_object ( $message ) && method_exists ( $message , '__toString' ))) {
throw new \TypeError ( 'Constraint violation message should be a string or an object which implements the __toString() method.' );
}
2012-11-27 21:42:05 +00:00
$this -> message = $message ;
2010-09-24 15:34:58 +01:00
$this -> messageTemplate = $messageTemplate ;
2014-02-21 17:18:23 +00:00
$this -> parameters = $parameters ;
$this -> plural = $plural ;
2010-06-24 10:24:08 +01:00
$this -> root = $root ;
$this -> propertyPath = $propertyPath ;
$this -> invalidValue = $invalidValue ;
2014-08-12 14:33:06 +01:00
$this -> constraint = $constraint ;
2012-05-21 16:13:00 +01:00
$this -> code = $code ;
2014-09-26 11:44:24 +01:00
$this -> cause = $cause ;
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 .
*
2016-06-28 06:50:50 +01:00
* @ return string The violation as string
2012-02-01 12:53:45 +00:00
*/
public function __toString ()
{
2018-07-05 12:24:53 +01:00
if ( \is_object ( $this -> root )) {
$class = 'Object(' . \get_class ( $this -> root ) . ')' ;
} 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 ;
2018-11-29 16:40:35 +00:00
$code = ( string ) $this -> code ;
2012-02-01 12:53:45 +00:00
if ( '' !== $propertyPath && '[' !== $propertyPath [ 0 ] && '' !== $class ) {
$class .= '.' ;
}
2018-11-29 16:40:35 +00:00
if ( '' !== $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 }
2014-02-21 17:18:23 +00:00
*/
public function getParameters ()
{
return $this -> parameters ;
2010-09-24 15:34:58 +01:00
}
2011-04-14 10:48:15 +01:00
/**
2014-04-15 06:57:34 +01:00
* { @ inheritdoc }
2014-02-21 17:18:23 +00:00
*/
public function getPlural ()
{
return $this -> plural ;
2011-04-14 10:48:15 +01: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-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
2014-08-12 14:33:06 +01:00
/**
2014-09-26 11:44:24 +01:00
* Returns the constraint whose validation caused the violation .
2014-08-12 14:33:06 +01:00
*
* @ return Constraint | null The constraint or null if it is not known
*/
public function getConstraint ()
{
return $this -> constraint ;
}
2014-09-26 11:44:24 +01:00
/**
* Returns the cause of the violation .
*
* @ return mixed
*/
public function getCause ()
{
return $this -> cause ;
}
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
}