1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud; |
16
|
|
|
|
17
|
|
|
use GuzzleHttp\Psr7\Response; |
18
|
|
|
use GuzzleHttp\RequestOptions; |
19
|
|
|
use TxTextControl\ReportingCloud\Assert\Assert; |
20
|
|
|
use TxTextControl\ReportingCloud\Filter\Filter; |
21
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
22
|
|
|
use TxTextControl\ReportingCloud\StatusCode\StatusCode; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Trait PostTrait |
26
|
|
|
* |
27
|
|
|
* @package TxTextControl\ReportingCloud |
28
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
29
|
|
|
*/ |
30
|
|
|
trait PostTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Abstract Methods |
34
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Construct URI with version number |
39
|
|
|
* |
40
|
|
|
* @param string $uri URI |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract protected function uri(string $uri): string; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Request the URI with options |
48
|
|
|
* |
49
|
|
|
* @param string $method HTTP method |
50
|
|
|
* @param string $uri URI |
51
|
|
|
* @param array $options Options |
52
|
|
|
* |
53
|
|
|
* @return mixed|null|\Psr\Http\Message\ResponseInterface |
54
|
|
|
* |
55
|
|
|
* @throws RuntimeException |
56
|
|
|
*/ |
57
|
|
|
abstract protected function request(string $method, string $uri, array $options); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
61
|
|
|
* |
62
|
|
|
* @param array $array FindAndReplaceData array |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
abstract protected function buildFindAndReplaceDataArray(array $array): array; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Using passed mergeSettings array, build array for backend |
70
|
|
|
* |
71
|
|
|
* @param array $array MergeSettings array |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
abstract protected function buildMergeSettingsArray(array $array): array; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Using passed documentsData array, build array for backend |
79
|
|
|
* |
80
|
|
|
* @param array $array AppendDocument array |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
abstract protected function buildDocumentsArray(array $array): array; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Using passed documentsSettings array, build array for backend |
88
|
|
|
* |
89
|
|
|
* @param array $array |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
abstract protected function buildDocumentSettingsArray(array $array): array; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Using the passed propertyMap, recursively build array |
97
|
|
|
* |
98
|
|
|
* @param array $array Array |
99
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* POST Methods |
107
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
108
|
|
|
*/ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Upload a base64 encoded template to template storage |
112
|
|
|
* |
113
|
|
|
* @param string $data Template encoded as base64 |
114
|
|
|
* @param string $templateName Template name |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
118
|
|
|
*/ |
119
|
9 |
|
public function uploadTemplateFromBase64(string $data, string $templateName): bool |
120
|
|
|
{ |
121
|
9 |
|
Assert::assertBase64Data($data); |
122
|
9 |
|
Assert::assertTemplateName($templateName); |
123
|
|
|
|
124
|
|
|
$query = [ |
125
|
9 |
|
'templateName' => $templateName, |
126
|
|
|
]; |
127
|
|
|
|
128
|
9 |
|
$result = $this->post('/templates/upload', $query, $data); |
129
|
|
|
|
130
|
9 |
|
return (null === $result) ? true : false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Upload a template to template storage |
135
|
|
|
* |
136
|
|
|
* @param string $templateFilename Template name |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
140
|
|
|
*/ |
141
|
13 |
|
public function uploadTemplate(string $templateFilename): bool |
142
|
|
|
{ |
143
|
13 |
|
Assert::assertTemplateExtension($templateFilename); |
144
|
10 |
|
Assert::filenameExists($templateFilename); |
145
|
|
|
|
146
|
9 |
|
$templateName = basename($templateFilename); |
147
|
|
|
|
148
|
9 |
|
$data = file_get_contents($templateFilename); |
149
|
9 |
|
$data = base64_encode($data); |
150
|
|
|
|
151
|
9 |
|
return $this->uploadTemplateFromBase64($data, $templateName); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Convert a document on the local file system to a different format |
156
|
|
|
* |
157
|
|
|
* @param string $documentFilename Document filename |
158
|
|
|
* @param string $returnFormat Return format |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
162
|
|
|
*/ |
163
|
6 |
|
public function convertDocument(string $documentFilename, string $returnFormat): string |
164
|
|
|
{ |
165
|
6 |
|
Assert::assertDocumentExtension($documentFilename); |
166
|
3 |
|
Assert::filenameExists($documentFilename); |
167
|
2 |
|
Assert::assertReturnFormat($returnFormat); |
168
|
|
|
|
169
|
|
|
$query = [ |
170
|
1 |
|
'returnFormat' => $returnFormat, |
171
|
|
|
]; |
172
|
|
|
|
173
|
1 |
|
$json = file_get_contents($documentFilename); |
174
|
1 |
|
$json = base64_encode($json); |
175
|
|
|
|
176
|
1 |
|
$result = $this->post('/document/convert', $query, $json); |
177
|
|
|
|
178
|
1 |
|
if (!is_string($result)) { |
179
|
|
|
return ''; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return (string) base64_decode($result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Merge data into a template and return an array of binary data. |
187
|
|
|
* Each record in the array is the binary data of one document |
188
|
|
|
* |
189
|
|
|
* @param array $mergeData Array of merge data |
190
|
|
|
* @param string $returnFormat Return format |
191
|
|
|
* @param string|null $templateName Template name |
192
|
|
|
* @param string|null $templateFilename Template filename on local file system |
193
|
|
|
* @param bool|null $append Append flag |
194
|
|
|
* @param array|null $mergeSettings Array of merge settings |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
198
|
|
|
*/ |
199
|
12 |
|
public function mergeDocument( |
200
|
|
|
array $mergeData, |
201
|
|
|
string $returnFormat, |
202
|
|
|
?string $templateName = null, |
203
|
|
|
?string $templateFilename = null, |
204
|
|
|
?bool $append = null, |
205
|
|
|
?array $mergeSettings = null |
206
|
|
|
): array { |
207
|
|
|
|
208
|
12 |
|
$ret = []; |
209
|
|
|
|
210
|
12 |
|
$query = []; |
211
|
12 |
|
$json = []; |
212
|
|
|
|
213
|
12 |
|
Assert::isArray($mergeData); |
214
|
12 |
|
$json['mergeData'] = $mergeData; |
215
|
|
|
|
216
|
12 |
|
Assert::assertReturnFormat($returnFormat); |
217
|
11 |
|
$query['returnFormat'] = $returnFormat; |
218
|
|
|
|
219
|
11 |
|
if (null !== $templateName) { |
220
|
2 |
|
Assert::assertTemplateName($templateName); |
221
|
1 |
|
$query['templateName'] = $templateName; |
222
|
|
|
} |
223
|
|
|
|
224
|
10 |
|
if (null !== $templateFilename) { |
225
|
9 |
|
Assert::assertTemplateExtension($templateFilename); |
226
|
6 |
|
Assert::filenameExists($templateFilename); |
227
|
5 |
|
$template = file_get_contents($templateFilename); |
228
|
5 |
|
$template = base64_encode($template); |
229
|
5 |
|
$json['template'] = $template; |
230
|
|
|
} |
231
|
|
|
|
232
|
6 |
|
if (null !== $append) { |
233
|
5 |
|
Assert::boolean($append); |
234
|
5 |
|
$query['append'] = Filter::filterBooleanToString($append); |
235
|
|
|
} |
236
|
|
|
|
237
|
6 |
|
if (is_array($mergeSettings)) { |
238
|
5 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
239
|
|
|
} |
240
|
|
|
|
241
|
3 |
|
$result = $this->post('/document/merge', $query, $json); |
242
|
|
|
|
243
|
3 |
|
if (is_array($result) && count($result) > 0) { |
244
|
3 |
|
$ret = array_map('base64_decode', $result); |
245
|
|
|
} |
246
|
|
|
|
247
|
3 |
|
return $ret; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Combine documents to appending them, divided by a new section, paragraph or nothing |
252
|
|
|
* |
253
|
|
|
* @param array $documentsData |
254
|
|
|
* @param string $returnFormat |
255
|
|
|
* @param array|null $documentSettings |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
259
|
|
|
*/ |
260
|
1 |
|
public function appendDocument( |
261
|
|
|
array $documentsData, |
262
|
|
|
string $returnFormat, |
263
|
|
|
?array $documentSettings = null |
264
|
|
|
): string { |
265
|
|
|
|
266
|
1 |
|
$query = []; |
267
|
1 |
|
$json = []; |
268
|
|
|
|
269
|
1 |
|
Assert::isArray($documentsData); |
270
|
1 |
|
$json['documents'] = $this->buildDocumentsArray($documentsData); |
271
|
|
|
|
272
|
1 |
|
Assert::assertReturnFormat($returnFormat); |
273
|
1 |
|
$query['returnFormat'] = $returnFormat; |
274
|
|
|
|
275
|
1 |
|
if (is_array($documentSettings)) { |
276
|
1 |
|
$json['documentSettings'] = $this->buildDocumentSettingsArray($documentSettings); |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
$result = $this->post('/document/append', $query, $json); |
280
|
|
|
|
281
|
1 |
|
if (!is_string($result)) { |
282
|
|
|
return ''; |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
return (string) base64_decode($result); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Perform find and replace in document and return binary data. |
290
|
|
|
* |
291
|
|
|
* @param array $findAndReplaceData Array of find and replace data |
292
|
|
|
* @param string $returnFormat Return format |
293
|
|
|
* @param string|null $templateName Template name |
294
|
|
|
* @param string|null $templateFilename Template filename on local file system |
295
|
|
|
* @param array|null $mergeSettings Array of merge settings |
296
|
|
|
* |
297
|
|
|
* @return string |
298
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
299
|
|
|
*/ |
300
|
10 |
|
public function findAndReplaceDocument( |
301
|
|
|
array $findAndReplaceData, |
302
|
|
|
string $returnFormat, |
303
|
|
|
?string $templateName = null, |
304
|
|
|
?string $templateFilename = null, |
305
|
|
|
?array $mergeSettings = null |
306
|
|
|
): string { |
307
|
|
|
|
308
|
10 |
|
$query = []; |
309
|
10 |
|
$json = []; |
310
|
|
|
|
311
|
10 |
|
Assert::isArray($findAndReplaceData); |
312
|
10 |
|
$json['findAndReplaceData'] = $this->buildFindAndReplaceDataArray($findAndReplaceData); |
313
|
|
|
|
314
|
10 |
|
Assert::assertReturnFormat($returnFormat); |
315
|
9 |
|
$query['returnFormat'] = $returnFormat; |
316
|
|
|
|
317
|
9 |
|
if (null !== $templateName) { |
318
|
2 |
|
Assert::assertTemplateName($templateName); |
319
|
1 |
|
$query['templateName'] = $templateName; |
320
|
|
|
} |
321
|
|
|
|
322
|
8 |
|
if (null !== $templateFilename) { |
323
|
7 |
|
Assert::assertTemplateExtension($templateFilename); |
324
|
4 |
|
Assert::filenameExists($templateFilename); |
325
|
3 |
|
$template = file_get_contents($templateFilename); |
326
|
3 |
|
$template = base64_encode($template); |
327
|
3 |
|
$json['template'] = $template; |
328
|
|
|
} |
329
|
|
|
|
330
|
4 |
|
if (is_array($mergeSettings)) { |
331
|
4 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
332
|
|
|
} |
333
|
|
|
|
334
|
2 |
|
$result = $this->post('/document/findandreplace', $query, $json); |
335
|
|
|
|
336
|
2 |
|
if (!is_string($result)) { |
337
|
|
|
return ''; |
338
|
|
|
} |
339
|
|
|
|
340
|
2 |
|
return (string) base64_decode($result); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Execute a POST request via REST client |
345
|
|
|
* |
346
|
|
|
* @param string $uri URI |
347
|
|
|
* @param array $query Query |
348
|
|
|
* @param string|array $json JSON |
349
|
|
|
* |
350
|
|
|
* @return array|string|null |
351
|
|
|
*/ |
352
|
14 |
|
private function post(string $uri, array $query = [], $json = '') |
353
|
|
|
{ |
354
|
|
|
$options = [ |
355
|
14 |
|
RequestOptions::QUERY => $query, |
356
|
14 |
|
RequestOptions::JSON => $json, |
357
|
|
|
]; |
358
|
|
|
|
359
|
|
|
$statusCodes = [ |
360
|
14 |
|
StatusCode::OK, |
361
|
14 |
|
StatusCode::CREATED, |
362
|
|
|
]; |
363
|
|
|
|
364
|
14 |
|
$response = $this->request('POST', $this->uri($uri), $options); |
365
|
|
|
|
366
|
14 |
|
if (!$response instanceof Response) { |
367
|
|
|
return ''; |
368
|
|
|
} |
369
|
|
|
|
370
|
14 |
|
if (!in_array($response->getStatusCode(), $statusCodes)) { |
371
|
|
|
return ''; |
372
|
|
|
} |
373
|
|
|
|
374
|
14 |
|
return json_decode($response->getBody()->getContents(), true); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|