Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions library/Exceptions/AgeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class AgeException extends AbstractNestedException
),
);

public function configure($name, array $params = array())
{
$params['minAge'] = static::stringify($params['minAge']);
$params['maxAge'] = static::stringify($params['maxAge']);

return parent::configure($name, $params);
}

public function chooseTemplate()
{
if (!$this->getParam('minAge')) {
Expand Down
4 changes: 2 additions & 2 deletions library/Exceptions/AlnumException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class AlnumException extends AlphaException
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and "{{additionalChars}}"',
self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and {{additionalChars}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)',
self::EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or "{{additionalChars}}"',
self::EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or {{additionalChars}}',
),
);
}
8 changes: 0 additions & 8 deletions library/Exceptions/BetweenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class BetweenException extends AbstractNestedException
),
);

public function configure($name, array $params = array())
{
$params['minValue'] = static::stringify($params['minValue']);
$params['maxValue'] = static::stringify($params['maxValue']);

return parent::configure($name, $params);
}

public function chooseTemplate()
{
if (!$this->getParam('minValue')) {
Expand Down
4 changes: 2 additions & 2 deletions library/Exceptions/ExtensionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class ExtensionException extends ValidationException
*/
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must have "{{extension}}" extension',
self::STANDARD => '{{name}} must have {{extension}} extension',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not have "{{extension}}" extension',
self::STANDARD => '{{name}} must not have {{extension}} extension',
),
);
}
4 changes: 2 additions & 2 deletions library/Exceptions/InException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class InException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be in ({{haystack}})',
self::STANDARD => '{{name}} must be in {{haystack}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be in ({{haystack}})',
self::STANDARD => '{{name}} must not be in {{haystack}}',
),
);
}
12 changes: 0 additions & 12 deletions library/Exceptions/KeySetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,4 @@ public function chooseTemplate()

return parent::chooseTemplate();
}

/**
* {@inheritdoc}
*/
public function setParam($name, $value)
{
if ($name === 'keys') {
$value = trim(json_encode($value), '[]');
}

return parent::setParam($name, $value);
}
}
8 changes: 0 additions & 8 deletions library/Exceptions/LengthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class LengthException extends ValidationException
),
);

public function configure($name, array $params = array())
{
$params['minValue'] = static::stringify($params['minValue']);
$params['maxValue'] = static::stringify($params['maxValue']);

return parent::configure($name, $params);
}

public function chooseTemplate()
{
if (!$this->getParam('minValue')) {
Expand Down
4 changes: 2 additions & 2 deletions library/Exceptions/MimetypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class MimetypeException extends ValidationException
*/
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must have "{{mimetype}}" mimetype',
self::STANDARD => '{{name}} must have {{mimetype}} mimetype',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not have "{{mimetype}}" mimetype',
self::STANDARD => '{{name}} must not have {{mimetype}} mimetype',
),
);
}
166 changes: 129 additions & 37 deletions library/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Respect\Validation\Exceptions;

use DateTime;
use Exception;
use InvalidArgumentException;
use Traversable;

class ValidationException extends InvalidArgumentException implements ValidationExceptionInterface
{
Expand All @@ -27,10 +29,22 @@ class ValidationException extends InvalidArgumentException implements Validation
self::STANDARD => 'Data validation failed for %s',
),
);

/**
* @var int
*/
private static $maxDepthStringify = 5;

/**
* @var int Maximum depth when stringifying nested arrays
* @var int
*/
private static $maxDepthStringify = 3;
private static $maxCountStringify = 10;

/**
* @var string
*/
private static $maxReplacementStringify = '...';

protected $id = 'validation';
protected $mode = self::MODE_DEFAULT;
protected $name = '';
Expand All @@ -42,23 +56,61 @@ public static function format($template, array $vars = array())
return preg_replace_callback(
'/{{(\w+)}}/',
function ($match) use ($vars) {
return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
if (!isset($vars[$match[1]])) {
return $match[0];
}

$value = $vars[$match[1]];
if ('name' == $match[1]) {
return $value;
}

return ValidationException::stringify($value);
},
$template
);
}

public static function stringify($value)
/**
* @param mixed $value
* @param int $depth
*
* @return string
*/
public static function stringify($value, $depth = 1)
{
if (is_string($value)) {
return $value;
} elseif (is_array($value)) {
return self::stringifyArray($value);
} elseif (is_object($value)) {
return static::stringifyObject($value);
} else {
return (string) $value;
if ($depth >= self::$maxDepthStringify) {
return self::$maxReplacementStringify;
}

if (is_array($value)) {
return static::stringifyArray($value, $depth);
}

if (is_object($value)) {
return static::stringifyObject($value, $depth);
}

if (is_resource($value)) {
return sprintf('`[resource] (%s)`', get_resource_type($value));
}

if (is_float($value)) {
if (is_infinite($value)) {
return ($value > 0 ? '' : '-').'INF';
}

if (is_nan($value)) {
return 'NaN';
}
}

$options = 0;
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$options = (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

return (@json_encode($value, $options) ?: $value);
}

/**
Expand All @@ -67,37 +119,77 @@ public static function stringify($value)
*
* @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;
public static function stringifyArray(array $value, $depth = 1)
{
$nextDepth = ($depth + 1);
if ($nextDepth >= self::$maxDepthStringify) {
return self::$maxReplacementStringify;
}

if (empty($value)) {
return '{ }';
}

$total = count($value);
$string = '';
$current = 0;
foreach ($value as $childKey => $childValue) {
if ($current++ >= self::$maxCountStringify) {
$string .= self::$maxReplacementStringify;
break;
}

if (!is_int($childKey)) {
$string .= sprintf('%s: ', static::stringify($childKey, $nextDepth));
}

$string .= static::stringify($childValue, $nextDepth);

if ($current !== $total) {
$string .= ', ';
}
}

return implode(', ', $items);
return sprintf('{ %s }', $string);
}

public static function stringifyObject($value)
/**
* @param mixed $value
* @param int $depth
*
* @return string
*/
public static function stringifyObject($value, $depth = 2)
{
$nextDepth = $depth + 1;

if ($value instanceof DateTime) {
return sprintf('"%s"', $value->format('Y-m-d H:i:s'));
}

$class = get_class($value);

if ($value instanceof Traversable) {
return sprintf('`[traversable] (%s: %s)`', $class, static::stringify(iterator_to_array($value), $nextDepth));
}

if ($value instanceof Exception) {
$properties = array(
'message' => $value->getMessage(),
'code' => $value->getCode(),
'file' => $value->getFile().':'.$value->getLine(),
);

return sprintf('`[exception] (%s: %s)`', $class, static::stringify($properties, $nextDepth));
}

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);
return static::stringify($value->__toString(), $nextDepth);
}

$properties = static::stringify(get_object_vars($value), $nextDepth);

return sprintf('`[object] (%s: %s)`', $class, str_replace('`', '', $properties));
}

public function __toString()
Expand Down Expand Up @@ -175,7 +267,7 @@ public function setId($id)

public function setName($name)
{
$this->name = static::stringify($name);
$this->name = $name;

return $this;
}
Expand All @@ -190,7 +282,7 @@ public function setMode($mode)

public function setParam($key, $value)
{
$this->params[$key] = ($key == 'translator') ? $value : static::stringify($value);
$this->params[$key] = $value;

return $this;
}
Expand Down
3 changes: 1 addition & 2 deletions library/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function getName()
public function reportError($input, array $extraParams = array())
{
$exception = $this->createException();
$input = ValidationException::stringify($input);
$name = $this->name ?: "\"$input\"";
$name = $this->name ?: ValidationException::stringify($input);
$params = array_merge(
get_class_vars(__CLASS__),
get_object_vars($this),
Expand Down
23 changes: 2 additions & 21 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,9 @@ public function __call($method, $arguments)
return $this->addRule(static::buildRule($method, $arguments));
}

/**
* @param mixed $input
* @param array $extraParams
*
* @return AllOfException
*/
public function reportError($input, array $extraParams = array())
protected function createException()
{
$exception = new AllOfException();
$input = AllOfException::stringify($input);
$name = $this->getName() ?: "\"$input\"";
$params = array_merge(
$extraParams,
get_object_vars($this),
get_class_vars(__CLASS__)
);
$exception->configure($name, $params);
if (!is_null($this->template)) {
$exception->setTemplate($this->template);
}

return $exception;
return new AllOfException();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ try {
Array
(
[allOf] => Invalid Validation Form
[first_name_length] => Invalid length for security_question fiif
[first_name_length] => Invalid length for security_question "fiif"
)
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ try {
--EXPECTF--
Array
(
[allOf] => These rules must pass for Validation Form
[allOf] => All of the required rules must pass for Validation Form
[first_name_length] => security_question must have a length between 5 and 256
)
Loading