|
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 |
|
return (string) base64_decode($result); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Merge data into a template and return an array of binary data. |
|
183
|
|
|
* Each record in the array is the binary data of one document |
|
184
|
|
|
* |
|
185
|
|
|
* @param array $mergeData Array of merge data |
|
186
|
|
|
* @param string $returnFormat Return format |
|
187
|
|
|
* @param string|null $templateName Template name |
|
188
|
|
|
* @param string|null $templateFilename Template filename on local file system |
|
189
|
|
|
* @param bool|null $append Append flag |
|
190
|
|
|
* @param array|null $mergeSettings Array of merge settings |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
|
194
|
|
|
*/ |
|
195
|
12 |
|
public function mergeDocument( |
|
196
|
|
|
array $mergeData, |
|
197
|
|
|
string $returnFormat, |
|
198
|
|
|
?string $templateName = null, |
|
199
|
|
|
?string $templateFilename = null, |
|
200
|
|
|
?bool $append = null, |
|
201
|
|
|
?array $mergeSettings = null |
|
202
|
|
|
): array { |
|
203
|
|
|
|
|
204
|
12 |
|
$ret = []; |
|
205
|
|
|
|
|
206
|
12 |
|
$query = []; |
|
207
|
12 |
|
$json = []; |
|
208
|
|
|
|
|
209
|
12 |
|
Assert::isArray($mergeData); |
|
210
|
12 |
|
$json['mergeData'] = $mergeData; |
|
211
|
|
|
|
|
212
|
12 |
|
Assert::assertReturnFormat($returnFormat); |
|
213
|
11 |
|
$query['returnFormat'] = $returnFormat; |
|
214
|
|
|
|
|
215
|
11 |
|
if (null !== $templateName) { |
|
216
|
2 |
|
Assert::assertTemplateName($templateName); |
|
217
|
1 |
|
$query['templateName'] = $templateName; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
10 |
|
if (null !== $templateFilename) { |
|
221
|
9 |
|
Assert::assertTemplateExtension($templateFilename); |
|
222
|
6 |
|
Assert::filenameExists($templateFilename); |
|
223
|
5 |
|
$template = file_get_contents($templateFilename); |
|
224
|
5 |
|
$template = base64_encode($template); |
|
225
|
5 |
|
$json['template'] = $template; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
6 |
|
if (null !== $append) { |
|
229
|
5 |
|
Assert::boolean($append); |
|
230
|
5 |
|
$query['append'] = Filter::filterBooleanToString($append); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
6 |
|
if (is_array($mergeSettings)) { |
|
234
|
5 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
3 |
|
$result = $this->post('/document/merge', $query, $json); |
|
238
|
|
|
|
|
239
|
3 |
|
if (is_array($result) && count($result) > 0) { |
|
240
|
3 |
|
$ret = array_map('base64_decode', $result); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
3 |
|
return $ret; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Combine documents to appending them, divided by a new section, paragraph or nothing |
|
248
|
|
|
* |
|
249
|
|
|
* @param array $documentsData |
|
250
|
|
|
* @param string $returnFormat |
|
251
|
|
|
* @param array|null $documentSettings |
|
252
|
|
|
* |
|
253
|
|
|
* @return string |
|
254
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
|
255
|
|
|
*/ |
|
256
|
1 |
|
public function appendDocument( |
|
257
|
|
|
array $documentsData, |
|
258
|
|
|
string $returnFormat, |
|
259
|
|
|
?array $documentSettings = null |
|
260
|
|
|
): string { |
|
261
|
|
|
|
|
262
|
1 |
|
$query = []; |
|
263
|
1 |
|
$json = []; |
|
264
|
|
|
|
|
265
|
1 |
|
Assert::isArray($documentsData); |
|
266
|
1 |
|
$json['documents'] = $this->buildDocumentsArray($documentsData); |
|
267
|
|
|
|
|
268
|
1 |
|
Assert::assertReturnFormat($returnFormat); |
|
269
|
1 |
|
$query['returnFormat'] = $returnFormat; |
|
270
|
|
|
|
|
271
|
1 |
|
if (is_array($documentSettings)) { |
|
272
|
1 |
|
$json['documentSettings'] = $this->buildDocumentSettingsArray($documentSettings); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
1 |
|
$result = $this->post('/document/append', $query, $json); |
|
276
|
|
|
|
|
277
|
1 |
|
return (string) base64_decode($result); |
|
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Perform find and replace in document and return binary data. |
|
282
|
|
|
* |
|
283
|
|
|
* @param array $findAndReplaceData Array of find and replace data |
|
284
|
|
|
* @param string $returnFormat Return format |
|
285
|
|
|
* @param string|null $templateName Template name |
|
286
|
|
|
* @param string|null $templateFilename Template filename on local file system |
|
287
|
|
|
* @param array|null $mergeSettings Array of merge settings |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
|
291
|
|
|
*/ |
|
292
|
10 |
|
public function findAndReplaceDocument( |
|
293
|
|
|
array $findAndReplaceData, |
|
294
|
|
|
string $returnFormat, |
|
295
|
|
|
?string $templateName = null, |
|
296
|
|
|
?string $templateFilename = null, |
|
297
|
|
|
?array $mergeSettings = null |
|
298
|
|
|
): string { |
|
299
|
|
|
|
|
300
|
10 |
|
$query = []; |
|
301
|
10 |
|
$json = []; |
|
302
|
|
|
|
|
303
|
10 |
|
Assert::isArray($findAndReplaceData); |
|
304
|
10 |
|
$json['findAndReplaceData'] = $this->buildFindAndReplaceDataArray($findAndReplaceData); |
|
305
|
|
|
|
|
306
|
10 |
|
Assert::assertReturnFormat($returnFormat); |
|
307
|
9 |
|
$query['returnFormat'] = $returnFormat; |
|
308
|
|
|
|
|
309
|
9 |
|
if (null !== $templateName) { |
|
310
|
2 |
|
Assert::assertTemplateName($templateName); |
|
311
|
1 |
|
$query['templateName'] = $templateName; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
8 |
|
if (null !== $templateFilename) { |
|
315
|
7 |
|
Assert::assertTemplateExtension($templateFilename); |
|
316
|
4 |
|
Assert::filenameExists($templateFilename); |
|
317
|
3 |
|
$template = file_get_contents($templateFilename); |
|
318
|
3 |
|
$template = base64_encode($template); |
|
319
|
3 |
|
$json['template'] = $template; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
4 |
|
if (is_array($mergeSettings)) { |
|
323
|
4 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
2 |
|
$result = $this->post('/document/findandreplace', $query, $json); |
|
327
|
|
|
|
|
328
|
2 |
|
return (string) base64_decode($result); |
|
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Execute a POST request via REST client |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $uri URI |
|
335
|
|
|
* @param array $query Query |
|
336
|
|
|
* @param string|array $json JSON |
|
337
|
|
|
* |
|
338
|
|
|
* @return array|string|null |
|
339
|
|
|
*/ |
|
340
|
14 |
|
private function post(string $uri, array $query = [], $json = '') |
|
341
|
|
|
{ |
|
342
|
|
|
$options = [ |
|
343
|
14 |
|
RequestOptions::QUERY => $query, |
|
344
|
14 |
|
RequestOptions::JSON => $json, |
|
345
|
|
|
]; |
|
346
|
|
|
|
|
347
|
|
|
$statusCodes = [ |
|
348
|
14 |
|
StatusCode::OK, |
|
349
|
14 |
|
StatusCode::CREATED, |
|
350
|
|
|
]; |
|
351
|
|
|
|
|
352
|
14 |
|
$response = $this->request('POST', $this->uri($uri), $options); |
|
353
|
|
|
|
|
354
|
14 |
|
if (!$response instanceof Response) { |
|
355
|
|
|
return ''; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
14 |
|
if (!in_array($response->getStatusCode(), $statusCodes)) { |
|
359
|
|
|
return ''; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
14 |
|
return json_decode($response->getBody()->getContents(), true); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|