Skip to content

Commit 8d34069

Browse files
optional parameters support
1 parent 89af154 commit 8d34069

File tree

1 file changed

+165
-122
lines changed

1 file changed

+165
-122
lines changed

Tabscanner/Api.php

Lines changed: 165 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,157 +6,200 @@
66

77
class Api
88
{
9-
private $api_key;
10-
private $api_url;
9+
private $api_key;
10+
private $api_url;
1111

12-
public function __construct($api_key, $api_url = 'https://api.tabscanner.com/')
12+
public function __construct($api_key, $api_url = 'https://api.tabscanner.com/')
1313
{
1414
$this->api_key = $api_key;
1515
$this->api_url = $api_url;
1616
}
1717

18-
public function upload($file, $user_id = 0)
18+
public function upload($file, $user_id = 0, $decimalPlaces = null, $language = null, $cents = null, $lineExtract = true, $documentType = 'auto', $testMode = null)
1919
{
20-
$client = new Client();
20+
$client = new Client();
2121
$api_upload_url = $this->api_url . $this->api_key . '/process';
22-
$file_type = gettype($file);
23-
$validate = $this->validate($file);
24-
25-
if ($validate['error']) {
26-
$response = [
27-
'message' => $validate['message'],
28-
'status' => 'failed',
29-
];
30-
31-
return $response;
32-
}
33-
34-
switch ($file_type) {
35-
//if from form upload
36-
case 'array':
37-
$filename = $file['name'];
38-
$content = fopen($file['tmp_name'], 'r');
39-
break;
40-
41-
//filepath
42-
case 'string':
43-
$filename = basename($file);
44-
$content = fopen($file, 'r');
45-
break;
22+
$file_type = gettype($file);
23+
$validate = $this->validate($file);
24+
25+
if ($validate['error']) {
26+
$response = [
27+
'message' => $validate['message'],
28+
'status' => 'failed',
29+
];
30+
31+
return $response;
32+
}
33+
34+
switch ($file_type) {
35+
//if from form upload
36+
case 'array':
37+
$filename = $file['name'];
38+
$content = fopen($file['tmp_name'], 'r');
39+
break;
40+
41+
//filepath
42+
case 'string':
43+
$filename = basename($file);
44+
$content = fopen($file, 'r');
45+
break;
4646

4747
case 'object':
4848
$filename = $file->getClientOriginalName();
4949
$content = fopen($file->getPathname(), 'r');
5050
break;
51-
}
52-
53-
$response = $client->request('POST', $api_upload_url, [
54-
'multipart' => [
55-
[
56-
'name' => 'file',
57-
'filename' => $filename,
58-
'contents' => $content
59-
],
60-
[
61-
'name' => 'lineExtract',
62-
'contents' => true
63-
],
64-
[
65-
'name' => 'dashboardUserId',
66-
'contents' => $user_id
67-
]
51+
}
52+
53+
$payload = [
54+
[
55+
'name' => 'file',
56+
'filename' => $filename,
57+
'contents' => $content
6858
]
59+
];
60+
61+
if (isset($user_id)) {
62+
array_push($payload, [
63+
'name' => 'dashboardUserId',
64+
'contents' => (int) $user_id
65+
]);
66+
}
67+
68+
if (isset($decimalPlaces)) {
69+
array_push($payload, [
70+
'name' => 'decimalPlaces',
71+
'contents' => (int) $decimalPlaces
72+
]);
73+
}
74+
75+
if (isset($language)) {
76+
array_push($payload, [
77+
'name' => 'language',
78+
'contents' => $language
79+
]);
80+
}
81+
82+
if (isset($cents)) {
83+
array_push($payload, [
84+
'name' => 'cents',
85+
'contents' => (bool) $cents
86+
]);
87+
}
88+
89+
if (isset($lineExtract)) {
90+
array_push($payload, [
91+
'name' => 'lineExtract',
92+
'contents' => (bool) $lineExtract
93+
]);
94+
}
95+
96+
if (isset($documentType)) {
97+
array_push($payload, [
98+
'name' => 'documentType',
99+
'contents' => $documentType
100+
]);
101+
}
102+
103+
if (isset($testMode)) {
104+
array_push($payload, [
105+
'name' => 'testMode',
106+
'contents' => (bool) $testMode
107+
]);
108+
}
109+
110+
$response = $client->request('POST', $api_upload_url, [
111+
'multipart' => $payload
69112
]);
70113

71-
$response_decoded = json_decode($response->getBody(), true);
114+
$response_decoded = json_decode($response->getBody(), true);
72115

73-
return $response_decoded;
116+
return $response_decoded;
74117
}
75118

76119
public function result($token)
77120
{
78-
$client = new Client();
121+
$client = new Client();
79122

80-
$api_result_url = $this->api_url . $this->api_key . '/result/' . $token;
123+
$api_result_url = $this->api_url . $this->api_key . '/result/' . $token;
81124

82-
$response = $client->get($api_result_url);
83-
$response_decoded = json_decode($response->getBody(), true);
125+
$response = $client->get($api_result_url);
126+
$response_decoded = json_decode($response->getBody(), true);
84127

85-
return $response_decoded;
128+
return $response_decoded;
86129
}
87130

88131
public function validate($file)
89132
{
90-
$file_type = gettype($file);
91-
$allowed = ['gif', 'png', 'jpg', 'jpeg'];
92-
93-
switch ($file_type) {
94-
//if from form upload
95-
case 'array':
96-
if (!isset($file['name']) || !isset($file['tmp_name']) || !isset($file['type'])) {
97-
return [
98-
'error' => true,
99-
'message' => 'Missing key required from array',
100-
];
101-
}
102-
103-
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
104-
105-
if (!in_array(strtolower($ext), $allowed)) {
106-
return [
107-
'error' => true,
108-
'message' => 'File type not supported',
109-
];
110-
}
111-
112-
$content = fopen($file['tmp_name'], 'r');
113-
114-
if (!$content) {
115-
return [
116-
'error' => true,
117-
'message' => 'Missing file content',
118-
];
119-
}
120-
121-
break;
122-
123-
//filepath
124-
case 'string':
125-
$ext = pathinfo(basename($file), PATHINFO_EXTENSION);
126-
127-
if (!in_array(strtolower($ext), $allowed)) {
128-
return [
129-
'error' => true,
130-
'message' => 'File type not supported',
131-
];
132-
}
133-
134-
$content = fopen($file, 'r');
135-
136-
if (!$content) {
137-
return [
138-
'error' => true,
139-
'message' => 'Missing file content',
140-
];
141-
}
142-
143-
break;
133+
$file_type = gettype($file);
134+
$allowed = ['gif', 'png', 'jpg', 'jpeg'];
135+
136+
switch ($file_type) {
137+
//if from form upload
138+
case 'array':
139+
if (!isset($file['name']) || !isset($file['tmp_name']) || !isset($file['type'])) {
140+
return [
141+
'error' => true,
142+
'message' => 'Missing key required from array',
143+
];
144+
}
145+
146+
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
147+
148+
if (!in_array(strtolower($ext), $allowed)) {
149+
return [
150+
'error' => true,
151+
'message' => 'File type not supported',
152+
];
153+
}
154+
155+
$content = fopen($file['tmp_name'], 'r');
156+
157+
if (!$content) {
158+
return [
159+
'error' => true,
160+
'message' => 'Missing file content',
161+
];
162+
}
163+
164+
break;
165+
166+
//filepath
167+
case 'string':
168+
$ext = pathinfo(basename($file), PATHINFO_EXTENSION);
169+
170+
if (!in_array(strtolower($ext), $allowed)) {
171+
return [
172+
'error' => true,
173+
'message' => 'File type not supported',
174+
];
175+
}
176+
177+
$content = fopen($file, 'r');
178+
179+
if (!$content) {
180+
return [
181+
'error' => true,
182+
'message' => 'Missing file content',
183+
];
184+
}
185+
186+
break;
144187

145188
case 'object':
146189
break;
147-
148-
default:
149-
return [
150-
'error' => true,
151-
'message' => 'Input parameter not supported',
152-
];
153-
154-
break;
155-
156-
return [
157-
'error' => false,
158-
'message' => 'File looks clean',
159-
];
160-
}
190+
191+
default:
192+
return [
193+
'error' => true,
194+
'message' => 'Input parameter not supported',
195+
];
196+
197+
break;
198+
199+
return [
200+
'error' => false,
201+
'message' => 'File looks clean',
202+
];
203+
}
161204
}
162-
}
205+
}

0 commit comments

Comments
 (0)