Completed
Pull Request — master (#4)
by
unknown
06:18
created

Verdicts   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 60
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A passing() 0 6 1
A failing() 0 6 1
A forField() 0 6 1
A map() 0 4 1
A filter() 0 4 1
A reduce() 0 4 1
A toArray() 0 4 1
A count() 0 4 1
A getIterator() 0 4 1
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