Skip to content

Commit 32fc36f

Browse files
✨ add field types easy getters in objectfields
1 parent 242f2ea commit 32fc36f

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Parsing/V2/Field/ObjectField.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Mindee\Parsing\V2\Field;
44

5+
use InvalidArgumentException;
6+
57
/**
68
* A field containing a nested set of inference fields.
79
*/
@@ -43,4 +45,103 @@ public function toStringFromList(): string
4345
{
4446
return substr($this->fields->toString(2), 4);
4547
}
48+
49+
/**
50+
* Returns a ListField instance for the specified key.
51+
*
52+
* @param string $key The key of the list field to retrieve.
53+
* @return ListField
54+
* @throws InvalidArgumentException When the field does not exist or is not a list field.
55+
*/
56+
public function getListField(string $key): ListField
57+
{
58+
$field = $this->fields->get($key);
59+
if (!($field instanceof ListField)) {
60+
throw new InvalidArgumentException("Field $key is not a list field.");
61+
}
62+
return $field;
63+
}
64+
65+
/**
66+
* Returns a SimpleField instance for the specified key.
67+
*
68+
* @param string $key The key of the simple field to retrieve.
69+
* @return SimpleField
70+
* @throws InvalidArgumentException When the field does not exist or is not a simple field.
71+
*/
72+
public function getSimpleField(string $key): SimpleField
73+
{
74+
$field = $this->fields->get($key);
75+
if (!($field instanceof SimpleField)) {
76+
throw new InvalidArgumentException("Field $key is not a simple field.");
77+
}
78+
return $field;
79+
}
80+
81+
/**
82+
* Returns an ObjectField instance for the specified key.
83+
*
84+
* @param string $key The key of the simple field to retrieve.
85+
* @return ObjectField
86+
* @throws InvalidArgumentException When the field does not exist or is not a simple field.
87+
*/
88+
public function getObjectField(string $key): ObjectField
89+
{
90+
$field = $this->fields->get($key);
91+
if (!($field instanceof ObjectField)) {
92+
throw new InvalidArgumentException("Field $key is not a simple field.");
93+
}
94+
return $field;
95+
}
96+
97+
/**
98+
* Returns an array of SimpleField instances.
99+
*
100+
* @return SimpleField[]
101+
* @throws InvalidArgumentException When a field does not exist or is not a simple field.
102+
*/
103+
public function getSimpleFields(): array
104+
{
105+
$out = [];
106+
foreach ($this->fields->getArrayCopy() as $field) {
107+
if ($field instanceof SimpleField) {
108+
$out[] = $field;
109+
}
110+
}
111+
return $out;
112+
}
113+
114+
/**
115+
* Returns an array of ListField instances.
116+
*
117+
* @return ListField[]
118+
* @throws InvalidArgumentException When a field does not exist or is not a list field.
119+
*/
120+
public function getListFields(): array
121+
{
122+
$out = [];
123+
foreach ($this->fields->getArrayCopy() as $field) {
124+
if ($field instanceof ListField) {
125+
$out[] = $field;
126+
}
127+
}
128+
return $out;
129+
}
130+
131+
/**
132+
* Returns an array of ObjectField instances.
133+
*
134+
* @return ObjectField[]
135+
* @throws InvalidArgumentException When a field does not exist or is not an object field.
136+
*/
137+
public function getObjectFields(): array
138+
{
139+
$out = [];
140+
foreach ($this->fields->getArrayCopy() as $field) {
141+
if ($field instanceof ObjectField) {
142+
$out[] = $field;
143+
}
144+
}
145+
return $out;
146+
}
46147
}

tests/V2/Parsing/InferenceResponseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ public function testDeepNestedFieldsMustExposeCorrectTypes(): void
168168

169169
$fieldObject = $root->get('field_object');
170170
$this->assertInstanceOf(ObjectField::class, $fieldObject);
171+
$this->assertInstanceOf(SimpleField::class, $fieldObject->getSimpleField('sub_object_simple'));
172+
$this->assertInstanceOf(ListField::class, $fieldObject->getListField('sub_object_list'));
173+
$this->assertInstanceOf(ObjectField::class, $fieldObject->getObjectField('sub_object_object'));
174+
$this->assertEquals(1, count($fieldObject->getSimpleFields()));
175+
$this->assertEquals(1, count($fieldObject->getListFields()));
176+
$this->assertEquals(1, count($fieldObject->getObjectFields()));
171177
$lvl1 = $fieldObject->fields;
178+
$this->assertInstanceOf(SimpleField::class, $lvl1->get('sub_object_simple'));
172179
$this->assertInstanceOf(ListField::class, $lvl1->get('sub_object_list'));
173180
$this->assertInstanceOf(ObjectField::class, $lvl1->get('sub_object_object'));
174181

0 commit comments

Comments
 (0)