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-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-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-06 23:21:15 +01:00
|
|
|
protected $id = '';
|
|
|
|
protected $template = '';
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
public function getParams()
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
return $this->params;
|
2010-12-03 22:36:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function create($input=null)
|
|
|
|
{
|
|
|
|
$i = new static;
|
|
|
|
if (func_get_args() > 0)
|
|
|
|
return call_user_func_array(array($i, 'configure'), func_get_args());
|
|
|
|
else
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configure($input=null)
|
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();
|
2011-02-06 23:21:15 +01:00
|
|
|
$this->stringifyInput();
|
|
|
|
$this->guessId();
|
2010-12-02 22:04:10 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
protected function guessId()
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
if (!empty($this->id))
|
2011-02-05 18:32:21 +01:00
|
|
|
return;
|
2011-02-06 23:21:15 +01:00
|
|
|
$id = end(explode('\\', get_called_class()));
|
|
|
|
$id = lcfirst(str_replace('Exception', '', $id));
|
|
|
|
$this->setId($id);
|
2011-02-05 18:32:21 +01:00
|
|
|
}
|
|
|
|
|
2011-02-06 23:21:15 +01:00
|
|
|
protected function stringifyInput()
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
$param = &$this->params[0];
|
|
|
|
if (!is_object($param) || method_exists($param, '__toString'))
|
|
|
|
$param = (string) $param;
|
|
|
|
else
|
|
|
|
$param = get_class($param);
|
2011-02-05 18:32:21 +01:00
|
|
|
}
|
|
|
|
|
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-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-06 23:21:15 +01:00
|
|
|
public function setId($id)
|
2011-02-02 03:28:02 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelatedByName($name)
|
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
foreach ($this->getIterator(true) as $e)
|
|
|
|
if ($e->getId() === $name)
|
2011-02-02 03:28:02 +01:00
|
|
|
return $e;
|
2011-02-03 23:16:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-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()
|
|
|
|
{
|
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(
|
|
|
|
array($this, 'chooseTemplate'), $this->params
|
|
|
|
);
|
2011-02-06 23:21:15 +01:00
|
|
|
return $this->template = static::$defaultTemplates[$templateKey];
|
2011-01-31 01:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|