|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Albert221\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use Countable; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
|
|
10
|
|
|
class Verdicts implements Countable, IteratorAggregate |
|
11
|
|
|
{ |
|
12
|
|
|
private $verdicts; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(array $verdicts) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->verdicts = $verdicts; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function passing(): Verdicts |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->filter(function (Verdict $verdict) { |
|
22
|
|
|
return $verdict->passes(); |
|
23
|
|
|
}); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function failing(): Verdicts |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->filter(function (Verdict $verdict) { |
|
29
|
|
|
return !$verdict->passes(); |
|
30
|
|
|
}); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function forField(string $fieldName): Verdicts |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->filter(function (Verdict $verdict) use ($fieldName) { |
|
36
|
|
|
return $verdict->getField()->getName() === $fieldName; |
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function map(callable $function): Verdicts |
|
41
|
|
|
{ |
|
42
|
|
|
return new static(array_map($function, $this->verdicts)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function filter(callable $function): Verdicts |
|
46
|
|
|
{ |
|
47
|
|
|
return new static(array_filter($this->verdicts, $function)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function reduce(callable $function, $initial = null) |
|
51
|
|
|
{ |
|
52
|
|
|
return new static(array_reduce($this->verdicts, $function, $initial)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function toArray(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->verdicts; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function /* Countable */ count(): int |
|
61
|
|
|
{ |
|
62
|
|
|
return count($this->verdicts); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function /* IteratorAggregate */ getIterator(): \Traversable |
|
66
|
|
|
{ |
|
67
|
|
|
return new \ArrayIterator($this->verdicts); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|