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
32 changes: 18 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/Input/PageOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}