-
Notifications
You must be signed in to change notification settings - Fork 1
Add split and classify document support to Client #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| namespace veryfi\classify; | ||
|
|
||
| trait ProcessClassifyDocumentBase64 | ||
| { | ||
| /** | ||
| * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ | ||
| * | ||
| * @param string $base64_encoded_string Buffer string of a file to submit for classify extraction | ||
| * @param string $file_name The file name including the extension | ||
| * @param array $kwargs Additional request parameters | ||
| * @return string Data extracted from the document | ||
| */ | ||
| public function classify_document_from_base64(string $base64_encoded_string, string $file_name, array $kwargs = []): string | ||
| { | ||
| $endpoint_name = '/classify/'; | ||
| $request_arguments = [ | ||
| 'file_name' => $file_name, | ||
| 'file_data' => $base64_encoded_string, | ||
| ]; | ||
| $request_arguments = array_replace($request_arguments, $kwargs); | ||
| return $this->request('POST', $endpoint_name, $request_arguments); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| namespace veryfi\classify; | ||
|
|
||
| trait ProcessClassifyDocumentUrl | ||
| { | ||
| /** | ||
| * Classify document from url and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ | ||
| * | ||
| * @param string|null $file_url Required if file_urls isn't specified. Publicly accessible URL to a file. | ||
| * @param array|null $file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files. | ||
| * @param array $kwargs Additional request parameters | ||
| * @return string Data extracted from the document. | ||
| */ | ||
| public function classify_document_from_url(?string $file_url = null, ?array $file_urls = null, array $kwargs = []): string | ||
| { | ||
| $endpoint_name = '/classify/'; | ||
| $request_arguments = [ | ||
| 'file_url' => $file_url, | ||
| 'file_urls' => $file_urls | ||
| ]; | ||
| $request_arguments = array_replace($request_arguments, $kwargs); | ||
| return $this->request('POST', $endpoint_name, $request_arguments); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
| namespace veryfi\split; | ||
|
|
||
| trait GetSplitDocument | ||
| { | ||
| /** | ||
| * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ | ||
| * | ||
| * @param string $document_id ID of the document you'd like to retrieve | ||
| * @param array $kwargs Additional request parameters | ||
| * @return string Data extracted from the Document | ||
| */ | ||
| public function get_split_document(string $document_id, array $kwargs = []): string | ||
| { | ||
| $endpoint_name = "/documents-set/$document_id/"; | ||
| $request_arguments = ['id' => $document_id]; | ||
| $request_arguments = array_replace($request_arguments, $kwargs); | ||
| return $this->request('GET', $endpoint_name, $request_arguments); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| namespace veryfi\split; | ||
|
|
||
| trait GetSplitDocuments | ||
| { | ||
| /** | ||
| * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ | ||
| * | ||
| * @param int $page The page number. The response is capped to maximum of 50 results per page. | ||
| * @param int $page_size The number of Documents per page. | ||
| * @param array $kwargs Additional request parameters | ||
| * @return string JSON object of previously processed documents | ||
| */ | ||
| public function get_split_documents(int $page = 1, int $page_size = 50, array $kwargs = []): string | ||
| { | ||
| $endpoint_name = '/documents-set/'; | ||
| $request_arguments = [ | ||
| 'page' => $page, | ||
| 'page_size' => $page_size | ||
| ]; | ||
| $request_arguments = array_replace($request_arguments, $kwargs); | ||
| return $this->request('GET', $endpoint_name, $request_arguments); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||
| <?php | ||||||
| namespace veryfi\split; | ||||||
|
|
||||||
| trait ProcessSplitDocumentBase64 | ||||||
| { | ||||||
| /** | ||||||
| * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ | ||||||
| * | ||||||
| * @param string $base64_encoded_string Buffer string of a file to submit for classify extraction | ||||||
|
||||||
| * @param string $base64_encoded_string Buffer string of a file to submit for classify extraction | |
| * @param string $base64_encoded_string Buffer string of a file to submit for split extraction |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| namespace veryfi\split; | ||
|
|
||
| trait ProcessSplitDocumentUrl | ||
| { | ||
| /** | ||
| * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ | ||
| * | ||
| * @param string|null $file_url Required if file_urls isn't specified. Publicly accessible URL to a file. | ||
| * @param array|null $file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files. | ||
| * @param array $kwargs Additional request parameters | ||
| * @return string Data extracted from the document. | ||
| */ | ||
| public function split_document_from_url(?string $file_url = null, ?array $file_urls = null, array $kwargs = []): string | ||
| { | ||
| $endpoint_name = '/documents-set/'; | ||
| $request_arguments = [ | ||
| 'file_url' => $file_url, | ||
| 'file_urls' => $file_urls | ||
| ]; | ||
| $request_arguments = array_replace($request_arguments, $kwargs); | ||
| return $this->request('POST', $endpoint_name, $request_arguments); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| use veryfi\Client; | ||
|
|
||
| require_once __DIR__ . '/ClientTestCase.php'; | ||
|
|
||
| class ClientClassifyDocumentsTest extends ClientTestCase | ||
| { | ||
| public function test_classify_document_from_base64(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/processDocument.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+18
to
+19
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| } | ||
|
|
||
| $file_path = $this->receipt_path; | ||
| $file_name = 'receipt.jpg'; | ||
| $base64_encoded_string = base64_encode(file_get_contents($file_path)); | ||
|
|
||
| $json_response = json_decode($veryfi_client->classify_document_from_base64($base64_encoded_string, $file_name), true); | ||
| $this->assertIsArray($json_response); | ||
| } | ||
|
|
||
| public function test_classify_document_from_url(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/processDocument.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+45
to
+46
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| } | ||
|
|
||
| $url = 'https://raw.githubusercontent.com/veryfi/veryfi-python/master/tests/assets/receipt_public.jpg'; | ||
| $json_response = json_decode($veryfi_client->classify_document_from_url($url), true); | ||
| $this->assertIsArray($json_response); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| use veryfi\Client; | ||
|
|
||
| require_once __DIR__ . '/ClientTestCase.php'; | ||
|
|
||
| class ClientSplitDocumentsTest extends ClientTestCase | ||
| { | ||
| public function test_get_split_documents(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/getDocuments.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+18
to
+19
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| } | ||
| $json_response = json_decode($veryfi_client->get_split_documents(), true); | ||
| $this->assertIsArray($json_response); | ||
| } | ||
|
|
||
| public function test_get_split_document(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/getDocument.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+40
to
+41
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
| $document_id = '125661908'; | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| $documents = json_decode($veryfi_client->get_split_documents(), true); | ||
| if (isset($documents['documents'][0]['id'])) { | ||
| $document_id = $documents['documents'][0]['id']; | ||
| } else { | ||
| $this->markTestSkipped('No documents found to test get_split_document'); | ||
| } | ||
| } | ||
|
|
||
| $json_response = json_decode($veryfi_client->get_split_document($document_id), true); | ||
| $this->assertEquals($document_id, $json_response['id']); | ||
| } | ||
|
|
||
| public function test_split_document_from_base64(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/processDocument.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+70
to
+71
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| } | ||
|
|
||
| $file_path = $this->receipt_path; | ||
| $file_name = 'receipt.jpg'; | ||
| $base64_encoded_string = base64_encode(file_get_contents($file_path)); | ||
|
|
||
| $json_response = json_decode($veryfi_client->split_document_from_base64($base64_encoded_string, $file_name), true); | ||
| $this->assertIsArray($json_response); | ||
| } | ||
|
|
||
| public function test_split_document_from_url(): void | ||
| { | ||
| if ($this->mock_responses) { | ||
| $veryfi_client = $this->getMockBuilder(Client::class) | ||
| ->onlyMethods(['exec_curl']) | ||
| ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) | ||
| ->getMock(); | ||
|
|
||
| $file_path = __DIR__ . '/resources/processDocument.json'; | ||
| $file = fopen($file_path, 'r'); | ||
| $file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8'); | ||
|
Comment on lines
+97
to
+98
|
||
| $veryfi_client->expects($this->once()) | ||
| ->method('exec_curl') | ||
| ->willReturn($file_data); | ||
|
|
||
| } else { | ||
| $veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key); | ||
| } | ||
|
|
||
| $url = 'https://raw.githubusercontent.com/veryfi/veryfi-python/master/tests/assets/receipt_public.jpg'; | ||
| $json_response = json_decode($veryfi_client->split_document_from_url($url), true); | ||
| $this->assertIsArray($json_response); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation incorrectly states 'from url' but this method processes documents from base64-encoded data. Update to 'Split document PDF from base64 and extract all the fields from it.'