diff --git a/src/Client.php b/src/Client.php index 723186b7..54190b3f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -8,21 +8,21 @@ namespace Mindee; +use Mindee\Error\MindeeApiException; use Mindee\Error\MindeeClientException; use Mindee\Error\MindeeHttpException; -use Mindee\Http\ResponseValidation; -use Mindee\Input\EnqueueAndParseMethodOptions; -use Mindee\Input\InputSource; -use Mindee\Input\PathInput; -use Mindee\Input\PredictMethodOptions; -use Mindee\Error\MindeeApiException; use Mindee\Http\Endpoint; use Mindee\Http\MindeeApi; +use Mindee\Http\ResponseValidation; use Mindee\Input\Base64Input; use Mindee\Input\BytesInput; +use Mindee\Input\EnqueueAndParseMethodOptions; use Mindee\Input\FileInput; +use Mindee\Input\InputSource; use Mindee\Input\LocalInputSource; use Mindee\Input\PageOptions; +use Mindee\Input\PathInput; +use Mindee\Input\PredictMethodOptions; use Mindee\Input\URLInputSource; use Mindee\Parsing\Common\AsyncPredictResponse; use Mindee\Parsing\Common\PredictResponse; @@ -247,10 +247,12 @@ private function makeEnqueueRequest( InputSource $inputDoc, PredictMethodOptions $options ): AsyncPredictResponse { - if ($inputDoc instanceof LocalInputSource) { - $this->cutDocPages($inputDoc, $options->pageOptions); - } else { - throw new MindeeApiException("Cannot edit non-local input sources."); + if (!$options->pageOptions->isEmpty()) { + if ($inputDoc instanceof LocalInputSource) { + $this->cutDocPages($inputDoc, $options->pageOptions); + } else { + throw new MindeeApiException("Cannot edit non-local input sources."); + } } $response = ResponseValidation::cleanRequestData($options->endpoint->predictAsyncRequestPost( $inputDoc, @@ -282,10 +284,12 @@ private function makeParseRequest( InputSource $inputDoc, PredictMethodOptions $options ): PredictResponse { - if ($inputDoc instanceof LocalInputSource) { - $this->cutDocPages($inputDoc, $options->pageOptions); - } else { - throw new MindeeApiException("Cannot edit non-local input sources."); + if (!$options->pageOptions->isEmpty()) { + if ($inputDoc instanceof LocalInputSource) { + $this->cutDocPages($inputDoc, $options->pageOptions); + } else { + throw new MindeeApiException("Cannot edit non-local input sources."); + } } $response = ResponseValidation::cleanRequestData($options->endpoint->predictRequestPost( $inputDoc, diff --git a/src/Input/PageOptions.php b/src/Input/PageOptions.php index 6e774e62..d3882e25 100644 --- a/src/Input/PageOptions.php +++ b/src/Input/PageOptions.php @@ -47,4 +47,22 @@ public function __construct( $this->operation = $operation; $this->onMinPage = $onMinPage; } + + + /** + * Checks whether the options are set. + * + * @return boolean + */ + public function isEmpty(): bool + { + if ( + ($this->pageIndexes !== null && $this->pageIndexes !== []) || + $this->operation !== KEEP_ONLY || + $this->onMinPage !== 0 + ) { + return false; + } + return true; + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 673cc89e..a1b78e00 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -5,9 +5,11 @@ use Mindee\Error\MindeeHttpClientException; use Mindee\Error\MindeeHttpException; use Mindee\Input\EnqueueAndParseMethodOptions; +use Mindee\Input\PageOptions; use Mindee\Input\PredictMethodOptions; use Mindee\Product\Custom\CustomV1; use Mindee\Product\Invoice\InvoiceV4; +use Mindee\Product\InvoiceSplitter\InvoiceSplitterV1; use PHPUnit\Framework\TestCase; class ClientTest extends TestCase @@ -85,4 +87,26 @@ public function testAsyncWrongPollingDelay() $asyncParseOptions = new EnqueueAndParseMethodOptions(); $asyncParseOptions->setDelaySec(0); } + + public function testPredictOptionsWrongInputType(){ + $pageOptions = new PageOptions([0,1]); + $this->assertFalse($pageOptions->isEmpty()); + $predictOptions = new PredictMethodOptions(); + $predictOptions->setPageOptions($pageOptions); + $urlInputSource = $this->dummyClient->sourceFromUrl("https://dummy"); + $this->expectException(MindeeApiException::class); + $this->dummyClient->parse(InvoiceV4::class, $urlInputSource, $predictOptions); + $this->expectException(MindeeApiException::class); + $this->dummyClient->enqueue(InvoiceSplitterV1::class, $urlInputSource, $predictOptions); + } + + public function testPredictOptionsValidInputType(){ + $predictOptions = new PredictMethodOptions(); + $this->assertTrue($predictOptions->pageOptions->isEmpty()); + $inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf"); + $this->expectException(MindeeHttpClientException::class); + $this->dummyClient->parse(InvoiceV4::class, $inputDoc, $predictOptions); + $this->expectException(MindeeHttpClientException::class); + $this->dummyClient->enqueue(InvoiceSplitterV1::class, $inputDoc, $predictOptions); + } }