2010-11-18 22:33:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-11-18 22:33:44 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-11-18 22:33:44 +00: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-11-18 22:33:44 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
2014-01-10 10:44:16 +00:00
|
|
|
use Symfony\Component\Form\Exception\BadMethodCallException;
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Wraps errors in forms.
|
2010-11-18 22:33:44 +00:00
|
|
|
*
|
2012-05-26 08:48:33 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2010-11-18 22:33:44 +00:00
|
|
|
*/
|
2014-01-10 10:44:16 +00:00
|
|
|
class FormError implements \Serializable
|
2010-11-18 22:33:44 +00:00
|
|
|
{
|
2012-11-27 22:17:30 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $message;
|
|
|
|
|
2011-03-21 20:01:14 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The template for the error message.
|
|
|
|
*
|
2011-03-21 20:01:14 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $messageTemplate;
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The parameters that should be substituted in the message template.
|
|
|
|
*
|
2011-03-21 20:01:14 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $messageParameters;
|
|
|
|
|
2012-02-02 12:36:42 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The value for error message pluralization.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var int|null
|
2012-02-02 12:36:42 +00:00
|
|
|
*/
|
|
|
|
protected $messagePluralization;
|
|
|
|
|
2014-01-10 10:44:16 +00:00
|
|
|
/**
|
2014-12-22 16:29:52 +00:00
|
|
|
* The cause for this error.
|
|
|
|
*
|
2014-01-10 10:44:16 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $cause;
|
|
|
|
|
|
|
|
/**
|
2014-12-22 16:29:52 +00:00
|
|
|
* The form that spawned this error.
|
|
|
|
*
|
2014-01-10 10:44:16 +00:00
|
|
|
* @var FormInterface
|
|
|
|
*/
|
|
|
|
private $origin;
|
|
|
|
|
2011-03-21 20:01:14 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Constructor.
|
2011-03-21 20:01:14 +00:00
|
|
|
*
|
2011-07-09 15:14:57 +01:00
|
|
|
* Any array key in $messageParameters will be used as a placeholder in
|
|
|
|
* $messageTemplate.
|
|
|
|
*
|
2014-12-04 20:26:11 +00:00
|
|
|
* @param string $message The translated error message
|
|
|
|
* @param string|null $messageTemplate The template for the error message
|
|
|
|
* @param array $messageParameters The parameters that should be
|
|
|
|
* substituted in the message template
|
|
|
|
* @param int|null $messagePluralization The value for error message pluralization
|
|
|
|
* @param mixed $cause The cause of the error
|
2012-11-27 22:17:30 +00:00
|
|
|
*
|
|
|
|
* @see \Symfony\Component\Translation\Translator
|
2011-03-21 20:01:14 +00:00
|
|
|
*/
|
2014-01-10 10:44:16 +00:00
|
|
|
public function __construct($message, $messageTemplate = null, array $messageParameters = array(), $messagePluralization = null, $cause = null)
|
2011-03-21 20:01:14 +00:00
|
|
|
{
|
2012-11-27 22:17:30 +00:00
|
|
|
$this->message = $message;
|
|
|
|
$this->messageTemplate = $messageTemplate ?: $message;
|
2011-03-21 20:01:14 +00:00
|
|
|
$this->messageParameters = $messageParameters;
|
2012-02-02 12:36:42 +00:00
|
|
|
$this->messagePluralization = $messagePluralization;
|
2014-01-10 10:44:16 +00:00
|
|
|
$this->cause = $cause;
|
2011-03-21 20:01:14 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 17:26:32 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Returns the error message.
|
2011-11-29 17:26:32 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMessage()
|
|
|
|
{
|
2012-11-27 22:17:30 +00:00
|
|
|
return $this->message;
|
2011-11-29 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 20:01:14 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Returns the error message template.
|
2011-03-21 20:01:14 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMessageTemplate()
|
|
|
|
{
|
|
|
|
return $this->messageTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Returns the parameters to be inserted in the message template.
|
2011-03-21 20:01:14 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMessageParameters()
|
|
|
|
{
|
|
|
|
return $this->messageParameters;
|
|
|
|
}
|
2012-02-02 12:36:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value for error message pluralization.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return int|null
|
2012-02-02 12:36:42 +00:00
|
|
|
*/
|
|
|
|
public function getMessagePluralization()
|
|
|
|
{
|
|
|
|
return $this->messagePluralization;
|
|
|
|
}
|
2014-01-10 10:44:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cause of this error.
|
|
|
|
*
|
|
|
|
* @return mixed The cause of this error
|
|
|
|
*/
|
|
|
|
public function getCause()
|
|
|
|
{
|
|
|
|
return $this->cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the form that caused this error.
|
|
|
|
*
|
|
|
|
* This method must only be called once.
|
|
|
|
*
|
|
|
|
* @param FormInterface $origin The form that caused this error
|
|
|
|
*
|
|
|
|
* @throws BadMethodCallException If the method is called more than once
|
|
|
|
*/
|
|
|
|
public function setOrigin(FormInterface $origin)
|
|
|
|
{
|
|
|
|
if (null !== $this->origin) {
|
|
|
|
throw new BadMethodCallException('setOrigin() must only be called once.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->origin = $origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the form that caused this error.
|
|
|
|
*
|
|
|
|
* @return FormInterface The form that caused this error
|
|
|
|
*/
|
|
|
|
public function getOrigin()
|
|
|
|
{
|
|
|
|
return $this->origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes this error.
|
|
|
|
*
|
|
|
|
* @return string The serialized error
|
|
|
|
*/
|
|
|
|
public function serialize()
|
|
|
|
{
|
|
|
|
return serialize(array(
|
|
|
|
$this->message,
|
|
|
|
$this->messageTemplate,
|
|
|
|
$this->messageParameters,
|
|
|
|
$this->messagePluralization,
|
2014-09-22 10:14:18 +01:00
|
|
|
$this->cause,
|
2014-01-10 10:44:16 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializes a serialized error.
|
|
|
|
*
|
|
|
|
* @param string $serialized The serialized error
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
|
|
|
list($this->message, $this->messageTemplate, $this->messageParameters, $this->messagePluralization, $this->cause) = unserialize($serialized);
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|