respect-validation/library/Respect/Validation/Exceptions/ValidationException.php

178 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Respect\Validation\Exceptions;
use DateTime;
use Exception;
use InvalidArgumentException;
class ValidationException extends InvalidArgumentException
{
const MODE_DEFAULT = 1;
const MODE_NEGATIVE = 2;
2011-02-07 02:12:41 +01:00
const STANDARD = 0;
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => 'Data validation failed for %s'
),
self::MODE_NEGATIVE => array(
self::STANDARD => 'Data validation failed for %s'
)
);
protected $id = 'validation';
protected $mode = self::MODE_DEFAULT;
protected $name = '';
protected $template = '';
protected $params = array();
public static function format($template, array $vars=array())
{
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];
}, $template
);
}
public static function stringify($value)
{
if (is_string($value))
return $value;
elseif (is_array($value))
return 'Array'; //FIXME
elseif (is_object($value))
return static::stringifyObject($value);
else
return (string) $value;
}
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);
}
public function __toString()
{
return $this->getMainMessage();
}
public function chooseTemplate()
2011-02-05 18:32:21 +01:00
{
return key(static::$defaultTemplates[$this->mode]);
2011-02-05 18:32:21 +01:00
}
public function configure($name, array $params = array())
2011-02-05 18:32:21 +01:00
{
$this->setName($name);
$this->setParams($params);
$this->message = $this->getMainMessage();
$this->setId($this->guessId());
return $this;
2011-02-05 18:32:21 +01:00
}
public function getName()
{
return $this->name;
}
public function getId()
{
return $this->id;
}
public function getMainMessage()
{
$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);
}
public function getParam($name)
{
return $this->hasParam($name) ? $this->params[$name] : false;
}
public function getParams()
{
return $this->params;
}
public function getTemplate()
{
if (!empty($this->template))
return $this->template;
else
2011-02-20 19:03:09 +01:00
return $this->template = $this->buildTemplate();
}
public function hasParam($name)
{
return isset($this->params[$name]);
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function setName($name)
{
$this->name = static::stringify($name);
return $this;
}
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);
return $this;
2011-02-20 19:03:09 +01:00
}
public function setParams(array $params)
{
2011-02-20 19:03:09 +01:00
foreach ($params as $key => $value)
$this->setParam($key, $value);
return $this;
}
public function setTemplate($template)
{
$this->template = $template;
return $this;
}
protected function buildTemplate()
{
$templateKey = $this->chooseTemplate();
return static::$defaultTemplates[$this->mode][$templateKey];
}
protected function guessId()
{
if (!empty($this->id) && $this->id != 'validation')
return $this->id;
2011-04-15 00:32:48 +02:00
$classParts = explode('\\', get_called_class());
$id = end($classParts);
$id = lcfirst(str_replace('Exception', '', $id));
return $id;
}
}
2011-03-13 15:50:47 +01:00