2010-12-02 22:04:10 +01:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 22:04:10 +01:00
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
use DateTime;
|
2011-02-07 11:52:18 +01:00
|
|
|
use InvalidArgumentException;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2015-01-30 02:53:20 +01:00
|
|
|
class ValidationException extends InvalidArgumentException implements ValidationExceptionInterface
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
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(
|
2015-01-08 03:15:35 +01:00
|
|
|
self::STANDARD => 'Data validation failed for %s',
|
2011-04-10 01:34:21 +02:00
|
|
|
),
|
|
|
|
self::MODE_NEGATIVE => array(
|
2015-01-08 03:15:35 +01:00
|
|
|
self::STANDARD => 'Data validation failed for %s',
|
|
|
|
),
|
2010-12-02 22:04:10 +01:00
|
|
|
);
|
2015-08-08 20:36:31 +02:00
|
|
|
/**
|
|
|
|
* @var int Maximum depth when stringifying nested arrays
|
|
|
|
*/
|
|
|
|
private static $maxDepthStringify = 3;
|
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
|
|
|
|
2015-01-08 03:15:35 +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+)}}/',
|
2015-01-08 03:15:35 +01:00
|
|
|
function ($match) use ($vars) {
|
2011-02-20 19:03:09 +01:00
|
|
|
return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
|
2013-01-14 22:55:03 +01:00
|
|
|
},
|
|
|
|
$template
|
2011-02-10 21:49:48 +01:00
|
|
|
);
|
2011-02-06 23:21:15 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
public static function stringify($value)
|
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (is_string($value)) {
|
2011-02-08 23:10:58 +01:00
|
|
|
return $value;
|
2013-01-14 22:55:03 +01:00
|
|
|
} elseif (is_array($value)) {
|
2015-08-08 20:36:31 +02:00
|
|
|
return self::stringifyArray($value);
|
2013-01-14 22:55:03 +01:00
|
|
|
} elseif (is_object($value)) {
|
2011-02-18 04:08:46 +01:00
|
|
|
return static::stringifyObject($value);
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2011-02-08 23:10:58 +01:00
|
|
|
return (string) $value;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-02-08 23:10:58 +01:00
|
|
|
}
|
|
|
|
|
2015-08-08 20:36:31 +02:00
|
|
|
/**
|
|
|
|
* @param array $value
|
|
|
|
* @param int $depth
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function stringifyArray($value, $depth = 0)
|
|
|
|
{
|
|
|
|
$items = array();
|
|
|
|
foreach ($value as $val) {
|
|
|
|
if (is_object($val)) {
|
|
|
|
$items[] = self::stringifyObject($val);
|
|
|
|
} elseif (is_array($val)) {
|
|
|
|
if ($depth >= self::$maxDepthStringify) {
|
|
|
|
$items[] = '...';
|
|
|
|
} else {
|
|
|
|
$items[] = '('.self::stringifyArray($val, $depth + 1).')';
|
|
|
|
}
|
|
|
|
} elseif (is_string($val)) {
|
|
|
|
$items[] = "'$val'";
|
|
|
|
} else {
|
|
|
|
$items[] = (string) $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $items);
|
|
|
|
}
|
|
|
|
|
2011-02-18 04:08:46 +01:00
|
|
|
public static function stringifyObject($value)
|
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (method_exists($value, '__toString')) {
|
2011-02-18 04:08:46 +01:00
|
|
|
return (string) $value;
|
2013-01-14 22:55:03 +01:00
|
|
|
} elseif ($value instanceof DateTime) {
|
2011-02-18 04:08:46 +01:00
|
|
|
return $value->format('Y-m-d H:i:s');
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2015-06-08 16:47:14 +02:00
|
|
|
return 'Object of class '.get_class($value);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
}
|
|
|
|
|
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());
|
2013-01-14 22:55:03 +01:00
|
|
|
|
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();
|
2013-01-14 22:55:03 +01:00
|
|
|
if (isset($vars['translator']) && is_callable($vars['translator'])) {
|
2011-12-27 03:43:22 +01:00
|
|
|
$template = call_user_func($vars['translator'], $template);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-27 03:43:22 +01:00
|
|
|
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()
|
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (!empty($this->template)) {
|
2011-02-06 23:21:15 +01:00
|
|
|
return $this->template;
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2011-02-20 19:03:09 +01:00
|
|
|
return $this->template = $this->buildTemplate();
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
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;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-02-07 11:52:18 +01:00
|
|
|
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);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
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();
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-04-10 01:34:21 +02:00
|
|
|
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);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
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
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
foreach ($params as $key => $value) {
|
2011-02-20 19:03:09 +01:00
|
|
|
$this->setParam($key, $value);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
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();
|
2013-01-14 22:55:03 +01:00
|
|
|
|
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()
|
|
|
|
{
|
2015-01-08 03:15:35 +01:00
|
|
|
if (!empty($this->id) && $this->id != 'validation') {
|
2011-04-12 20:26:03 +02:00
|
|
|
return $this->id;
|
2015-01-08 03:15:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$pieces = explode('\\', get_called_class());
|
|
|
|
$exceptionClassShortName = end($pieces);
|
|
|
|
$ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName);
|
|
|
|
$ruleName = lcfirst($ruleClassShortName);
|
|
|
|
|
|
|
|
return $ruleName;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|