Completed
Push — master ( 6630f6...b334d3 )
by Albert
05:18
created

ValidationState::offsetExists()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation;
6
7
use ArrayAccess;
8
use OutOfBoundsException;
9
use OutOfRangeException;
10
11
class ValidationState implements ArrayAccess
12
{
13
    const FLAT = 0;
14
    const GROUPED = 1;
15
16
    /**
17
     * @var Verdict[]
18
     */
19
    private $verdicts;
20
21
    /**
22
     * ValidationState constructor.
23
     *
24
     * @param array $verdicts
25
     */
26
    public function __construct(array $verdicts)
27
    {
28
        $this->verdicts = $verdicts;
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    public function isValid(): bool
35
    {
36
        foreach ($this->verdicts as $verdict) {
37
            if (!$verdict->passes()) {
38
                return false;
39
            }
40
        }
41
42
        return true;
43
    }
44
45
    /**
46
     * @param int $strategy
47
     *
48
     * @return Verdict[]
49
     */
50
    public function all(int $strategy = self::FLAT): array
51
    {
52
        if (self::FLAT === $strategy) {
53
            return $this->verdicts;
54
        }
55
56
        $array = [];
57
        foreach ($this->verdicts as $verdict) {
58
            $array[$verdict->getField()->getName()] = $verdict;
59
        }
60
61
        return $array;
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @return bool
68
     */
69
    public function isFieldValid(string $name): bool
70
    {
71
        $doesExist = false;
72
        foreach ($this->verdicts as $verdict) {
73
            if ($name !== $verdict->getField()->getName()) {
74
                continue;
75
            }
76
77
            $doesExist = true;
78
79
            if (!$verdict->passes()) {
80
                return false;
81
            }
82
        }
83
84
        if (!$doesExist) {
85
            throw new \InvalidArgumentException(sprintf(
86
                'Field with name "%s" does not exist.',
87
                $name
88
            ));
89
        }
90
91
        return true;
92
    }
93
94
    /**
95
     * @param string $name
96
     *
97
     * @return Verdict[]
98
     */
99
    public function field(string $name): array
100
    {
101
        $verdicts = [];
102
        foreach ($this->verdicts as $verdict) {
103
            if ($name === $verdict->getField()->getName()) {
104
                $verdicts[] = $verdict;
105
            }
106
        }
107
108
        return $verdicts;
109
    }
110
111
    /**
112
     * @param int|string $offset
113
     *
114
     * @return bool
115
     */
116
    public function offsetExists($offset): bool
117
    {
118
        if (is_int($offset)) {
119
            return isset($this->verdicts[$offset]);
120
        }
121
122
        if (!is_string($offset)) {
123
            throw new OutOfRangeException(sprintf(
124
                'Offset should be an integer index for FLAT strategy or string for GROUPED, %s given.',
125
                is_scalar($offset) ? gettype($offset): get_class($offset)
126
            ));
127
        }
128
129
        foreach ($this->verdicts as $verdict) {
130
            if ($offset === $verdict->getField()->getName()) {
131
                return true;
132
            }
133
        }
134
135
        return false;
136
    }
137
138
    /**
139
     * @param int|string $offset
140
     *
141
     * @return array
142
     */
143
    public function offsetGet($offset): array
144
    {
145
        if (!$this->offsetExists($offset)) {
146
            throw new OutOfBoundsException(sprintf(
147
                'Item with offset "%s" does not exist.',
148
                $offset
149
            ));
150
        }
151
152
        if (is_int($offset)) {
153
            return $this->all()[$offset];
154
        }
155
156
        return $this->field($offset);
157
    }
158
159
    public function offsetSet($offset, $value)
160
    {
161
        // You cannot set any value.
162
    }
163
164
    public function offsetUnset($offset)
165
    {
166
        // You cannot unset any value.
167
    }
168
169
170
}
171