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