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\Exception\InvalidArgumentException; |
19
|
|
|
use TxTextControl\ReportingCloud\Filter\StaticFilter; |
20
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
21
|
|
|
use TxTextControl\ReportingCloud\Validator\StaticValidator; |
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($uri); |
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($method, $uri, $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); |
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); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Using the passed propertyMap, recursively build array |
78
|
|
|
* |
79
|
|
|
* @param array $array Array |
80
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* POST Methods |
88
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Upload a template to template storage |
93
|
|
|
* |
94
|
|
|
* @param string $templateFilename Template name |
95
|
|
|
* |
96
|
|
|
* @throws InvalidArgumentException |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
26 |
|
public function uploadTemplate($templateFilename) |
101
|
|
|
{ |
102
|
26 |
|
$ret = false; |
103
|
|
|
|
104
|
26 |
|
StaticValidator::execute($templateFilename, 'TemplateExtension'); |
105
|
20 |
|
StaticValidator::execute($templateFilename, 'FileExists'); |
106
|
|
|
|
107
|
18 |
|
$templateFilename = realpath($templateFilename); |
108
|
18 |
|
$templateName = basename($templateFilename); |
109
|
|
|
|
110
|
|
|
$query = [ |
111
|
18 |
|
'templateName' => $templateName, |
112
|
9 |
|
]; |
113
|
|
|
|
114
|
18 |
|
$json = file_get_contents($templateFilename); |
115
|
18 |
|
$json = base64_encode($json); |
116
|
|
|
|
117
|
|
|
$options = [ |
118
|
18 |
|
RequestOptions::QUERY => $query, |
119
|
18 |
|
RequestOptions::JSON => $json, |
120
|
9 |
|
]; |
121
|
|
|
|
122
|
18 |
|
$response = $this->request('POST', $this->uri('/templates/upload'), $options); |
123
|
|
|
|
124
|
18 |
|
if ($response instanceof Response && 201 === $response->getStatusCode()) { |
125
|
18 |
|
$ret = true; |
126
|
9 |
|
} |
127
|
|
|
|
128
|
18 |
|
return $ret; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Upload a template to template storage |
133
|
|
|
* |
134
|
|
|
* @param string $templateFilename Template name |
135
|
|
|
* |
136
|
|
|
* @throws InvalidArgumentException |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function uploadTemplateFromBase64($base64, $templateName) |
141
|
|
|
{ |
142
|
|
|
$ret = false; |
143
|
|
|
|
144
|
|
|
StaticValidator::execute($templateName, 'TemplateExtension'); |
145
|
|
|
|
146
|
|
|
$query = [ |
147
|
|
|
'templateName' => $templateName, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
$options = [ |
151
|
|
|
RequestOptions::QUERY => $query, |
152
|
|
|
RequestOptions::JSON => $base64, |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
$response = $this->request('POST', $this->uri('/templates/upload'), $options); |
156
|
|
|
|
157
|
|
|
if ($response instanceof Response && 201 === $response->getStatusCode()) { |
158
|
|
|
$ret = true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $ret; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Convert a document on the local file system to a different format |
166
|
|
|
* |
167
|
|
|
* @param string $documentFilename Document filename |
168
|
|
|
* @param string $returnFormat Return format |
169
|
|
|
* |
170
|
|
|
* @throws InvalidArgumentException |
171
|
|
|
* |
172
|
|
|
* @return string|null |
173
|
|
|
*/ |
174
|
12 |
|
public function convertDocument($documentFilename, $returnFormat) |
175
|
|
|
{ |
176
|
12 |
|
$ret = null; |
177
|
|
|
|
178
|
12 |
|
StaticValidator::execute($documentFilename, 'DocumentExtension'); |
179
|
6 |
|
StaticValidator::execute($documentFilename, 'FileExists'); |
180
|
4 |
|
StaticValidator::execute($returnFormat, 'ReturnFormat'); |
181
|
|
|
|
182
|
|
|
$query = [ |
183
|
2 |
|
'returnFormat' => $returnFormat, |
184
|
1 |
|
]; |
185
|
|
|
|
186
|
2 |
|
$documentFilename = realpath($documentFilename); |
187
|
|
|
|
188
|
2 |
|
$json = file_get_contents($documentFilename); |
189
|
2 |
|
$json = base64_encode($json); |
190
|
|
|
|
191
|
|
|
$options = [ |
192
|
2 |
|
RequestOptions::QUERY => $query, |
193
|
2 |
|
RequestOptions::JSON => $json, |
194
|
1 |
|
]; |
195
|
|
|
|
196
|
2 |
|
$response = $this->request('POST', $this->uri('/document/convert'), $options); |
197
|
|
|
|
198
|
2 |
|
if ($response instanceof Response && 200 === $response->getStatusCode()) { |
199
|
2 |
|
$ret = base64_decode($response->getBody()); |
200
|
1 |
|
} |
201
|
|
|
|
202
|
2 |
|
return $ret; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Merge data into a template and return an array of binary data. |
207
|
|
|
* Each record in the array is the binary data of one document |
208
|
|
|
* |
209
|
|
|
* @param array $mergeData Array of merge data |
210
|
|
|
* @param string $returnFormat Return format |
211
|
|
|
* @param string $templateName Template name |
212
|
|
|
* @param string $templateFilename Template filename on local file system |
213
|
|
|
* @param bool $append Append flag |
214
|
|
|
* @param array $mergeSettings Array of merge settings |
215
|
|
|
* |
216
|
|
|
* @throws InvalidArgumentException |
217
|
|
|
* |
218
|
|
|
* @return array|null |
219
|
|
|
*/ |
220
|
28 |
|
public function mergeDocument( |
221
|
|
|
$mergeData, |
222
|
|
|
$returnFormat, |
223
|
|
|
$templateName = null, |
224
|
|
|
$templateFilename = null, |
225
|
|
|
$append = null, |
226
|
|
|
$mergeSettings = [] |
227
|
|
|
) { |
228
|
28 |
|
$ret = null; |
229
|
|
|
|
230
|
28 |
|
StaticValidator::execute($mergeData, 'TypeArray'); |
231
|
28 |
|
StaticValidator::execute($returnFormat, 'ReturnFormat'); |
232
|
|
|
|
233
|
26 |
|
if (null !== $templateName) { |
234
|
4 |
|
StaticValidator::execute($templateName, 'TemplateName'); |
235
|
1 |
|
} |
236
|
|
|
|
237
|
24 |
|
if (null !== $templateFilename) { |
238
|
22 |
|
StaticValidator::execute($templateFilename, 'TemplateExtension'); |
239
|
16 |
|
StaticValidator::execute($templateFilename, 'FileExists'); |
240
|
14 |
|
$templateFilename = realpath($templateFilename); |
241
|
7 |
|
} |
242
|
|
|
|
243
|
16 |
|
if (null !== $append) { |
244
|
14 |
|
$append = StaticFilter::execute($append, 'BooleanToString'); |
245
|
6 |
|
} |
246
|
|
|
|
247
|
14 |
|
StaticValidator::execute($mergeSettings, 'TypeArray'); |
248
|
|
|
|
249
|
|
|
$query = [ |
250
|
12 |
|
'returnFormat' => $returnFormat, |
251
|
12 |
|
'append' => $append, |
252
|
6 |
|
]; |
253
|
|
|
|
254
|
12 |
|
if (null !== $templateName) { |
255
|
2 |
|
$query['templateName'] = $templateName; |
256
|
1 |
|
} |
257
|
|
|
|
258
|
|
|
$json = [ |
259
|
12 |
|
'mergeData' => $mergeData, |
260
|
6 |
|
]; |
261
|
|
|
|
262
|
12 |
|
if (null !== $templateFilename) { |
263
|
10 |
|
$template = file_get_contents($templateFilename); |
264
|
10 |
|
$template = base64_encode($template); |
265
|
10 |
|
$json['template'] = $template; |
266
|
5 |
|
} |
267
|
|
|
|
268
|
12 |
|
if (count($mergeSettings) > 0) { |
269
|
10 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
270
|
2 |
|
} |
271
|
|
|
|
272
|
|
|
$options = [ |
273
|
6 |
|
RequestOptions::QUERY => $query, |
274
|
6 |
|
RequestOptions::JSON => $json, |
275
|
3 |
|
]; |
276
|
|
|
|
277
|
6 |
|
$response = $this->request('POST', $this->uri('/document/merge'), $options); |
278
|
|
|
|
279
|
6 |
|
if ($response instanceof Response && 200 === $response->getStatusCode()) { |
280
|
6 |
|
$body = json_decode($response->getBody(), true); |
281
|
6 |
|
if (is_array($body) && count($body) > 0) { |
282
|
6 |
|
$ret = array_map('base64_decode', $body); |
283
|
3 |
|
} |
284
|
3 |
|
} |
285
|
|
|
|
286
|
6 |
|
return $ret; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Perform find and replace in document and return binary data. |
291
|
|
|
* |
292
|
|
|
* @param array $findAndReplaceData Array of find and replace data |
293
|
|
|
* @param string $returnFormat Return format |
294
|
|
|
* @param string $templateName Template name |
295
|
|
|
* @param string $templateFilename Template filename on local file system |
296
|
|
|
* @param array $mergeSettings Array of merge settings |
297
|
|
|
* |
298
|
|
|
* @throws InvalidArgumentException |
299
|
|
|
* |
300
|
|
|
* @return string|null |
301
|
|
|
*/ |
302
|
22 |
|
public function findAndReplaceDocument( |
303
|
|
|
$findAndReplaceData, |
304
|
|
|
$returnFormat, |
305
|
|
|
$templateName = null, |
306
|
|
|
$templateFilename = null, |
307
|
|
|
$mergeSettings = [] |
308
|
|
|
) { |
309
|
22 |
|
$ret = null; |
310
|
|
|
|
311
|
22 |
|
StaticValidator::execute($findAndReplaceData, 'TypeArray'); |
312
|
22 |
|
StaticValidator::execute($returnFormat, 'ReturnFormat'); |
313
|
|
|
|
314
|
20 |
|
if (null !== $templateName) { |
315
|
4 |
|
StaticValidator::execute($templateName, 'TemplateName'); |
316
|
1 |
|
} |
317
|
|
|
|
318
|
18 |
|
if (null !== $templateFilename) { |
319
|
16 |
|
StaticValidator::execute($templateFilename, 'TemplateExtension'); |
320
|
10 |
|
StaticValidator::execute($templateFilename, 'FileExists'); |
321
|
8 |
|
$templateFilename = realpath($templateFilename); |
322
|
4 |
|
} |
323
|
|
|
|
324
|
10 |
|
StaticValidator::execute($mergeSettings, 'TypeArray'); |
325
|
|
|
|
326
|
|
|
$query = [ |
327
|
8 |
|
'returnFormat' => $returnFormat, |
328
|
4 |
|
]; |
329
|
|
|
|
330
|
8 |
|
if (null !== $templateName) { |
331
|
2 |
|
$query['templateName'] = $templateName; |
332
|
1 |
|
} |
333
|
|
|
|
334
|
|
|
$json = [ |
335
|
8 |
|
'findAndReplaceData' => $this->buildFindAndReplaceDataArray($findAndReplaceData), |
336
|
4 |
|
]; |
337
|
|
|
|
338
|
8 |
|
if (null !== $templateFilename) { |
339
|
6 |
|
$template = file_get_contents($templateFilename); |
340
|
6 |
|
$template = base64_encode($template); |
341
|
6 |
|
$json['template'] = $template; |
342
|
3 |
|
} |
343
|
|
|
|
344
|
8 |
|
if (count($mergeSettings) > 0) { |
345
|
8 |
|
$json['mergeSettings'] = $this->buildMergeSettingsArray($mergeSettings); |
346
|
2 |
|
} |
347
|
|
|
|
348
|
|
|
$options = [ |
349
|
4 |
|
RequestOptions::QUERY => $query, |
350
|
4 |
|
RequestOptions::JSON => $json, |
351
|
2 |
|
]; |
352
|
|
|
|
353
|
4 |
|
$response = $this->request('POST', $this->uri('/document/findandreplace'), $options); |
354
|
|
|
|
355
|
4 |
|
if ($response instanceof Response && 200 === $response->getStatusCode()) { |
356
|
4 |
|
$ret = base64_decode($response->getBody()); |
357
|
2 |
|
} |
358
|
|
|
|
359
|
4 |
|
return $ret; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|