This repository was archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPostTrait.php
More file actions
509 lines (426 loc) · 15.2 KB
/
Copy pathPostTrait.php
File metadata and controls
509 lines (426 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<?php
declare(strict_types=1);
/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://git.io/Jejj2 for the canonical source repository
* @license https://git.io/Jejjr
* @copyright © 2022 Text Control GmbH
*/
namespace TxTextControl\ReportingCloud;
use Ctw\Http\HttpMethod;
use Ctw\Http\HttpStatus;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use TxTextControl\ReportingCloud\Assert\Assert;
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\Exception\RuntimeException;
use TxTextControl\ReportingCloud\Filter\Filter;
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
use TxTextControl\ReportingCloud\PropertyMap\ModifiedDocument as ModifiedDocumentPropertyMap;
use TxTextControl\ReportingCloud\PropertyMap\TrackedChanges as TrackedChangesPropertyMap;
use TxTextControl\ReportingCloud\Stdlib\FileUtils;
/**
* Trait PostTrait
*
* @package TxTextControl\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
trait PostTrait
{
// <editor-fold desc="Abstract methods">
/**
* Construct URI with version number
*
* @param string $uri URI
*
* @return string
*/
abstract protected function uri(string $uri): string;
/**
* Request the URI with options
*
* @param string $method HTTP method
* @param string $uri URI
* @param array $options Options
*
* @return ResponseInterface
* @throws RuntimeException
*/
abstract protected function request(string $method, string $uri, array $options): ResponseInterface;
/**
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
*
* @param array $array FindAndReplaceData array
*
* @return array
*/
abstract protected function buildFindAndReplaceDataArray(array $array): array;
/**
* Using passed mergeSettings array, build array for backend
*
* @param array $array MergeSettings array
*
* @return array
*/
abstract protected function buildMergeSettingsArray(array $array): array;
/**
* Using passed documentsData array, build array for backend
*
* @param array $array AppendDocument array
*
* @return array
*/
abstract protected function buildDocumentsArray(array $array): array;
/**
* Using passed documentsSettings array, build array for backend
*
* @param array $array
*
* @return array
*/
abstract protected function buildDocumentSettingsArray(array $array): array;
/**
* Using the passed propertyMap, recursively build array
*
* @param array $array Array
* @param PropertyMap $propertyMap PropertyMap
*
* @return array
*/
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
// </editor-fold>
// <editor-fold desc="Methods">
/**
* Upload a base64 encoded template to template storage
*
* @param string $data Template encoded as base64
* @param string $templateName Template name
*
* @return bool
* @throws InvalidArgumentException
*/
public function uploadTemplateFromBase64(string $data, string $templateName): bool
{
Assert::assertBase64Data($data);
Assert::assertTemplateName($templateName);
$query = [
'templateName' => $templateName,
];
$result = $this->post('/templates/upload', $query, $data, HttpStatus::STATUS_CREATED);
return null === $result;
}
/**
* Upload a template to template storage
*
* @param string $templateFilename Template name
*
* @return bool
* @throws InvalidArgumentException
*/
public function uploadTemplate(string $templateFilename): bool
{
Assert::assertTemplateExtension($templateFilename);
Assert::assertFilenameExists($templateFilename);
$templateName = basename($templateFilename);
$data = FileUtils::read($templateFilename, true);
return $this->uploadTemplateFromBase64($data, $templateName);
}
/**
* Convert a document on the local file system to a different format
*
* @param string $documentFilename Document filename
* @param string $returnFormat Return format
*
* @return string
* @throws InvalidArgumentException
*/
public function convertDocument(string $documentFilename, string $returnFormat): string
{
Assert::assertDocumentExtension($documentFilename);
Assert::assertFilenameExists($documentFilename);
Assert::assertReturnFormat($returnFormat);
$query = [
'returnFormat' => $returnFormat,
];
$data = FileUtils::read($documentFilename, true);
$result = $this->post('/document/convert', $query, $data, HttpStatus::STATUS_OK);
if (is_string($result) && strlen($result) > 0) {
$decoded = base64_decode($result, true);
if (is_string($decoded)) {
return $decoded;
}
}
return '';
}
/**
* Merge data into a template and return an array of binary data.
* Each record in the array is the binary data of one document
*
* @param array $mergeData Array of merge data
* @param string $returnFormat Return format
* @param string $templateName Template name
* @param string $templateFilename Template filename on local file system
* @param bool $append Append flag
* @param array $mergeSettings Array of merge settings
*
* @return array
* @throws InvalidArgumentException
*/
public function mergeDocument(
array $mergeData,
string $returnFormat,
string $templateName = '',
string $templateFilename = '',
bool $append = false,
array $mergeSettings = []
): array {
$query = [];
$json = [];
$json['mergeData'] = $mergeData;
Assert::assertReturnFormat($returnFormat);
$query['returnFormat'] = $returnFormat;
if (strlen($templateName) > 0) {
Assert::assertTemplateName($templateName);
$query['templateName'] = $templateName;
}
if (strlen($templateFilename) > 0) {
Assert::assertTemplateExtension($templateFilename);
Assert::assertFilenameExists($templateFilename);
$json['template'] = FileUtils::read($templateFilename, true);
}
if ($append) {
//Assert::assertBoolean($append);
$query['append'] = Filter::filterBooleanToString($append);
}
if (count($mergeSettings) > 0) {
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
}
$result = $this->post('/document/merge', $query, $json, HttpStatus::STATUS_OK);
if (is_array($result)) {
foreach ($result as $key => $value) {
$value = base64_decode($value, true);
assert(is_string($value));
$result[$key] = $value;
}
return $result;
}
return [];
}
/**
* Combine documents by appending them, divided by a new section, paragraph or nothing
*
* @param array $documentsData
* @param string $returnFormat
* @param array $documentSettings
*
* @return string
* @throws InvalidArgumentException
*/
public function appendDocument(
array $documentsData,
string $returnFormat,
array $documentSettings = []
): string {
$query = [];
$json = [];
$json['documents'] = $this->buildDocumentsArray($documentsData);
Assert::assertReturnFormat($returnFormat);
$query['returnFormat'] = $returnFormat;
if (count($documentSettings) > 0) {
$json['documentSettings'] = $this->buildDocumentSettingsArray($documentSettings);
}
$result = $this->post('/document/append', $query, $json, HttpStatus::STATUS_OK);
if (is_string($result) && strlen($result) > 0) {
$decoded = base64_decode($result, true);
if (is_string($decoded)) {
return $decoded;
}
}
return '';
}
/**
* Perform find and replace in document and return binary data.
*
* @param array $findAndReplaceData Array of find and replace data
* @param string $returnFormat Return format
* @param string $templateName Template name
* @param string $templateFilename Template filename on local file system
* @param array $mergeSettings Array of merge settings
*
* @return string
* @throws InvalidArgumentException
*/
public function findAndReplaceDocument(
array $findAndReplaceData,
string $returnFormat,
string $templateName = '',
string $templateFilename = '',
array $mergeSettings = []
): string {
$query = [];
$json = [];
$json['findAndReplaceData'] = $this->buildFindAndReplaceDataArray($findAndReplaceData);
Assert::assertReturnFormat($returnFormat);
$query['returnFormat'] = $returnFormat;
if (strlen($templateName) > 0) {
Assert::assertTemplateName($templateName);
$query['templateName'] = $templateName;
}
if (strlen($templateFilename) > 0) {
Assert::assertTemplateExtension($templateFilename);
Assert::assertFilenameExists($templateFilename);
$json['template'] = FileUtils::read($templateFilename, true);
}
if (count($mergeSettings) > 0) {
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings);
}
$result = $this->post('/document/findandreplace', $query, $json, HttpStatus::STATUS_OK);
if (is_string($result) && strlen($result) > 0) {
$decoded = base64_decode($result, true);
if (is_string($decoded)) {
return $decoded;
}
}
return '';
}
/**
* Generate a thumbnail image per page of specified document filename.
* Return an array of binary data with each record containing one thumbnail.
*
* @param string $documentFilename Document filename
* @param int $zoomFactor Zoom factor
* @param int $fromPage From page
* @param int $toPage To page
* @param string $imageFormat Image format
*
* @return array
* @throws InvalidArgumentException
*/
public function getDocumentThumbnails(
string $documentFilename,
int $zoomFactor,
int $fromPage,
int $toPage,
string $imageFormat
): array {
Assert::assertDocumentThumbnailExtension($documentFilename);
Assert::assertFilenameExists($documentFilename);
Assert::assertZoomFactor($zoomFactor);
Assert::assertPage($fromPage);
Assert::assertPage($toPage);
Assert::assertImageFormat($imageFormat);
$query = [
'zoomFactor' => $zoomFactor,
'fromPage' => $fromPage,
'toPage' => $toPage,
'imageFormat' => $imageFormat,
];
$data = FileUtils::read($documentFilename, true);
$result = $this->post('/document/thumbnails', $query, $data, HttpStatus::STATUS_OK);
if (is_array($result)) {
foreach ($result as $key => $value) {
$value = base64_decode($value, true);
assert(is_string($value));
$result[$key] = $value;
}
return $result;
}
return [];
}
/**
* Return the tracked changes in a document.
*
* @param string $documentFilename Document filename
*
* @return array
* @throws InvalidArgumentException
*/
public function getTrackedChanges(string $documentFilename): array
{
$ret = [];
$propertyMap = new TrackedChangesPropertyMap();
Assert::assertDocumentExtension($documentFilename);
Assert::assertFilenameExists($documentFilename);
$data = FileUtils::read($documentFilename, true);
$result = $this->post('/processing/review/trackedchanges', [], $data, HttpStatus::STATUS_OK);
if (is_array($result)) {
$ret = $this->buildPropertyMapArray($result, $propertyMap);
array_walk($ret, function (array &$record): void {
$key = 'change_time';
if (isset($record[$key])) {
//@todo [20190902] return value of backend DateTime in Zulu timezone
//Assert::assertDateTime($record[$key]);
$record[$key] = Filter::filterDateTimeToTimestamp($record[$key]);
}
});
}
return $ret;
}
/**
* Removes a specific tracked change and returns the resulting document.
*
* @param string $documentFilename Document filename
* @param int $id The ID of the tracked change that needs to be removed
* @param bool $accept Specifies whether the tracked change should be accepted or not (reject)
*
* @return array
*
* @throws InvalidArgumentException
*/
public function removeTrackedChange(
string $documentFilename,
int $id,
bool $accept
): array {
$ret = [];
$propertyMap = new ModifiedDocumentPropertyMap();
Assert::assertDocumentExtension($documentFilename);
Assert::assertFilenameExists($documentFilename);
$query = [
'id' => $id,
'accept' => Filter::filterBooleanToString($accept),
];
$data = FileUtils::read($documentFilename, true);
$result = $this->post('/processing/review/removetrackedchange', $query, $data, HttpStatus::STATUS_OK);
if (is_array($result)) {
$ret = $this->buildPropertyMapArray($result, $propertyMap);
$key = 'document';
if (isset($ret[$key]) && is_string($ret[$key])) {
$ret[$key] = base64_decode($ret[$key], true);
}
}
return $ret;
}
/**
* Execute a POST request via REST client
*
* @param string $uri URI
* @param array $query Query
* @param mixed $json JSON
* @param int $statusCode Required HTTP status code for response
*
* @return mixed
*/
private function post(
string $uri,
array $query = [],
$json = '',
int $statusCode = 0
) {
$ret = '';
$options = [
RequestOptions::QUERY => $query,
RequestOptions::JSON => $json,
];
$response = $this->request(HttpMethod::METHOD_POST, $this->uri($uri), $options);
if ($statusCode === $response->getStatusCode()) {
$ret = json_decode($response->getBody()->getContents(), true);
}
return $ret;
}
// </editor-fold>
}