-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloakedErrors.php
78 lines (62 loc) · 1.4 KB
/
CloakedErrors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Bakame\Aide\Error;
use Countable;
use ErrorException;
use Iterator;
use IteratorAggregate;
use function count;
/**
* @implements IteratorAggregate<int, ErrorException>
*/
class CloakedErrors implements Countable, IteratorAggregate
{
/** @var array<ErrorException> */
private array $errors;
public function __construct(ErrorException ...$errors)
{
$this->errors = $errors;
}
public function count(): int
{
return count($this->errors);
}
/**
* @return Iterator<int, ErrorException>
*/
public function getIterator(): Iterator
{
yield from $this->errors;
}
public function unshift(ErrorException $exception): void
{
array_unshift($this->errors, $exception);
}
public function reset(): void
{
$this->errors = [];
}
public function isEmpty(): bool
{
return [] === $this->errors;
}
public function isNotEmpty(): bool
{
return !$this->isEmpty();
}
public function first(): ?ErrorException
{
return $this->get(-1);
}
public function last(): ?ErrorException
{
return $this->get(0);
}
public function get(int $offset): ?ErrorException
{
if ($offset < 0) {
$offset += count($this->errors);
}
return $this->errors[$offset] ?? null;
}
}