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-02-02 03:28:02 +01:00
|
|
|
use \RecursiveIteratorIterator;
|
2011-01-31 01:17:06 +01:00
|
|
|
use \RecursiveTreeIterator;
|
|
|
|
use Respect\Validation\ExceptionIterator;
|
2011-02-02 03:28:02 +01:00
|
|
|
use Respect\Validation\Reportable;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
|
|
|
class ValidationException extends InvalidArgumentException
|
|
|
|
{
|
2011-02-05 18:32:21 +01:00
|
|
|
const ITERATE_TREE = 1;
|
|
|
|
const ITERATE_ALL = 2;
|
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();
|
2011-02-02 03:28:02 +01:00
|
|
|
protected $name = '';
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2010-12-03 22:36:46 +01:00
|
|
|
public static function create()
|
|
|
|
{
|
|
|
|
$instance = new static;
|
2011-02-05 18:32:21 +01:00
|
|
|
return func_num_args() > 0 ? $instance : $instance->configure(func_get_args());
|
2010-12-03 22:36:46 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 01:23:02 +01:00
|
|
|
public function configure()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-01-31 01:17:06 +01:00
|
|
|
$this->message = $this->getMainMessage();
|
2011-02-05 18:32:21 +01:00
|
|
|
$this->params = func_get_args();
|
|
|
|
$this->stringifyParams();
|
|
|
|
$this->guessName();
|
2010-12-02 22:04:10 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:32:21 +01:00
|
|
|
protected function guessName()
|
|
|
|
{
|
|
|
|
if (!empty($this->name))
|
|
|
|
return;
|
|
|
|
$name = end(explode('\\', get_called_class()));
|
|
|
|
$name = lcfirst(str_replace('Exception', '', $name));
|
|
|
|
$this->setName($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function stringifyParams()
|
|
|
|
{
|
|
|
|
foreach ($this->params as &$param)
|
|
|
|
if (!is_object($param) || method_exists($param, '__toString'))
|
|
|
|
$param = (string) $param;
|
|
|
|
else
|
|
|
|
$param = get_class($param);
|
|
|
|
}
|
|
|
|
|
2010-12-03 01:23:02 +01:00
|
|
|
public function chooseTemplate()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-05 18:32:21 +01:00
|
|
|
return key(static::$defaultTemplates);
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getFullMessage()
|
|
|
|
{
|
|
|
|
$message = array();
|
2011-02-05 18:32:21 +01:00
|
|
|
foreach ($this->iterate(false, self::ITERATE_TREE) as $m)
|
2011-01-31 01:17:06 +01:00
|
|
|
$message[] = $m;
|
|
|
|
return implode(PHP_EOL, $message);
|
|
|
|
}
|
|
|
|
|
2011-02-02 03:28:02 +01:00
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelatedByName($name)
|
|
|
|
{
|
2011-02-05 18:32:21 +01:00
|
|
|
foreach ($this->iterate(true) as $e)
|
2011-02-02 03:28:02 +01:00
|
|
|
if ($e->getName() === $name)
|
|
|
|
return $e;
|
2011-02-03 23:16:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:32:21 +01:00
|
|
|
public function iterate($full=false, $mode=self::ITERATE_ALL)
|
|
|
|
{
|
|
|
|
$exceptionIterator = new ExceptionIterator($this, $full);
|
|
|
|
if ($mode == self::ITERATE_ALL)
|
|
|
|
return new RecursiveIteratorIterator($exceptionIterator, 1);
|
|
|
|
else
|
|
|
|
return new RecursiveTreeIterator($exceptionIterator);
|
|
|
|
}
|
|
|
|
|
2011-02-03 23:16:58 +01:00
|
|
|
public function findRelated()
|
|
|
|
{
|
|
|
|
$target = $this;
|
|
|
|
$path = func_get_args();
|
|
|
|
while (!empty($path) && $target !== false)
|
|
|
|
$target = $this->getRelatedByName(array_shift($path));
|
|
|
|
return $target;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getMainMessage()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2010-12-03 01:23:02 +01:00
|
|
|
$sprintfParams = $this->params;
|
2011-02-02 03:28:02 +01:00
|
|
|
if (empty($sprintfParams))
|
|
|
|
return $this->message;
|
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-02-02 03:28:02 +01:00
|
|
|
public function addRelated(ValidationException $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
|
|
|
}
|
|
|
|
|
|
|
|
}
|