2010-12-02 22:04:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
|
|
|
|
use \InvalidArgumentException;
|
2010-12-03 22:36:46 +01:00
|
|
|
use \Exception;
|
2011-01-31 01:17:06 +01:00
|
|
|
use \RecursiveTreeIterator;
|
|
|
|
use Respect\Validation\ExceptionIterator;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
|
|
|
class ValidationException extends InvalidArgumentException
|
|
|
|
{
|
2011-01-31 01:17:06 +01:00
|
|
|
|
2010-12-02 22:04:10 +01:00
|
|
|
public static $defaultTemplates = array(
|
2011-01-31 01:17:06 +01:00
|
|
|
'Data validation failed: "%s"'
|
2010-12-02 22:04:10 +01:00
|
|
|
);
|
|
|
|
protected $related = array();
|
|
|
|
protected $params = array();
|
|
|
|
|
2010-12-03 22:36:46 +01:00
|
|
|
public static function create()
|
|
|
|
{
|
|
|
|
$instance = new static;
|
|
|
|
$params = func_get_args();
|
|
|
|
if (!empty($params))
|
|
|
|
$instance->configure($params);
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2010-12-03 01:23:02 +01:00
|
|
|
public function configure()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
$this->params = array_map(
|
|
|
|
function($mixed) {
|
|
|
|
return is_object($mixed) ? get_class($mixed) : strval($mixed);
|
|
|
|
}, func_get_args()
|
|
|
|
);
|
2011-01-31 01:17:06 +01:00
|
|
|
$this->message = $this->getMainMessage();
|
2010-12-02 22:04:10 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-12-03 01:23:02 +01:00
|
|
|
public function chooseTemplate()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
return array_shift(array_keys(static::$defaultTemplates));
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getFullMessage()
|
|
|
|
{
|
|
|
|
$message = array();
|
|
|
|
foreach (new RecursiveTreeIterator(new ExceptionIterator($this)) as $m)
|
|
|
|
$message[] = $m;
|
|
|
|
return implode(PHP_EOL, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMainMessage()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
$sprintfParams = $this->params;
|
2011-01-31 01:17:06 +01:00
|
|
|
array_unshift($sprintfParams, $this->getTemplate());
|
|
|
|
return call_user_func_array('sprintf', $sprintfParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelated()
|
|
|
|
{
|
|
|
|
return $this->related;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setRelated(array $relatedExceptions)
|
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
foreach ($relatedExceptions as $related)
|
2011-01-31 01:17:06 +01:00
|
|
|
$this->addRelated($related);
|
2010-12-02 22:04:10 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function addRelated(Exception $related)
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
$this->related[] = $related;
|
2010-12-03 22:36:46 +01:00
|
|
|
return $this;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getTemplate()
|
|
|
|
{
|
|
|
|
$templateKey = call_user_func_array(
|
|
|
|
array($this, 'chooseTemplate'), $this->params
|
|
|
|
);
|
|
|
|
return static::$defaultTemplates[$templateKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-01-31 01:17:06 +01:00
|
|
|
return $this->getMainMessage();
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|