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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
declare(strict_types=1);
namespace Bakame\Http\StructuredFields\Validation;
use ArrayAccess;
use Bakame\Http\StructuredFields\InvalidOffset;
use Countable;
use Iterator;
use IteratorAggregate;
use Stringable;
use TypeError;
use function array_filter;
use function array_map;
use function count;
use function implode;
use function is_int;
use const ARRAY_FILTER_USE_BOTH;
/**
* @implements IteratorAggregate<array-key,Violation>
* @implements ArrayAccess<array-key,Violation>
*/
final class ViolationList implements IteratorAggregate, Countable, ArrayAccess, Stringable
{
/** @var array<Violation> */
private array $errors = [];
/**
* @param iterable<array-key, Violation> $errors
*/
public function __construct(iterable $errors = [])
{
$this->addAll($errors);
}
public function count(): int
{
return count($this->errors);
}
public function getIterator(): Iterator
{
yield from $this->errors;
}
public function __toString(): string
{
return implode(PHP_EOL, array_map(fn (Violation $e): string => $e->getMessage(), $this->errors));
}
/**
* @return array<array-key, string>
*/
public function summary(): array
{
return array_map(fn (Violation $e): string => $e->getMessage(), $this->errors);
}
public function isEmpty(): bool
{
return [] === $this->errors;
}
public function isNotEmpty(): bool
{
return ! $this->isEmpty();
}
/**
* @param string|int $offset
*/
public function offsetExists(mixed $offset): bool
{
return $this->has($offset);
}
/**
* @param string|int $offset
*
* @return Violation
*/
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}
/**
* @param string|int $offset
*/
public function offsetUnset(mixed $offset): void
{
unset($this->errors[$offset]);
}
/**
* @param string|int|null $offset
* @param Violation $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (null === $offset) {
throw new TypeError('null can not be used as a valid offset value.');
}
$this->add($offset, $value);
}
public function has(string|int $offset): bool
{
if (is_int($offset)) {
return null !== $this->filterIndex($offset);
}
return array_key_exists($offset, $this->errors);
}
public function get(string|int $offset): Violation
{
return $this->errors[$this->filterIndex($offset) ?? throw InvalidOffset::dueToIndexNotFound($offset)];
}
public function add(string|int $offset, Violation $error): void
{
$this->errors[$offset] = $error;
}
/**
* @param iterable<array-key, Violation> $errors
*/
public function addAll(iterable $errors): void
{
foreach ($errors as $offset => $error) {
$this->add($offset, $error);
}
}
private function filterIndex(string|int $index, int|null $max = null): string|int|null
{
if (!is_int($index)) {
return $index;
}
$max ??= count($this->errors);
return match (true) {
[] === $this->errors,
0 > $max + $index,
0 > $max - $index - 1 => null,
0 > $index => $max + $index,
default => $index,
};
}
/**
* @param callable(Violation, array-key): bool $callback
*/
public function filter(callable $callback): self
{
return new self(array_filter($this->errors, $callback, ARRAY_FILTER_USE_BOTH));
}
public function toException(): Violation
{
return new Violation((string) $this);
}
}
|