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