Skip to content

Commit 017c599

Browse files
authored
Merge pull request #19 from veryfi/feature/add-classify-split
Add split and classify document support to Client
2 parents 192c917 + 7c5b6c6 commit 017c599

10 files changed

Lines changed: 328 additions & 9 deletions

src/veryfi/Client.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
use veryfi\documents\lineitems\GetLineItem;
7373
use veryfi\documents\lineitems\GetLineItems;
7474
use veryfi\documents\lineitems\UpdateLineItem;
75+
use veryfi\split\GetSplitDocument;
76+
use veryfi\split\GetSplitDocuments;
77+
use veryfi\split\ProcessSplitDocumentBase64;
78+
use veryfi\split\ProcessSplitDocumentUrl;
79+
use veryfi\classify\ProcessClassifyDocumentBase64;
80+
use veryfi\classify\ProcessClassifyDocumentUrl;
7581

7682

7783

@@ -143,14 +149,15 @@ class Client
143149
* @param string $api_version Api version to use Veryfi, currently v8
144150
* @param int $api_timeout Api timeout for call Veryfi api, by default 120
145151
*/
146-
public function __construct(string $client_id,
147-
string $client_secret,
148-
string $username,
149-
string $api_key,
150-
string $base_url = 'https://api.veryfi.com/api/',
151-
string $api_version = 'v8',
152-
int $api_timeout = 120)
153-
{
152+
public function __construct(
153+
string $client_id,
154+
string $client_secret,
155+
string $username,
156+
string $api_key,
157+
string $base_url = 'https://api.veryfi.com/api/',
158+
string $api_version = 'v8',
159+
int $api_timeout = 120
160+
) {
154161
$this->client_id = $client_id;
155162
$this->client_secret = $client_secret;
156163
$this->username = $username;
@@ -173,5 +180,7 @@ public function __construct(string $client_id,
173180
use DeleteW8BENE, GetW8BENE, GetW8BENEs, ProcessW8BENE, ProcessW8BENEBase64, ProcessW8BENEUrl;
174181
use DeleteW9, GetW9, GetW9s, ProcessW9, ProcessW9Base64, ProcessW9Url;
175182
use AddLineItem, DeleteLineItem, DeleteLineItems, GetLineItem, GetLineItems, UpdateLineItem;
183+
use GetSplitDocument, GetSplitDocuments, ProcessSplitDocumentBase64, ProcessSplitDocumentUrl;
184+
use ProcessClassifyDocumentBase64, ProcessClassifyDocumentUrl;
176185

177186
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace veryfi\classify;
3+
4+
trait ProcessClassifyDocumentBase64
5+
{
6+
/**
7+
* Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/
8+
*
9+
* @param string $base64_encoded_string Buffer string of a file to submit for classify extraction
10+
* @param string $file_name The file name including the extension
11+
* @param array $kwargs Additional request parameters
12+
* @return string Data extracted from the document
13+
*/
14+
public function classify_document_from_base64(string $base64_encoded_string, string $file_name, array $kwargs = []): string
15+
{
16+
$endpoint_name = '/classify/';
17+
$request_arguments = [
18+
'file_name' => $file_name,
19+
'file_data' => $base64_encoded_string,
20+
];
21+
$request_arguments = array_replace($request_arguments, $kwargs);
22+
return $this->request('POST', $endpoint_name, $request_arguments);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace veryfi\classify;
3+
4+
trait ProcessClassifyDocumentUrl
5+
{
6+
/**
7+
* Classify document from url and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/
8+
*
9+
* @param string|null $file_url Required if file_urls isn't specified. Publicly accessible URL to a file.
10+
* @param array|null $file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files.
11+
* @param array $kwargs Additional request parameters
12+
* @return string Data extracted from the document.
13+
*/
14+
public function classify_document_from_url(?string $file_url = null, ?array $file_urls = null, array $kwargs = []): string
15+
{
16+
$endpoint_name = '/classify/';
17+
$request_arguments = [
18+
'file_url' => $file_url,
19+
'file_urls' => $file_urls
20+
];
21+
$request_arguments = array_replace($request_arguments, $kwargs);
22+
return $this->request('POST', $endpoint_name, $request_arguments);
23+
}
24+
}

src/veryfi/client/GetHeaders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait GetHeaders
99
private function get_headers(): array
1010
{
1111
return array(
12-
'User-Agent' => 'php veryfi-php/1.1.0',
12+
'User-Agent' => 'php veryfi-php/1.0.5',
1313
'Accept' => 'application/json',
1414
'Content-Type' => 'application/json',
1515
'Client-ID' => $this->client_id,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace veryfi\split;
3+
4+
trait GetSplitDocument
5+
{
6+
/**
7+
* 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/
8+
*
9+
* @param string $document_id ID of the document you'd like to retrieve
10+
* @param array $kwargs Additional request parameters
11+
* @return string Data extracted from the Document
12+
*/
13+
public function get_split_document(string $document_id, array $kwargs = []): string
14+
{
15+
$endpoint_name = "/documents-set/$document_id/";
16+
$request_arguments = ['id' => $document_id];
17+
$request_arguments = array_replace($request_arguments, $kwargs);
18+
return $this->request('GET', $endpoint_name, $request_arguments);
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace veryfi\split;
3+
4+
trait GetSplitDocuments
5+
{
6+
/**
7+
* 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/
8+
*
9+
* @param int $page The page number. The response is capped to maximum of 50 results per page.
10+
* @param int $page_size The number of Documents per page.
11+
* @param array $kwargs Additional request parameters
12+
* @return string JSON object of previously processed documents
13+
*/
14+
public function get_split_documents(int $page = 1, int $page_size = 50, array $kwargs = []): string
15+
{
16+
$endpoint_name = '/documents-set/';
17+
$request_arguments = [
18+
'page' => $page,
19+
'page_size' => $page_size
20+
];
21+
$request_arguments = array_replace($request_arguments, $kwargs);
22+
return $this->request('GET', $endpoint_name, $request_arguments);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace veryfi\split;
3+
4+
trait ProcessSplitDocumentBase64
5+
{
6+
/**
7+
* Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
8+
*
9+
* @param string $base64_encoded_string Buffer string of a file to submit for classify extraction
10+
* @param string $file_name The file name including the extension
11+
* @param array $kwargs Additional request parameters
12+
* @return string Data extracted from the document
13+
*/
14+
public function split_document_from_base64(string $base64_encoded_string, string $file_name, array $kwargs = []): string
15+
{
16+
$endpoint_name = '/documents-set/';
17+
$request_arguments = [
18+
'file_name' => $file_name,
19+
'file_data' => $base64_encoded_string,
20+
];
21+
$request_arguments = array_replace($request_arguments, $kwargs);
22+
return $this->request('POST', $endpoint_name, $request_arguments);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace veryfi\split;
3+
4+
trait ProcessSplitDocumentUrl
5+
{
6+
/**
7+
* Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
8+
*
9+
* @param string|null $file_url Required if file_urls isn't specified. Publicly accessible URL to a file.
10+
* @param array|null $file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files.
11+
* @param array $kwargs Additional request parameters
12+
* @return string Data extracted from the document.
13+
*/
14+
public function split_document_from_url(?string $file_url = null, ?array $file_urls = null, array $kwargs = []): string
15+
{
16+
$endpoint_name = '/documents-set/';
17+
$request_arguments = [
18+
'file_url' => $file_url,
19+
'file_urls' => $file_urls
20+
];
21+
$request_arguments = array_replace($request_arguments, $kwargs);
22+
return $this->request('POST', $endpoint_name, $request_arguments);
23+
}
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use veryfi\Client;
4+
5+
require_once __DIR__ . '/ClientTestCase.php';
6+
7+
class ClientClassifyDocumentsTest extends ClientTestCase
8+
{
9+
public function test_classify_document_from_base64(): void
10+
{
11+
if ($this->mock_responses) {
12+
$veryfi_client = $this->getMockBuilder(Client::class)
13+
->onlyMethods(['exec_curl'])
14+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
15+
->getMock();
16+
17+
$file_path = __DIR__ . '/resources/processDocument.json';
18+
$file = fopen($file_path, 'r');
19+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
20+
$veryfi_client->expects($this->once())
21+
->method('exec_curl')
22+
->willReturn($file_data);
23+
24+
} else {
25+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
26+
}
27+
28+
$file_path = $this->receipt_path;
29+
$file_name = 'receipt.jpg';
30+
$base64_encoded_string = base64_encode(file_get_contents($file_path));
31+
32+
$json_response = json_decode($veryfi_client->classify_document_from_base64($base64_encoded_string, $file_name), true);
33+
$this->assertIsArray($json_response);
34+
}
35+
36+
public function test_classify_document_from_url(): void
37+
{
38+
if ($this->mock_responses) {
39+
$veryfi_client = $this->getMockBuilder(Client::class)
40+
->onlyMethods(['exec_curl'])
41+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
42+
->getMock();
43+
44+
$file_path = __DIR__ . '/resources/processDocument.json';
45+
$file = fopen($file_path, 'r');
46+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
47+
$veryfi_client->expects($this->once())
48+
->method('exec_curl')
49+
->willReturn($file_data);
50+
51+
} else {
52+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
53+
}
54+
55+
$url = 'https://raw.githubusercontent.com/veryfi/veryfi-python/master/tests/assets/receipt_public.jpg';
56+
$json_response = json_decode($veryfi_client->classify_document_from_url($url), true);
57+
$this->assertIsArray($json_response);
58+
}
59+
}

tests/ClientSplitDocumentsTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
use veryfi\Client;
4+
5+
require_once __DIR__ . '/ClientTestCase.php';
6+
7+
class ClientSplitDocumentsTest extends ClientTestCase
8+
{
9+
public function test_get_split_documents(): void
10+
{
11+
if ($this->mock_responses) {
12+
$veryfi_client = $this->getMockBuilder(Client::class)
13+
->onlyMethods(['exec_curl'])
14+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
15+
->getMock();
16+
17+
$file_path = __DIR__ . '/resources/getDocuments.json';
18+
$file = fopen($file_path, 'r');
19+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
20+
$veryfi_client->expects($this->once())
21+
->method('exec_curl')
22+
->willReturn($file_data);
23+
24+
} else {
25+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
26+
}
27+
$json_response = json_decode($veryfi_client->get_split_documents(), true);
28+
$this->assertIsArray($json_response);
29+
}
30+
31+
public function test_get_split_document(): void
32+
{
33+
if ($this->mock_responses) {
34+
$veryfi_client = $this->getMockBuilder(Client::class)
35+
->onlyMethods(['exec_curl'])
36+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
37+
->getMock();
38+
39+
$file_path = __DIR__ . '/resources/getDocument.json';
40+
$file = fopen($file_path, 'r');
41+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
42+
$veryfi_client->expects($this->once())
43+
->method('exec_curl')
44+
->willReturn($file_data);
45+
$document_id = '125661908';
46+
47+
} else {
48+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
49+
$documents = json_decode($veryfi_client->get_split_documents(), true);
50+
if (isset($documents['documents'][0]['id'])) {
51+
$document_id = $documents['documents'][0]['id'];
52+
} else {
53+
$this->markTestSkipped('No documents found to test get_split_document');
54+
}
55+
}
56+
57+
$json_response = json_decode($veryfi_client->get_split_document($document_id), true);
58+
$this->assertEquals($document_id, $json_response['id']);
59+
}
60+
61+
public function test_split_document_from_base64(): void
62+
{
63+
if ($this->mock_responses) {
64+
$veryfi_client = $this->getMockBuilder(Client::class)
65+
->onlyMethods(['exec_curl'])
66+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
67+
->getMock();
68+
69+
$file_path = __DIR__ . '/resources/processDocument.json';
70+
$file = fopen($file_path, 'r');
71+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
72+
$veryfi_client->expects($this->once())
73+
->method('exec_curl')
74+
->willReturn($file_data);
75+
76+
} else {
77+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
78+
}
79+
80+
$file_path = $this->receipt_path;
81+
$file_name = 'receipt.jpg';
82+
$base64_encoded_string = base64_encode(file_get_contents($file_path));
83+
84+
$json_response = json_decode($veryfi_client->split_document_from_base64($base64_encoded_string, $file_name), true);
85+
$this->assertIsArray($json_response);
86+
}
87+
88+
public function test_split_document_from_url(): void
89+
{
90+
if ($this->mock_responses) {
91+
$veryfi_client = $this->getMockBuilder(Client::class)
92+
->onlyMethods(['exec_curl'])
93+
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
94+
->getMock();
95+
96+
$file_path = __DIR__ . '/resources/processDocument.json';
97+
$file = fopen($file_path, 'r');
98+
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
99+
$veryfi_client->expects($this->once())
100+
->method('exec_curl')
101+
->willReturn($file_data);
102+
103+
} else {
104+
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
105+
}
106+
107+
$url = 'https://raw.githubusercontent.com/veryfi/veryfi-python/master/tests/assets/receipt_public.jpg';
108+
$json_response = json_decode($veryfi_client->split_document_from_url($url), true);
109+
$this->assertIsArray($json_response);
110+
}
111+
}

0 commit comments

Comments
 (0)