respect-validation/library/Exceptions/NestedValidationException.php
2018-05-27 16:12:05 +02:00

206 lines
5.2 KiB
PHP

<?php
/*
* 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.
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use IteratorAggregate;
use RecursiveIteratorIterator;
use SplObjectStorage;
class NestedValidationException extends ValidationException implements IteratorAggregate
{
/**
* @var SplObjectStorage
*/
private $exceptions = [];
/**
* @param ValidationException $exception
*
* @return self
*/
public function addRelated(ValidationException $exception)
{
$this->getRelated()->attach($exception);
return $this;
}
/**
* @return RecursiveIteratorIterator
*/
private function getRecursiveIterator()
{
$exceptionIterator = new RecursiveExceptionIterator($this);
$recursiveIteratorIterator = new RecursiveIteratorIterator(
$exceptionIterator,
RecursiveIteratorIterator::SELF_FIRST
);
return $recursiveIteratorIterator;
}
/**
* Returns weather an exception should be omitted or not.
*
* @param ExceptionInterface $exception
*
* @return bool
*/
private function isOmissible(ExceptionInterface $exception)
{
if (!$exception instanceof self) {
return false;
}
$relatedExceptions = $exception->getRelated();
$relatedExceptions->rewind();
$childException = $relatedExceptions->current();
return 1 === $relatedExceptions->count() && !$childException instanceof NonOmissibleExceptionInterface;
}
/**
* @return SplObjectStorage
*/
public function getIterator()
{
$childrenExceptions = new SplObjectStorage();
$recursiveIteratorIterator = $this->getRecursiveIterator();
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator();
$lastDepth = 0;
$lastDepthOriginal = 0;
$knownDepths = [];
foreach ($recursiveIteratorIterator as $childException) {
if ($this->isOmissible($childException)) {
continue;
}
$currentDepth = $lastDepth;
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
if (isset($knownDepths[$currentDepthOriginal])) {
$currentDepth = $knownDepths[$currentDepthOriginal];
} elseif ($currentDepthOriginal > $lastDepthOriginal
&& ($this->hasCustomTemplate() || 1 != $exceptionIterator->count())) {
++$currentDepth;
}
if (!isset($knownDepths[$currentDepthOriginal])) {
$knownDepths[$currentDepthOriginal] = $currentDepth;
}
$lastDepth = $currentDepth;
$lastDepthOriginal = $currentDepthOriginal;
$childrenExceptions->attach(
$childException,
[
'depth' => $currentDepth,
'depth_original' => $currentDepthOriginal,
'previous_depth' => $lastDepth,
'previous_depth_original' => $lastDepthOriginal,
]
);
}
return $childrenExceptions;
}
/**
* @return array
*/
public function getMessages()
{
$messages = [$this->getMessage()];
foreach ($this as $exception) {
$messages[] = $exception->getMessage();
}
if (count($messages) > 1) {
array_shift($messages);
}
return $messages;
}
/**
* @return string
*/
public function getFullMessage()
{
$marker = '-';
$messages = [];
$exceptions = $this->getIterator();
if ($this->hasCustomTemplate() || 1 != count($exceptions)) {
$messages[] = sprintf('%s %s', $marker, $this->getMessage());
}
foreach ($exceptions as $exception) {
$depth = $exceptions[$exception]['depth'];
$prefix = str_repeat(' ', $depth * 2);
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
}
return implode(PHP_EOL, $messages);
}
/**
* @return SplObjectStorage
*/
public function getRelated()
{
if (!$this->exceptions instanceof SplObjectStorage) {
$this->exceptions = new SplObjectStorage();
}
return $this->exceptions;
}
/**
* @param string $name
* @param mixed $value
*
* @return self
*/
public function setParam($name, $value)
{
if ('translator' === $name) {
foreach ($this->getRelated() as $exception) {
$exception->setParam($name, $value);
}
}
parent::setParam($name, $value);
return $this;
}
/**
* @param array $exceptions
*
* @return self
*/
public function setRelated(array $exceptions)
{
foreach ($exceptions as $exception) {
$this->addRelated($exception);
}
return $this;
}
}