-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsedInput.php
More file actions
196 lines (174 loc) · 6.05 KB
/
Copy pathParsedInput.php
File metadata and controls
196 lines (174 loc) · 6.05 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Firehed\Input\Containers;
use ArrayAccess;
use DomainException;
use BadMethodCallException;
use UnexpectedValueException;
use Firehed\Input\Exceptions\InputException;
use Firehed\Input\Interfaces\ValidationInterface;
/**
* @implements ArrayAccess<array-key, mixed>
*/
class ParsedInput extends RawInput implements ArrayAccess
{
/**
* @param array<array-key, mixed> $data
*/
public function __construct(array $data)
{
parent::__construct($data);
$this->setIsParsed(true);
}
/**
* @param ParsedInput $add data to add
* @return $this
* @throws BadMethodCallException
*/
public function addData(ParsedInput $add): self
{
if ($this->isValidated()) {
throw new BadMethodCallException(
"Data cannot be added after validation is performed"
);
}
if ($add->isValidated()) {
throw new BadMethodCallException(
"Data cannot be added after validation is performed"
);
}
$this->setData(array_merge($add->getData(), $this->getData()));
return $this;
}
/**
* @param ValidationInterface $validator Validation requirements
* @return SafeInput
* @throws InputException
*/
public function validate(ValidationInterface $validator): SafeInput
{
$data = $this->getData();
$clean_out = [];
$missing = [];
$invalid = [];
$unexpected = [];
foreach ($validator->getRequiredInputs() as $key => $input) {
if (!isset($data[$key]) && !array_key_exists($key, $data)) {
$missing[] = $key;
continue;
}
try {
$clean_out[$key] = $input->setValue($data[$key])
->evaluate();
} catch (InputException $e) {
$prefix = function ($k) use ($key) {
return $key . '.' . $k;
};
$invalid = array_merge($invalid, array_map($prefix, $e->getInvalid()));
$missing = array_merge($missing, array_map($prefix, $e->getMissing()));
$unexpected = array_merge($unexpected, array_map($prefix, $e->getUnexpected()));
} catch (UnexpectedValueException $e) {
$invalid[] = $key;
} finally {
unset($data[$key]);
}
} unset($key, $input);
foreach ($validator->getOptionalInputs() as $key => $input) {
if (array_key_exists($key, $data)) {
try {
$clean_out[$key] = $input->setValue($data[$key])
->evaluate();
} catch (InputException $e) {
$prefix = function ($k) use ($key) {
return $key . '.' . $k;
};
$invalid = array_merge($invalid, array_map($prefix, $e->getInvalid()));
$missing = array_merge($missing, array_map($prefix, $e->getMissing()));
$unexpected = array_merge($unexpected, array_map($prefix, $e->getUnexpected()));
} catch (UnexpectedValueException $e) {
$invalid[] = $key;
} finally {
unset($data[$key]);
}
} else {
$clean_out[$key] = $input->getDefaultValue();
unset($data[$key]); // in case of literal null value
}
} unset($key, $input);
$unexpected = array_merge($unexpected, array_keys($data));
// This is a not-beautiful way of expressing "if at least two error
// arrays are nonempty", since this should retain the more specific
// exception code when only one type of error is present.
if (($missing && ($invalid || $unexpected)) || ($invalid && $unexpected)) {
throw new InputException(
InputException::MULTIPLE_VALUE_ERRORS,
compact('invalid', 'missing', 'unexpected')
);
}
if ($missing) {
throw new InputException(InputException::MISSING_VALUES, $missing);
}
if ($invalid) {
throw new InputException(InputException::INVALID_VALUES, $invalid);
}
if ($unexpected) {
throw new InputException(InputException::UNEXPECTED_VALUES, $unexpected);
}
$this->setData($clean_out);
$this->setIsValidated(true);
return new SafeInput($this);
}
/**
* Return the data as an array (this loses the metadata around
* parsing and validating)
*
* @return array<mixed>
*/
public function asArray(): array
{
return $this->getData();
}
// ----(ArrayAccess)-------------------------------------------------------
/**
* Invoked via `isset` and `empty`
*/
public function offsetExists($offset): bool
{
throw new BadMethodCallException(
"ParsedInput is already validated, and contains all expected " .
"keys. Use standard binary comparitors on the values."
);
}
/**
* Invoked by array access of the object
*/
public function offsetGet($offset): mixed
{
$data = $this->getData();
if (
isset($data[$offset]) ||
array_key_exists($offset, $data)
) {
return $data[$offset];
}
throw new DomainException(
"You are trying to access a value which does not exist. Because " .
"the input is already validated, this means there is a bug in " .
"the code. Most likely, there is a typo in the key."
);
}
/**
* Invoked by setting an array value on the object
*/
public function offsetSet($offset, $value): never
{
throw new BadMethodCallException("ParsedInput is read-only");
}
/**
* Invoked via `unset`
*/
public function offsetUnset($offset): never
{
throw new BadMethodCallException("ParsedInput is read-only");
}
}