Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator; Chris@0: Chris@0: /** Chris@0: * Default implementation of {@ConstraintViolationInterface}. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class ConstraintViolation implements ConstraintViolationInterface Chris@0: { Chris@0: private $message; Chris@0: private $messageTemplate; Chris@0: private $parameters; Chris@0: private $plural; Chris@0: private $root; Chris@0: private $propertyPath; Chris@0: private $invalidValue; Chris@0: private $constraint; Chris@0: private $code; Chris@0: private $cause; Chris@0: Chris@0: /** Chris@0: * Creates a new constraint violation. Chris@0: * Chris@0: * @param string $message The violation message Chris@0: * @param string $messageTemplate The raw violation message Chris@0: * @param array $parameters The parameters to substitute in the Chris@0: * raw violation message Chris@0: * @param mixed $root The value originally passed to the Chris@0: * validator Chris@0: * @param string $propertyPath The property path from the root Chris@0: * value to the invalid value Chris@0: * @param mixed $invalidValue The invalid value that caused this Chris@0: * violation Chris@0: * @param int|null $plural The number for determining the plural Chris@0: * form when translating the message Chris@0: * @param mixed $code The error code of the violation Chris@0: * @param Constraint|null $constraint The constraint whose validation Chris@0: * caused the violation Chris@0: * @param mixed $cause The cause of the violation Chris@0: */ Chris@0: public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null) Chris@0: { Chris@0: $this->message = $message; Chris@0: $this->messageTemplate = $messageTemplate; Chris@0: $this->parameters = $parameters; Chris@0: $this->plural = $plural; Chris@0: $this->root = $root; Chris@0: $this->propertyPath = $propertyPath; Chris@0: $this->invalidValue = $invalidValue; Chris@0: $this->constraint = $constraint; Chris@0: $this->code = $code; Chris@0: $this->cause = $cause; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts the violation into a string for debugging purposes. Chris@0: * Chris@0: * @return string The violation as string Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@17: if (\is_object($this->root)) { Chris@17: $class = 'Object('.\get_class($this->root).')'; Chris@17: } elseif (\is_array($this->root)) { Chris@0: $class = 'Array'; Chris@0: } else { Chris@0: $class = (string) $this->root; Chris@0: } Chris@0: Chris@0: $propertyPath = (string) $this->propertyPath; Chris@17: $code = (string) $this->code; Chris@0: Chris@0: if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) { Chris@0: $class .= '.'; Chris@0: } Chris@0: Chris@17: if ('' !== $code) { Chris@0: $code = ' (code '.$code.')'; Chris@0: } Chris@0: Chris@0: return $class.$propertyPath.":\n ".$this->getMessage().$code; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMessageTemplate() Chris@0: { Chris@0: return $this->messageTemplate; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getParameters() Chris@0: { Chris@0: return $this->parameters; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPlural() Chris@0: { Chris@0: return $this->plural; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMessage() Chris@0: { Chris@0: return $this->message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRoot() Chris@0: { Chris@0: return $this->root; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyPath() Chris@0: { Chris@0: return $this->propertyPath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getInvalidValue() Chris@0: { Chris@0: return $this->invalidValue; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the constraint whose validation caused the violation. Chris@0: * Chris@0: * @return Constraint|null The constraint or null if it is not known Chris@0: */ Chris@0: public function getConstraint() Chris@0: { Chris@0: return $this->constraint; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the cause of the violation. Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function getCause() Chris@0: { Chris@0: return $this->cause; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCode() Chris@0: { Chris@0: return $this->code; Chris@0: } Chris@0: }