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;
|
2011-02-10 21:49:48 +01:00
|
|
|
use Respect\Validation\Validatable;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
|
|
|
class ValidationException extends InvalidArgumentException
|
|
|
|
{
|
2011-02-07 02:12:41 +01:00
|
|
|
const STANDARD = 0;
|
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-18 04:08:46 +01:00
|
|
|
protected $id = 'validation';
|
|
|
|
protected $name = '';
|
2011-02-06 23:21:15 +01:00
|
|
|
protected $template = '';
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public static function format($template, array $vars=array())
|
2011-02-06 23:21:15 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
return preg_replace_callback(
|
|
|
|
'/{{(\w+)}}/',
|
|
|
|
function($match) use($vars) {
|
|
|
|
return $vars[$match[1]];
|
|
|
|
}, $template
|
|
|
|
);
|
2011-02-06 23:21:15 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public static function stringify($value)
|
|
|
|
{
|
|
|
|
if (is_string($value))
|
|
|
|
return $value;
|
|
|
|
elseif (is_object($value))
|
2011-02-18 04:08:46 +01:00
|
|
|
return static::stringifyObject($value);
|
2011-02-08 23:10:58 +01:00
|
|
|
else
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
|
2011-02-18 04:08:46 +01:00
|
|
|
public static function stringifyObject($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);
|
|
|
|
}
|
|
|
|
|
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 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-10 21:49:48 +01:00
|
|
|
public function configure($name, array $params = array())
|
2011-02-05 18:32:21 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
$this->setName($name);
|
|
|
|
$this->setParams($params);
|
2011-02-08 23:10:58 +01:00
|
|
|
$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-08 23:10:58 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
return $this->name;
|
2011-02-08 23:10:58 +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
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getMainMessage()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
$vars = $this->getParams();
|
|
|
|
$vars['name'] = $this->getName();
|
|
|
|
return static::format($this->getTemplate(), $vars);
|
2011-01-31 01:17:06 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function getParam($name)
|
2011-01-31 01:17:06 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
return $this->hasParam($name) ? $this->params[$name] : false;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function hasParam($name)
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
return isset($this->params[$name]);
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function getParams()
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
return $this->params;
|
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-02-10 21:49:48 +01:00
|
|
|
$templateKey = $this->chooseTemplate();
|
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;
|
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)
|
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
$this->name = static::stringify($name);
|
2011-02-08 23:10:58 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function setParams(array $params)
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
$this->params = array_map(array(get_called_class(), 'stringify'),
|
2011-02-18 04:08:46 +01:00
|
|
|
$params);
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|