2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
|
|
|
|
use ArrayIterator;
|
|
|
|
use Countable;
|
|
|
|
use RecursiveIterator;
|
|
|
|
use UnexpectedValueException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2024-01-31 22:15:08 +08:00
|
|
|
*
|
|
|
|
* @implements RecursiveIterator<int, ValidationException>
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
final class RecursiveExceptionIterator implements RecursiveIterator, Countable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ArrayIterator|ValidationException[]
|
|
|
|
*/
|
|
|
|
private $exceptions;
|
|
|
|
|
|
|
|
public function __construct(NestedValidationException $parent)
|
|
|
|
{
|
|
|
|
$this->exceptions = new ArrayIterator($parent->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count(): int
|
|
|
|
{
|
|
|
|
return $this->exceptions->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasChildren(): bool
|
|
|
|
{
|
|
|
|
if (!$this->valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->current() instanceof NestedValidationException;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getChildren(): self
|
|
|
|
{
|
|
|
|
$exception = $this->current();
|
|
|
|
if (!$exception instanceof NestedValidationException) {
|
|
|
|
throw new UnexpectedValueException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new static($exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ValidationException|NestedValidationException
|
|
|
|
*/
|
|
|
|
public function current(): ValidationException
|
|
|
|
{
|
|
|
|
return $this->exceptions->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key(): int
|
|
|
|
{
|
|
|
|
return $this->exceptions->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next(): void
|
|
|
|
{
|
|
|
|
$this->exceptions->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind(): void
|
|
|
|
{
|
|
|
|
$this->exceptions->rewind();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid(): bool
|
|
|
|
{
|
|
|
|
return $this->exceptions->valid();
|
|
|
|
}
|
|
|
|
}
|