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

186 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace Respect\Validation\Exceptions;
use DateTime;
use Exception;
use InvalidArgumentException;
use Respect\Validation\Validatable;
class ValidationException extends InvalidArgumentException
{
2011-02-07 02:12:41 +01:00
const STANDARD = 0;
public static $defaultTemplates = array(
self::STANDARD => 'Data validation failed for %s'
);
protected $id = 'validation';
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_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);
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();
return static::format($this->getTemplate(), $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;
}
2011-02-20 19:03:09 +01:00
public function setParam($key, $value)
{
$this->params[$key] = static::stringify($value);
}
public function setParams(array $params)
{
2011-02-20 19:03:09 +01:00
foreach ($params as $key => $value)
$this->setParam($key, $value);
}
public function setTemplate($template)
{
$this->template = $template;
}
protected function buildTemplate()
{
$templateKey = $this->chooseTemplate();
return static::$defaultTemplates[$templateKey];
}
protected function guessId()
{
if (!empty($this->id) && $this->id != 'validation')
return;
$id = end(explode('\\', get_called_class()));
$id = lcfirst(str_replace('Exception', '', $id));
return $id;
}
}
2011-03-13 15:50:47 +01:00
/**
* LICENSE
*
* Copyright (c) 2009-2011, Alexandre Gomes Gaigalas.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/