-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractObjectSchema.php
More file actions
173 lines (141 loc) · 4.8 KB
/
AbstractObjectSchema.php
File metadata and controls
173 lines (141 loc) · 4.8 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
<?php
declare(strict_types=1);
namespace Chubbyphp\Parsing\Schema;
use Chubbyphp\Parsing\Error;
use Chubbyphp\Parsing\Errors;
use Chubbyphp\Parsing\ErrorsException;
abstract class AbstractObjectSchema extends AbstractSchemaInnerParse implements ObjectSchemaInterface
{
public const string ERROR_TYPE_CODE = 'abstract_object.type';
public const string ERROR_TYPE_TEMPLATE = 'Type should be "array|\stdClass|\Traversable", {{given}} given';
public const string ERROR_UNKNOWN_FIELD_CODE = 'abstract_object.unknownField';
public const string ERROR_UNKNOWN_FIELD_TEMPLATE = 'Unknown field {{fieldName}}';
/**
* @var array<string, SchemaInterface>
*/
private readonly array $fieldToSchema;
/**
* @var null|array<string>
*/
private ?array $strict = null;
/**
* @var null|array<string>
*/
private ?array $optional = null;
/**
* @param array<mixed, mixed> $fieldToSchema
*/
public function __construct(array $fieldToSchema)
{
$typeCheckedFieldToSchema = [];
foreach ($fieldToSchema as $fieldName => $fieldSchema) {
if (!\is_string($fieldName)) {
throw new \InvalidArgumentException(
\sprintf(
'Argument #1 name #%s ($fieldToSchema) must be of type string, %s given',
$fieldName,
$this->getDataType($fieldName)
)
);
}
if (!$fieldSchema instanceof SchemaInterface) {
throw new \InvalidArgumentException(
\sprintf(
'Argument #1 value of #%s ($fieldToSchema) must be of type %s, %s given',
$fieldName,
SchemaInterface::class,
$this->getDataType($fieldSchema)
)
);
}
$typeCheckedFieldToSchema[$fieldName] = $fieldSchema;
}
$this->fieldToSchema = $typeCheckedFieldToSchema;
$this->preParses[] = static function (mixed $input) {
if ($input instanceof \stdClass || $input instanceof \Traversable) {
return (array) $input;
}
if ($input instanceof \JsonSerializable) {
return $input->jsonSerialize();
}
return $input;
};
}
/**
* @return array<string, SchemaInterface>
*/
final public function getFieldToSchema(): array
{
return $this->fieldToSchema;
}
final public function getFieldSchema(string $field): ?SchemaInterface
{
return $this->fieldToSchema[$field] ?? null;
}
final public function strict(array $strict = []): static
{
$clone = clone $this;
$clone->strict = $strict;
return $clone;
}
/**
* @param array<string> $optional
*/
final public function optional(array $optional = []): static
{
$clone = clone $this;
$clone->optional = $optional;
return $clone;
}
protected function innerParse(mixed $input): mixed
{
if (!\is_array($input)) {
throw new ErrorsException(
new Error(
static::ERROR_TYPE_CODE,
static::ERROR_TYPE_TEMPLATE,
['given' => $this->getDataType($input)]
)
);
}
/** @var array<string, mixed> $input */
$childrenErrors = new Errors();
$this->unknownFields($input, $childrenErrors);
$output = $this->parseFields($input, $childrenErrors);
if ($childrenErrors->has()) {
throw new ErrorsException($childrenErrors);
}
return $output;
}
/**
* @param array<string, mixed> $input
*/
abstract protected function parseFields(array $input, Errors $childrenErrors): mixed;
/**
* @param array<string, mixed> $input
*/
final protected function skip(array $input, string $fieldName): bool
{
return !\array_key_exists($fieldName, $input)
&& \is_array($this->optional)
&& \in_array($fieldName, $this->optional, true);
}
/**
* @param array<string, mixed> $input
*/
private function unknownFields(array $input, Errors $childrenErrors): void
{
if (null === $this->strict) {
return;
}
foreach (array_keys($input) as $fieldName) {
if (!\in_array($fieldName, $this->strict, true) && !isset($this->fieldToSchema[$fieldName])) {
$childrenErrors->add(new Error(
static::ERROR_UNKNOWN_FIELD_CODE,
static::ERROR_UNKNOWN_FIELD_TEMPLATE,
['fieldName' => $fieldName]
), $fieldName);
}
}
}
}