respect-validation/library/Exceptions/RecursiveExceptionIterator.php
Henrique Moody 9a13c9fb03
Update coding standards
This change will bring many breaking changes. The good thing is that we
can finally use more modern resources available in PHP.

I can imagine that's not a popular change since it will bring many
breaking changes to users, but we shouldn't be stuck in time because of
that. Using some of those features will make it easier to contribute to
the project. At least, I hope so.

There are still some useless doc-blocks, and we're not using "readonly"
properties when we could. I aim to send those changes soon.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 00:22:41 +01:00

83 lines
1.7 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use ArrayIterator;
use Countable;
use RecursiveIterator;
use UnexpectedValueException;
/**
* @implements RecursiveIterator<int, ValidationException>
*/
final class RecursiveExceptionIterator implements RecursiveIterator, Countable
{
/**
* @var ArrayIterator<int, ValidationException>
*/
private ArrayIterator $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 (int) $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();
}
}