1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Albert221\Validation; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Countable; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
class VerdictList implements Countable, IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var VerdictInterface[] |
16
|
|
|
*/ |
17
|
|
|
private $verdicts; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Verdicts constructor. |
21
|
|
|
* |
22
|
|
|
* @param VerdictInterface[] $verdicts |
23
|
|
|
*/ |
24
|
5 |
|
public function __construct(array $verdicts) |
25
|
|
|
{ |
26
|
5 |
|
$this->verdicts = $verdicts; |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
3 |
|
public function passes(): bool |
33
|
|
|
{ |
34
|
3 |
|
return count($this->failing()) === 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
1 |
|
public function fails(): bool |
41
|
|
|
{ |
42
|
1 |
|
return count($this->failing()) > 0; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return VerdictList |
47
|
|
|
*/ |
48
|
|
|
public function passing(): VerdictList |
49
|
|
|
{ |
50
|
|
|
return $this->filter(function (VerdictInterface $verdict) { |
51
|
|
|
return $verdict->passes(); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return VerdictList |
57
|
|
|
*/ |
58
|
|
|
public function failing(): VerdictList |
59
|
|
|
{ |
60
|
4 |
|
return $this->filter(function (VerdictInterface $verdict) { |
61
|
4 |
|
return !$verdict->passes(); |
62
|
4 |
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $fieldName |
67
|
|
|
* |
68
|
|
|
* @return VerdictList |
69
|
|
|
*/ |
70
|
|
|
public function forField(string $fieldName): VerdictList |
71
|
|
|
{ |
72
|
|
|
return $this->filter(function (VerdictInterface $verdict) use ($fieldName) { |
73
|
|
|
return $verdict->getField()->getName() === $fieldName; |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param callable $function |
79
|
|
|
* |
80
|
|
|
* @return VerdictList |
81
|
|
|
*/ |
82
|
|
|
public function map(callable $function): VerdictList |
83
|
|
|
{ |
84
|
|
|
return new static(array_map($function, $this->verdicts)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param callable $function |
89
|
|
|
* |
90
|
|
|
* @return VerdictList |
91
|
|
|
*/ |
92
|
4 |
|
public function filter(callable $function): VerdictList |
93
|
|
|
{ |
94
|
4 |
|
return new static(array_filter($this->verdicts, $function)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param callable $function |
99
|
|
|
* @param mixed $initial |
100
|
|
|
* |
101
|
|
|
* @return VerdictList |
102
|
|
|
*/ |
103
|
|
|
public function reduce(callable $function, $initial = null): VerdictList |
104
|
|
|
{ |
105
|
|
|
return new static(array_reduce($this->verdicts, $function, $initial)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return VerdictInterface[] |
110
|
|
|
*/ |
111
|
|
|
public function toArray(): array |
112
|
|
|
{ |
113
|
|
|
return $this->verdicts; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
4 |
|
public function count(): int |
120
|
|
|
{ |
121
|
4 |
|
return count($this->verdicts); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Traversable |
126
|
|
|
*/ |
127
|
|
|
public function getIterator(): Traversable |
128
|
|
|
{ |
129
|
|
|
return new ArrayIterator($this->verdicts); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|