2010-12-02 22:04:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
use DateTime;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use RecursiveTreeIterator;
|
2011-01-31 01:17:06 +01:00
|
|
|
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-07 02:12:41 +01:00
|
|
|
const STANDARD = 0;
|
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-02-08 23:10:58 +01:00
|
|
|
self::STANDARD => 'Data validation failed for %s'
|
2010-12-02 22:04:10 +01:00
|
|
|
);
|
2011-02-07 18:21:58 +01:00
|
|
|
protected $context = null;
|
2011-02-06 23:21:15 +01:00
|
|
|
protected $id = '';
|
2011-02-07 11:52:18 +01:00
|
|
|
protected $params = array();
|
|
|
|
protected $related = array();
|
2011-02-06 23:21:15 +01:00
|
|
|
protected $template = '';
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public static function create($name=null)
|
2011-02-06 23:21:15 +01:00
|
|
|
{
|
|
|
|
$i = new static;
|
|
|
|
if (func_get_args() > 0)
|
|
|
|
return call_user_func_array(array($i, 'configure'), func_get_args());
|
|
|
|
else
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public static function stringify($value)
|
|
|
|
{
|
|
|
|
if (is_string($value))
|
|
|
|
return $value;
|
|
|
|
elseif (is_object($value))
|
|
|
|
if (method_exists($value, '__toString'))
|
|
|
|
return (string) $value;
|
|
|
|
elseif ($value instanceof DateTime)
|
|
|
|
return $value->format('Y-m-d H:i:s');
|
|
|
|
else
|
|
|
|
return "Object of class " . get_class($value);
|
|
|
|
else
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function __toString()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
return $this->getMainMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addRelated(ValidationException $related)
|
|
|
|
{
|
|
|
|
$this->related[] = $related;
|
2010-12-02 22:04:10 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function chooseTemplate()
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
return key(static::$defaultTemplates);
|
2011-02-05 18:32:21 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public function configure($name)
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
$this->params = func_get_args();
|
2011-02-08 23:10:58 +01:00
|
|
|
foreach ($this->params as &$p)
|
|
|
|
$p = static::stringify($p);
|
|
|
|
$this->message = $this->getMainMessage();
|
2011-02-07 11:52:18 +01:00
|
|
|
$this->guessId();
|
|
|
|
return $this;
|
2011-02-05 18:32:21 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function findRelated()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
$target = $this;
|
|
|
|
$path = func_get_args();
|
|
|
|
while (!empty($path) && $target !== false)
|
|
|
|
$target = $this->getRelatedByName(array_shift($path));
|
|
|
|
return $target;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getFullMessage()
|
|
|
|
{
|
|
|
|
$message = array();
|
2011-02-06 23:21:15 +01:00
|
|
|
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
|
2011-01-31 01:17:06 +01:00
|
|
|
$message[] = $m;
|
|
|
|
return implode(PHP_EOL, $message);
|
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->params[0];
|
|
|
|
}
|
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
public function getId()
|
2011-02-02 03:28:02 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
return $this->id;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
public function getIterator($full=false, $mode=self::ITERATE_ALL)
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
|
|
|
$exceptionIterator = new ExceptionIterator($this, $full);
|
|
|
|
if ($mode == self::ITERATE_ALL)
|
|
|
|
return new RecursiveIteratorIterator($exceptionIterator, 1);
|
|
|
|
else
|
|
|
|
return new RecursiveTreeIterator($exceptionIterator);
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getMainMessage()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-08 23:10:58 +01:00
|
|
|
$sprintfParams = $this->getParams();
|
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);
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function getParams()
|
2011-01-31 01:17:06 +01:00
|
|
|
{
|
2011-02-08 23:10:58 +01:00
|
|
|
$params = array_slice($this->params, 1);
|
|
|
|
array_unshift($params, $this->getName());
|
|
|
|
return $params;
|
|
|
|
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function getRelated()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
return $this->related;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function getRelatedByName($name)
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
foreach ($this->getIterator(true) as $e)
|
|
|
|
if ($e->getId() === $name)
|
|
|
|
return $e;
|
|
|
|
return false;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getTemplate()
|
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
if (!empty($this->template))
|
|
|
|
return $this->template;
|
2011-01-31 01:17:06 +01:00
|
|
|
$templateKey = call_user_func_array(
|
2011-02-08 23:10:58 +01:00
|
|
|
array($this, 'chooseTemplate'), $this->getParams()
|
2011-01-31 01:17:06 +01:00
|
|
|
);
|
2011-02-07 18:21:58 +01:00
|
|
|
if (is_null($this->context))
|
|
|
|
$this->template = static::$defaultTemplates[$templateKey];
|
|
|
|
else
|
|
|
|
$this->template = $this->context->getTemplate($this, $templateKey);
|
|
|
|
return $this->template;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContext($context)
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
foreach ($this->related as $r)
|
|
|
|
$r->setContext($context);
|
2011-01-31 01:17:06 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function setId($id)
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-07 11:52:18 +01:00
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->params[0] = static::stringify($name);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
public function setRelated(array $relatedExceptions)
|
|
|
|
{
|
|
|
|
foreach ($relatedExceptions as $related)
|
|
|
|
$this->addRelated($related);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function guessId()
|
|
|
|
{
|
|
|
|
if (!empty($this->id))
|
|
|
|
return;
|
|
|
|
$id = end(explode('\\', get_called_class()));
|
|
|
|
$id = lcfirst(str_replace('Exception', '', $id));
|
|
|
|
$this->setId($id);
|
|
|
|
}
|
|
|
|
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|