Skip to content

Commit 438f818

Browse files
committed
field-to-schema
1 parent 2c66740 commit 438f818

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ $p = new Parser();
267267

268268
$schema = $p->object(['name' => $p->string()]);
269269

270+
// get the fieldToSchema which was passed within the initialization
271+
$fieldToSchema = $schema->getFieldToSchema();
272+
270273
// stdClass object
271274
$data = $schema->parse(['name' => 'example']);
272275

src/Schema/ObjectSchema.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ObjectSchema extends AbstractSchema implements ObjectSchemaInterface
1818
/**
1919
* @var array<string, SchemaInterface>
2020
*/
21-
private array $fieldNameToSchema;
21+
private array $fieldToSchema;
2222

2323
/**
2424
* @var null|array<string>
@@ -31,16 +31,16 @@ final class ObjectSchema extends AbstractSchema implements ObjectSchemaInterface
3131
private ?array $optional = null;
3232

3333
/**
34-
* @param array<string, SchemaInterface> $fieldNameToSchema
34+
* @param array<string, SchemaInterface> $fieldToSchema
3535
* @param class-string $classname
3636
*/
37-
public function __construct(array $fieldNameToSchema, private string $classname = \stdClass::class)
37+
public function __construct(array $fieldToSchema, private string $classname = \stdClass::class)
3838
{
39-
foreach ($fieldNameToSchema as $fieldName => $fieldSchema) {
39+
foreach ($fieldToSchema as $fieldName => $fieldSchema) {
4040
if (!\is_string($fieldName)) {
4141
throw new \InvalidArgumentException(
4242
\sprintf(
43-
'Argument #1 name #%s ($fieldNameToSchema) must be of type string, %s given',
43+
'Argument #1 name #%s ($fieldToSchema) must be of type string, %s given',
4444
$fieldName,
4545
$this->getDataType($fieldName)
4646
)
@@ -50,7 +50,7 @@ public function __construct(array $fieldNameToSchema, private string $classname
5050
if (!$fieldSchema instanceof SchemaInterface) {
5151
throw new \InvalidArgumentException(
5252
\sprintf(
53-
'Argument #1 value of #%s ($fieldNameToSchema) must be of type %s, %s given',
53+
'Argument #1 value of #%s ($fieldToSchema) must be of type %s, %s given',
5454
$fieldName,
5555
SchemaInterface::class,
5656
$this->getDataType($fieldSchema)
@@ -59,7 +59,7 @@ public function __construct(array $fieldNameToSchema, private string $classname
5959
}
6060
}
6161

62-
$this->fieldNameToSchema = $fieldNameToSchema;
62+
$this->fieldToSchema = $fieldToSchema;
6363
}
6464

6565
public function parse(mixed $input): mixed
@@ -111,9 +111,17 @@ public function parse(mixed $input): mixed
111111
}
112112
}
113113

114-
public function getFieldSchema(string $fieldName): ?SchemaInterface
114+
/**
115+
* @return array<string, SchemaInterface>
116+
*/
117+
public function getFieldToSchema(): array
118+
{
119+
return $this->fieldToSchema;
120+
}
121+
122+
public function getFieldSchema(string $field): ?SchemaInterface
115123
{
116-
return $this->fieldNameToSchema[$fieldName] ?? null;
124+
return $this->fieldToSchema[$field] ?? null;
117125
}
118126

119127
/**
@@ -150,7 +158,7 @@ private function unknownFields(array $input, ParserErrorException $childrenParse
150158
}
151159

152160
foreach (array_keys($input) as $fieldName) {
153-
if (!\in_array($fieldName, $this->strict, true) && !isset($this->fieldNameToSchema[$fieldName])) {
161+
if (!\in_array($fieldName, $this->strict, true) && !isset($this->fieldToSchema[$fieldName])) {
154162
$childrenParserErrorException->addError(new Error(
155163
self::ERROR_UNKNOWN_FIELD_CODE,
156164
self::ERROR_UNKNOWN_FIELD_TEMPLATE,
@@ -165,7 +173,7 @@ private function unknownFields(array $input, ParserErrorException $childrenParse
165173
*/
166174
private function parseFields(array $input, object $object, ParserErrorException $childrenParserErrorException): void
167175
{
168-
foreach ($this->fieldNameToSchema as $fieldName => $fieldSchema) {
176+
foreach ($this->fieldToSchema as $fieldName => $fieldSchema) {
169177
try {
170178
if (
171179
!\array_key_exists($fieldName, $input)

tests/Unit/Schema/ObjectSchemaTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testConstructWithoutFieldName(): void
5656
throw new \Exception('code should not be reached');
5757
} catch (\InvalidArgumentException $invalidArgumentException) {
5858
self::assertSame(
59-
'Argument #1 name #0 ($fieldNameToSchema) must be of type string, integer given',
59+
'Argument #1 name #0 ($fieldToSchema) must be of type string, integer given',
6060
$invalidArgumentException->getMessage()
6161
);
6262
}
@@ -70,7 +70,7 @@ public function testConstructWithoutFieldSchema(): void
7070
throw new \Exception('code should not be reached');
7171
} catch (\InvalidArgumentException $invalidArgumentException) {
7272
self::assertSame(
73-
'Argument #1 value of #field2 ($fieldNameToSchema) must be of type Chubbyphp\Parsing\Schema\SchemaInterface, string given',
73+
'Argument #1 value of #field2 ($fieldToSchema) must be of type Chubbyphp\Parsing\Schema\SchemaInterface, string given',
7474
$invalidArgumentException->getMessage()
7575
);
7676
}
@@ -370,6 +370,15 @@ public function testSafeParseFailed(): void
370370
], $this->errorsToSimpleArray($schema->safeParse(null)->exception->getErrors()));
371371
}
372372

373+
public function testGetFieldToSchema(): void
374+
{
375+
$fieldToSchema = ['field1' => new StringSchema(), 'field2' => new IntSchema()];
376+
377+
$schema = new ObjectSchema($fieldToSchema);
378+
379+
self::assertSame($fieldToSchema, $schema->getFieldToSchema());
380+
}
381+
373382
public function testGetFieldSchemaSuccess(): void
374383
{
375384
$field2Schema = new IntSchema();

0 commit comments

Comments
 (0)