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