Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/Parsing/V2/Field/ObjectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mindee\Parsing\V2\Field;

use InvalidArgumentException;

/**
* A field containing a nested set of inference fields.
*/
Expand Down Expand Up @@ -43,4 +45,103 @@ public function toStringFromList(): string
{
return substr($this->fields->toString(2), 4);
}

/**
* Returns a ListField instance for the specified key.
*
* @param string $key The key of the list field to retrieve.
* @return ListField
* @throws InvalidArgumentException When the field does not exist or is not a list field.
*/
public function getListField(string $key): ListField
{
$field = $this->fields->get($key);
if (!($field instanceof ListField)) {
throw new InvalidArgumentException("Field $key is not a list field.");
}
return $field;
}

/**
* Returns a SimpleField instance for the specified key.
*
* @param string $key The key of the simple field to retrieve.
* @return SimpleField
* @throws InvalidArgumentException When the field does not exist or is not a simple field.
*/
public function getSimpleField(string $key): SimpleField
{
$field = $this->fields->get($key);
if (!($field instanceof SimpleField)) {
throw new InvalidArgumentException("Field $key is not a simple field.");
}
return $field;
}

/**
* Returns an ObjectField instance for the specified key.
*
* @param string $key The key of the simple field to retrieve.
* @return ObjectField
* @throws InvalidArgumentException When the field does not exist or is not a simple field.
*/
public function getObjectField(string $key): ObjectField
{
$field = $this->fields->get($key);
if (!($field instanceof ObjectField)) {
throw new InvalidArgumentException("Field $key is not a simple field.");
}
return $field;
}

/**
* Returns an array of SimpleField instances.
*
* @return SimpleField[]
* @throws InvalidArgumentException When a field does not exist or is not a simple field.
*/
public function getSimpleFields(): array
{
$out = [];
foreach ($this->fields->getArrayCopy() as $field) {
if ($field instanceof SimpleField) {
$out[] = $field;
}
}
return $out;
}

/**
* Returns an array of ListField instances.
*
* @return ListField[]
* @throws InvalidArgumentException When a field does not exist or is not a list field.
*/
public function getListFields(): array
{
$out = [];
foreach ($this->fields->getArrayCopy() as $field) {
if ($field instanceof ListField) {
$out[] = $field;
}
}
return $out;
}

/**
* Returns an array of ObjectField instances.
*
* @return ObjectField[]
* @throws InvalidArgumentException When a field does not exist or is not an object field.
*/
public function getObjectFields(): array
{
$out = [];
foreach ($this->fields->getArrayCopy() as $field) {
if ($field instanceof ObjectField) {
$out[] = $field;
}
}
return $out;
}
}
4 changes: 2 additions & 2 deletions tests/V2/ClientV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testDocumentGetInferenceAsync(): void
/** @var MindeeApiV2&MockObject $predictable */
$predictable = $this->createMock(MindeeApiV2::class);

$jsonFile = \TestingUtilities::getV2DataDir() . '/products/financial_document/complete.json';
$jsonFile = \TestingUtilities::getV2DataDir() . '/products/extraction/financial_document/complete.json';
$this->assertFileExists($jsonFile, 'Test resource file must exist');

$json = json_decode(file_get_contents($jsonFile), true);
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testDocumentGetInferenceAsync(): void

public function testInferenceLoadsLocally(): void
{
$jsonFile = \TestingUtilities::getV2DataDir() . '/products/financial_document/complete.json';
$jsonFile = \TestingUtilities::getV2DataDir() . '/products/extraction/financial_document/complete.json';
$this->assertFileExists($jsonFile, 'Test resource file must exist');

$localResponse = new LocalResponse($jsonFile);
Expand Down
2 changes: 1 addition & 1 deletion tests/V2/ClientV2TestFunctional.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function testDataSchemaMustSucceed(): void {
TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf'
);
$dataSchemaReplace = file_get_contents(
TestingUtilities::getV2DataDir() . '/inference/data_schema_replace_param.json'
TestingUtilities::getV2DataDir() . '/products/extraction/data_schema_replace_param.json'
);

$inferenceParams = new InferenceParameters($this->modelId, dataSchema: $dataSchemaReplace);
Expand Down
2 changes: 1 addition & 1 deletion tests/V2/Input/InferenceParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class InferenceParameterTest extends TestCase
private DataSchema $expectedSchemaObject;

protected function setUp(): void {
$fileContents = file_get_contents(\TestingUtilities::getV2DataDir() . '/inference/data_schema_replace_param.json');
$fileContents = file_get_contents(\TestingUtilities::getV2DataDir() . '/products/extraction/data_schema_replace_param.json');
$this->expectedSchemaString = $fileContents;
$this->expectedSchemaDict = json_decode($fileContents, true);
$this->expectedSchemaObject = new DataSchema($fileContents);
Expand Down
4 changes: 2 additions & 2 deletions tests/V2/Input/LocalResponseV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class LocalResponseV2Test extends TestCase

protected function setUp(): void
{
$this->filePath = \TestingUtilities::getV2DataDir() . '/inference/standard_field_types.json';
$this->filePath = \TestingUtilities::getV2DataDir() . '/products/extraction/standard_field_types.json';
}

protected function assertLocalResponse(LocalResponse $localResponse): void
{
$fakeHMACSigning = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
$signature = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85";
$signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1";
$reflectedLocalResponse = new \ReflectionClass($localResponse);
$reflectedFile = $reflectedLocalResponse->getProperty('file');
$reflectedFile->setAccessible(true);
Expand Down
31 changes: 19 additions & 12 deletions tests/V2/Parsing/InferenceResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function readFileAsString(string $path): string
*/
public function testAsyncPredictWhenEmptyMustHaveValidProperties(): void
{
$response = $this->loadFromResource('v2/products/financial_document/blank.json');
$response = $this->loadFromResource('v2/products/extraction/financial_document/blank.json');
$fields = $response->inference->result->fields;

$this->assertCount(21, $fields, 'Expected 21 fields');
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testAsyncPredictWhenEmptyMustHaveValidProperties(): void
*/
public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
{
$response = $this->loadFromResource('v2/products/financial_document/complete.json');
$response = $this->loadFromResource('v2/products/extraction/financial_document/complete.json');
$inference = $response->inference;

$this->assertNotNull($inference, 'Inference must not be null');
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
*/
public function testDeepNestedFieldsMustExposeCorrectTypes(): void
{
$response = $this->loadFromResource('v2/inference/deep_nested_fields.json');
$response = $this->loadFromResource('v2/products/extraction/deep_nested_fields.json');
$inference = $response->inference;
$this->assertNotNull($inference);

Expand All @@ -168,7 +168,14 @@ public function testDeepNestedFieldsMustExposeCorrectTypes(): void

$fieldObject = $root->get('field_object');
$this->assertInstanceOf(ObjectField::class, $fieldObject);
$this->assertInstanceOf(SimpleField::class, $fieldObject->getSimpleField('sub_object_simple'));
$this->assertInstanceOf(ListField::class, $fieldObject->getListField('sub_object_list'));
$this->assertInstanceOf(ObjectField::class, $fieldObject->getObjectField('sub_object_object'));
$this->assertEquals(1, count($fieldObject->getSimpleFields()));
$this->assertEquals(1, count($fieldObject->getListFields()));
$this->assertEquals(1, count($fieldObject->getObjectFields()));
$lvl1 = $fieldObject->fields;
$this->assertInstanceOf(SimpleField::class, $lvl1->get('sub_object_simple'));
$this->assertInstanceOf(ListField::class, $lvl1->get('sub_object_list'));
$this->assertInstanceOf(ObjectField::class, $lvl1->get('sub_object_object'));

Expand All @@ -195,7 +202,7 @@ public function testDeepNestedFieldsMustExposeCorrectTypes(): void
*/
public function testStandardFieldTypesMustExposeCorrectTypes(): void
{
$response = $this->loadFromResource('v2/inference/standard_field_types.json');
$response = $this->loadFromResource('v2/products/extraction/standard_field_types.json');
$inference = $response->inference;
$this->assertNotNull($inference);

Expand Down Expand Up @@ -276,7 +283,7 @@ public function testStandardFieldTypesMustExposeCorrectTypes(): void
*/
public function testRawTextsMustBeAccessible(): void
{
$response = $this->loadFromResource('v2/inference/raw_texts.json');
$response = $this->loadFromResource('v2/products/extraction/raw_texts.json');
$inference = $response->inference;
$this->assertNotNull($inference);

Expand All @@ -303,9 +310,9 @@ public function testRawTextsMustBeAccessible(): void
*/
public function testRstDisplayMustBeAccessible(): void
{
$response = $this->loadFromResource('v2/inference/standard_field_types.json');
$response = $this->loadFromResource('v2/products/extraction/standard_field_types.json');
$expectedRst = $this->readFileAsString(
\TestingUtilities::getV2DataDir() . '/inference/standard_field_types.rst'
\TestingUtilities::getV2DataDir() . '/products/extraction/standard_field_types.rst'
);
$inference = $response->inference;
$this->assertNotNull($inference);
Expand All @@ -317,7 +324,7 @@ public function testRstDisplayMustBeAccessible(): void
*/
public function testCoordinatesAndLocationDataMustBeAccessible(): void
{
$response = $this->loadFromResource('v2/products/financial_document/complete_with_coordinates.json');
$response = $this->loadFromResource('v2/products/extraction/financial_document/complete_with_coordinates.json');
$inference = $response->inference;
$this->assertNotNull($inference);

Expand Down Expand Up @@ -376,15 +383,15 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void

public function testRagMetadataWhenMatched()
{
$response = $this->loadFromResource('v2/inference/rag_matched.json');
$response = $this->loadFromResource('v2/products/extraction/rag_matched.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$this->assertEquals('12345abc-1234-1234-1234-123456789abc', $inference->result->rag->retrievedDocumentId);
}

public function testRagMetadataWhenNotMatched()
{
$response = $this->loadFromResource('v2/inference/rag_not_matched.json');
$response = $this->loadFromResource('v2/products/extraction/rag_not_matched.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$this->assertNull($inference->result->rag->retrievedDocumentId);
Expand All @@ -404,7 +411,7 @@ public function testShouldLoadWith422Error()

public function testTextContextIsTrue(): void
{
$response = $this->loadFromResource('v2/inference/text_context_enabled.json');
$response = $this->loadFromResource('v2/products/extraction/text_context_enabled.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$activeOptions = $inference->activeOptions;
Expand All @@ -417,7 +424,7 @@ public function testTextContextIsTrue(): void

public function testTextContextIsFalse(): void
{
$response = $this->loadFromResource('v2/products/financial_document/complete.json');
$response = $this->loadFromResource('v2/products/extraction/financial_document/complete.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$activeOptions = $inference->activeOptions;
Expand Down
2 changes: 1 addition & 1 deletion tests/resources
Submodule resources updated 38 files
+ v1/products/multi_receipts_detector/default_sample_180.jpg
+ v1/products/multi_receipts_detector/default_sample_270.jpg
+ v1/products/multi_receipts_detector/default_sample_90.jpg
+2 −2 v1/products/multi_receipts_detector/response_v1/complete.json
+310 −0 v1/products/multi_receipts_detector/response_v1/complete_180.json
+310 −0 v1/products/multi_receipts_detector/response_v1/complete_270.json
+310 −0 v1/products/multi_receipts_detector/response_v1/complete_90.json
+7 −0 v2/errors/error_401_invalid_api_key.json
+7 −0 v2/errors/error_401_missing_credentials.json
+0 −35 v2/inference/text_context_enabled.json
+22 −0 v2/products/classification/classification_single.json
+ v2/products/classification/default_invoice.jpg
+69 −0 v2/products/crop/crop_multiple.json
+45 −0 v2/products/crop/crop_single.json
+ v2/products/crop/default_sample.jpg
+ v2/products/crop/multipage_sample.pdf
+3 −0 v2/products/extraction/data_schema_replace.json
+0 −0 v2/products/extraction/data_schema_replace_param.json
+6 −1 v2/products/extraction/deep_nested_fields.json
+3 −0 v2/products/extraction/financial_document/blank.json
+3 −0 v2/products/extraction/financial_document/complete.json
+3 −0 v2/products/extraction/financial_document/complete_with_coordinates.json
+ v2/products/extraction/financial_document/default_sample.jpg
+3 −0 v2/products/extraction/financial_document/default_sample.json
+3 −0 v2/products/extraction/rag_matched.json
+3 −0 v2/products/extraction/rag_not_matched.json
+3 −0 v2/products/extraction/raw_texts.json
+0 −0 v2/products/extraction/raw_texts.txt
+3 −0 v2/products/extraction/standard_field_types.json
+0 −0 v2/products/extraction/standard_field_types.rst
+38 −0 v2/products/extraction/text_context_enabled.json
+ v2/products/ocr/default_sample.jpg
+23,136 −0 v2/products/ocr/ocr_multiple.json
+6,431 −0 v2/products/ocr/ocr_single.json
+ v2/products/split/default_sample.pdf
+ v2/products/split/invoice_5p.pdf
+42 −0 v2/products/split/split_multiple.json
+28 −0 v2/products/split/split_single.json