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;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
|
|
|
class ValidationException extends InvalidArgumentException
|
|
|
|
{
|
2011-04-10 01:34:21 +02:00
|
|
|
const MODE_DEFAULT = 1;
|
|
|
|
const MODE_NEGATIVE = 2;
|
2011-02-07 02:12:41 +01:00
|
|
|
const STANDARD = 0;
|
2010-12-02 22:04:10 +01:00
|
|
|
public static $defaultTemplates = array(
|
2011-04-10 01:34:21 +02:00
|
|
|
self::MODE_DEFAULT => array(
|
|
|
|
self::STANDARD => 'Data validation failed for %s'
|
|
|
|
),
|
|
|
|
self::MODE_NEGATIVE => array(
|
|
|
|
self::STANDARD => 'Data validation failed for %s'
|
|
|
|
)
|
2010-12-02 22:04:10 +01:00
|
|
|
);
|
2011-02-18 04:08:46 +01:00
|
|
|
protected $id = 'validation';
|
2011-04-10 01:34:21 +02:00
|
|
|
protected $mode = self::MODE_DEFAULT;
|
2011-02-18 04:08:46 +01:00
|
|
|
protected $name = '';
|
2011-02-06 23:21:15 +01:00
|
|
|
protected $template = '';
|
2011-03-18 20:08:04 +01:00
|
|
|
protected $params = array();
|
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) {
|
2011-02-20 19:03:09 +01:00
|
|
|
return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
|
2011-02-10 21:49:48 +01:00
|
|
|
}, $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;
|
2011-11-25 21:56:05 +01:00
|
|
|
elseif (is_array($value))
|
|
|
|
return 'Array'; //FIXME
|
2011-02-08 23:10:58 +01:00
|
|
|
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-04-10 01:34:21 +02:00
|
|
|
return key(static::$defaultTemplates[$this->mode]);
|
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-20 17:59:53 +01:00
|
|
|
$this->setId($this->guessId());
|
2011-02-07 11:52:18 +01:00
|
|
|
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();
|
2011-12-27 03:43:22 +01:00
|
|
|
$template = $this->getTemplate();
|
|
|
|
if(isset($vars['translator']) && is_callable($vars['translator']))
|
|
|
|
$template = call_user_func($vars['translator'], $template);
|
|
|
|
return static::format($template, $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 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-07 18:21:58 +01:00
|
|
|
else
|
2011-02-20 19:03:09 +01:00
|
|
|
return $this->template = $this->buildTemplate();
|
2011-02-20 17:59:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasParam($name)
|
|
|
|
{
|
|
|
|
return isset($this->params[$name]);
|
2011-02-07 18:21:58 +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-04-10 01:34:21 +02:00
|
|
|
public function setMode($mode)
|
|
|
|
{
|
|
|
|
$this->mode = $mode;
|
|
|
|
$this->template = $this->buildTemplate();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-20 19:03:09 +01:00
|
|
|
public function setParam($key, $value)
|
|
|
|
{
|
2011-12-27 03:43:22 +01:00
|
|
|
$this->params[$key] = ($key == 'translator') ? $value : static::stringify($value);
|
2011-04-10 01:34:21 +02:00
|
|
|
return $this;
|
2011-02-20 19:03:09 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function setParams(array $params)
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2011-02-20 19:03:09 +01:00
|
|
|
foreach ($params as $key => $value)
|
|
|
|
$this->setParam($key, $value);
|
2011-04-10 01:34:21 +02:00
|
|
|
return $this;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->template = $template;
|
2011-04-10 01:34:21 +02:00
|
|
|
return $this;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
protected function buildTemplate()
|
|
|
|
{
|
|
|
|
$templateKey = $this->chooseTemplate();
|
2011-04-10 01:34:21 +02:00
|
|
|
return static::$defaultTemplates[$this->mode][$templateKey];
|
2011-02-20 17:59:53 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
protected function guessId()
|
|
|
|
{
|
2011-03-31 00:06:59 +02:00
|
|
|
if (!empty($this->id) && $this->id != 'validation')
|
2011-04-12 20:26:03 +02:00
|
|
|
return $this->id;
|
2011-04-15 00:32:48 +02:00
|
|
|
$classParts = explode('\\', get_called_class());
|
|
|
|
$id = end($classParts);
|
2011-02-07 11:52:18 +01:00
|
|
|
$id = lcfirst(str_replace('Exception', '', $id));
|
2011-02-20 17:59:53 +01:00
|
|
|
return $id;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|
2011-03-13 15:50:47 +01:00
|
|
|
|