* * 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 Exception $exception * * @return bool */ private function isOmissible(Exception $exception) { if (!$exception instanceof self) { return false; } $relatedExceptions = $exception->getRelated(); $relatedExceptions->rewind(); $childException = $relatedExceptions->current(); return 1 === $relatedExceptions->count() && !$childException instanceof NonOmissibleException; } /** * @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; } }