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
*/
2014-02-21 17:18:23 +00:00
private $parameters ;
2012-11-22 14:58:46 +00:00
/**
2014-04-16 11:30:19 +01:00
* @ var int | null
2012-11-22 14:58:46 +00:00
*/
2014-02-21 17:18:23 +00:00
private $plural ;
2012-11-22 14:58:46 +00:00
/**
* @ var mixed
*/
private $root ;
/**
* @ var string
*/
private $propertyPath ;
/**
* @ var mixed
*/
private $invalidValue ;
2014-08-12 14:33:06 +01:00
/**
* @ var Constraint | null
*/
private $constraint ;
2012-11-22 14:58:46 +00:00
/**
* @ var mixed
*/
private $code ;
2010-06-24 09:40:05 +01:00
2014-09-26 11:44:24 +01:00
/**
* @ var mixed
*/
private $cause ;
2012-11-22 14:58:46 +00:00
/**
* Creates a new constraint violation .
*
2014-08-12 14:33:06 +01:00
* @ param string $message The violation message
* @ param string $messageTemplate The raw violation message
* @ param array $parameters The parameters to substitute in the
* raw violation 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 that caused this
* violation
* @ param int | null $plural The number for determining the plural
* form when translating the message
* @ param mixed $code The error code of the violation
2014-09-26 11:44:24 +01:00
* @ param Constraint | null $constraint The constraint whose validation
* caused the violation
* @ param mixed $cause The cause of the violation
2014-08-12 14:33:06 +01:00
*/
2014-09-26 11:44:24 +01:00
public function __construct ( $message , $messageTemplate , array $parameters , $root , $propertyPath , $invalidValue , $plural = null , $code = null , Constraint $constraint = null , $cause = 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 ;
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 .
*
* @ 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 }
2014-11-29 13:13:33 +00:00
*
* @ deprecated Deprecated since version 2.7 , to be removed in 3.0 .
* Use getParameters () instead
2011-01-16 04:41:23 +00:00
*/
2010-09-24 15:34:58 +01:00
public function getMessageParameters ()
{
2014-11-29 13:13:33 +00:00
trigger_error ( 'ConstraintViolation::getMessageParameters() was deprecated since version 2.7, to be removed in 3.0. Use ConstraintViolation::getParameters() instead.' , E_USER_DEPRECATED );
2014-02-21 17:18:23 +00:00
return $this -> parameters ;
}
/**
* Alias of { @ link getMessageParameters ()} .
*/
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-11-29 13:13:33 +00:00
*
* @ deprecated Deprecated since version 2.7 , to be removed in 3.0 .
* Use getPlural () instead
2011-04-14 10:48:15 +01:00
*/
public function getMessagePluralization ()
{
2014-11-29 13:13:33 +00:00
trigger_error ( 'ConstraintViolation::getMessagePluralization() was deprecated since version 2.7, to be removed in 3.0. Use ConstraintViolation::getPlural() instead.' , E_USER_DEPRECATED );
2014-02-21 17:18:23 +00:00
return $this -> plural ;
}
/**
* Alias of { @ link getMessagePluralization ()} .
*/
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
}