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