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