1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud; |
16
|
|
|
|
17
|
|
|
use GuzzleHttp\RequestOptions; |
18
|
|
|
use TxTextControl\ReportingCloud\Assert\Assert; |
19
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
20
|
|
|
use TxTextControl\ReportingCloud\Filter\Filter; |
21
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
22
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap; |
23
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\ApiKey as ApiKeyPropertyMap; |
24
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap; |
25
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap; |
26
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap; |
27
|
|
|
use TxTextControl\ReportingCloud\StatusCode\StatusCode; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Trait GetTrait |
31
|
|
|
* |
32
|
|
|
* @package TxTextControl\ReportingCloud |
33
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
34
|
|
|
*/ |
35
|
|
|
trait GetTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Abstract Methods |
39
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Construct URI with version number |
44
|
|
|
* |
45
|
|
|
* @param string $uri URI |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
abstract protected function uri(string $uri): string; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Request the URI with options |
53
|
|
|
* |
54
|
|
|
* @param string $method HTTP method |
55
|
|
|
* @param string $uri URI |
56
|
|
|
* @param array $options Options |
57
|
|
|
* |
58
|
|
|
* @return mixed|null|\Psr\Http\Message\ResponseInterface |
59
|
|
|
* |
60
|
|
|
* @throws RuntimeException |
61
|
|
|
*/ |
62
|
|
|
abstract protected function request(string $method, string $uri, array $options); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Using the passed propertyMap, recursively build array |
66
|
|
|
* |
67
|
|
|
* @param array $array Array |
68
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* GET Methods |
76
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return an associative array of API keys associated with the Reporting Cloud account |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
3 |
|
public function getApiKeys(): array |
85
|
|
|
{ |
86
|
3 |
|
$ret = []; |
87
|
|
|
|
88
|
3 |
|
$propertyMap = new ApiKeyPropertyMap(); |
89
|
|
|
|
90
|
3 |
|
$result = $this->get('/account/apikeys', null, null, StatusCode::OK); |
91
|
|
|
|
92
|
3 |
|
if (is_array($result) && count($result) > 0) { |
93
|
3 |
|
foreach ($result as $record) { |
94
|
3 |
|
$ret[] = $this->buildPropertyMapArray($record, $propertyMap); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return $ret; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check a corpus of text for spelling errors. |
103
|
|
|
* |
104
|
|
|
* Return an array of misspelled words, if spelling errors are found in the corpus of text. |
105
|
|
|
* |
106
|
|
|
* Return an empty array, if no misspelled words are found in the corpus of text. |
107
|
|
|
* |
108
|
|
|
* @param string $text Corpus of text that should be spell checked |
109
|
|
|
* @param string $language Language of specified text |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
113
|
|
|
*/ |
114
|
1 |
|
public function proofingCheck(string $text, string $language): array |
115
|
|
|
{ |
116
|
1 |
|
$ret = []; |
117
|
|
|
|
118
|
1 |
|
Assert::string($text); |
119
|
1 |
|
Assert::assertLanguage($language); |
120
|
|
|
|
121
|
1 |
|
$propertyMap = new IncorrectWordMap(); |
122
|
|
|
|
123
|
|
|
$query = [ |
124
|
1 |
|
'text' => $text, |
125
|
1 |
|
'language' => $language, |
126
|
|
|
]; |
127
|
|
|
|
128
|
1 |
|
$result = $this->get('/proofing/check', $query, null, StatusCode::OK); |
129
|
|
|
|
130
|
1 |
|
if (is_array($result) && count($result) > 0) { |
131
|
1 |
|
$ret = $this->buildPropertyMapArray($result, $propertyMap); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $ret; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return an array of available dictionaries on the Reporting Cloud service |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
1 |
|
public function getAvailableDictionaries(): array |
143
|
|
|
{ |
144
|
1 |
|
$ret = []; |
145
|
|
|
|
146
|
1 |
|
$result = $this->get('/proofing/availabledictionaries', null, null, StatusCode::OK); |
147
|
|
|
|
148
|
1 |
|
if (is_array($result) && count($result) > 0) { |
149
|
1 |
|
$ret = array_map('trim', $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $ret; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Return an array of suggestions for a misspelled word |
157
|
|
|
* |
158
|
|
|
* @param string $word Word that should be spell checked |
159
|
|
|
* @param string $language Language of specified text |
160
|
|
|
* @param int $max Maximum number of suggestions to return |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
164
|
|
|
*/ |
165
|
1 |
|
public function getProofingSuggestions(string $word, string $language, int $max = 10): array |
166
|
|
|
{ |
167
|
1 |
|
$ret = []; |
168
|
|
|
|
169
|
1 |
|
Assert::string($word); |
170
|
1 |
|
Assert::assertLanguage($language); |
171
|
1 |
|
Assert::integer($max); |
172
|
|
|
|
173
|
|
|
$query = [ |
174
|
1 |
|
'word' => $word, |
175
|
1 |
|
'language' => $language, |
176
|
1 |
|
'max' => $max, |
177
|
|
|
]; |
178
|
|
|
|
179
|
1 |
|
$result = $this->get('/proofing/suggestions', $query, null, StatusCode::OK); |
180
|
|
|
|
181
|
1 |
|
if (is_array($result) && count($result) > 0) { |
182
|
1 |
|
$ret = array_map('trim', $result); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $ret; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return an array of merge blocks and merge fields in a template file in template storage |
190
|
|
|
* |
191
|
|
|
* @param string $templateName Template name |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
195
|
|
|
*/ |
196
|
1 |
|
public function getTemplateInfo(string $templateName): array |
197
|
|
|
{ |
198
|
1 |
|
$ret = []; |
199
|
|
|
|
200
|
1 |
|
$propertyMap = new TemplateInfoPropertyMap(); |
201
|
|
|
|
202
|
1 |
|
Assert::assertTemplateName($templateName); |
203
|
|
|
|
204
|
|
|
$query = [ |
205
|
1 |
|
'templateName' => $templateName, |
206
|
|
|
]; |
207
|
|
|
|
208
|
1 |
|
$result = $this->get('/templates/info', $query, null, StatusCode::OK); |
209
|
|
|
|
210
|
1 |
|
if (is_array($result) && count($result) > 0) { |
211
|
1 |
|
$ret = $this->buildPropertyMapArray($result, $propertyMap); |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
return $ret; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Return an array of binary data. |
219
|
|
|
* Each record in the array is the binary data of a thumbnail image |
220
|
|
|
* |
221
|
|
|
* @param string $templateName Template name |
222
|
|
|
* @param int $zoomFactor Zoom factor |
223
|
|
|
* @param int $fromPage From page |
224
|
|
|
* @param int $toPage To page |
225
|
|
|
* @param string $imageFormat Image format |
226
|
|
|
* |
227
|
|
|
* @throws InvalidArgumentException |
228
|
|
|
* |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
6 |
|
public function getTemplateThumbnails( |
232
|
|
|
string $templateName, |
233
|
|
|
int $zoomFactor, |
234
|
|
|
int $fromPage, |
235
|
|
|
int $toPage, |
236
|
|
|
string $imageFormat |
237
|
|
|
): array { |
238
|
6 |
|
$ret = []; |
239
|
|
|
|
240
|
6 |
|
Assert::assertTemplateName($templateName); |
241
|
5 |
|
Assert::assertZoomFactor($zoomFactor); |
242
|
4 |
|
Assert::assertPage($fromPage); |
243
|
3 |
|
Assert::assertPage($toPage); |
244
|
2 |
|
Assert::assertImageFormat($imageFormat); |
245
|
|
|
|
246
|
|
|
$query = [ |
247
|
1 |
|
'templateName' => $templateName, |
248
|
1 |
|
'zoomFactor' => $zoomFactor, |
249
|
1 |
|
'fromPage' => $fromPage, |
250
|
1 |
|
'toPage' => $toPage, |
251
|
1 |
|
'imageFormat' => $imageFormat, |
252
|
|
|
]; |
253
|
|
|
|
254
|
1 |
|
$result = $this->get('/templates/thumbnails', $query, null, StatusCode::OK); |
255
|
|
|
|
256
|
1 |
|
if (is_array($result) && count($result) > 0) { |
257
|
1 |
|
$ret = array_map('base64_decode', $result); |
258
|
|
|
} |
259
|
|
|
|
260
|
1 |
|
return $ret; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Return the number of templates in template storage |
265
|
|
|
* |
266
|
|
|
* @return int |
267
|
|
|
*/ |
268
|
1 |
|
public function getTemplateCount(): int |
269
|
|
|
{ |
270
|
1 |
|
return (int) $this->get('/templates/count', null, null, StatusCode::OK); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return an array properties for the templates in template storage |
275
|
|
|
* |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
1 |
|
public function getTemplateList(): array |
279
|
|
|
{ |
280
|
1 |
|
$ret = []; |
281
|
|
|
|
282
|
1 |
|
$propertyMap = new TemplateListPropertyMap(); |
283
|
|
|
|
284
|
1 |
|
$result = $this->get('/templates/list', null, null, StatusCode::OK); |
285
|
|
|
|
286
|
1 |
|
if (is_array($result) && count($result) > 0) { |
287
|
1 |
|
$ret = $this->buildPropertyMapArray($result, $propertyMap); |
288
|
|
|
array_walk($ret, function (&$record) { |
289
|
1 |
|
$key = 'modified'; |
290
|
1 |
|
if (isset($record[$key])) { |
291
|
1 |
|
Assert::assertDateTime($record[$key]); |
292
|
1 |
|
$record[$key] = Filter::filterDateTimeToTimestamp($record[$key]); |
293
|
|
|
} |
294
|
1 |
|
}); |
295
|
|
|
} |
296
|
|
|
|
297
|
1 |
|
return $ret; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Return the number of pages in a template in template storage |
302
|
|
|
* |
303
|
|
|
* @param string $templateName Template name |
304
|
|
|
* |
305
|
|
|
* @return int |
306
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
307
|
|
|
*/ |
308
|
2 |
|
public function getTemplatePageCount(string $templateName): int |
309
|
|
|
{ |
310
|
2 |
|
Assert::assertTemplateName($templateName); |
311
|
|
|
|
312
|
|
|
$query = [ |
313
|
1 |
|
'templateName' => $templateName, |
314
|
|
|
]; |
315
|
|
|
|
316
|
1 |
|
return (int) $this->get('/templates/pagecount', $query, null, StatusCode::OK); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Return true, if the template exists in template storage |
321
|
|
|
* |
322
|
|
|
* @param string $templateName Template name |
323
|
|
|
* |
324
|
|
|
* @return bool |
325
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
326
|
|
|
*/ |
327
|
2 |
|
public function templateExists(string $templateName): bool |
328
|
|
|
{ |
329
|
2 |
|
Assert::assertTemplateName($templateName); |
330
|
|
|
|
331
|
|
|
$query = [ |
332
|
1 |
|
'templateName' => $templateName, |
333
|
|
|
]; |
334
|
|
|
|
335
|
1 |
|
return (bool) $this->get('/templates/exists', $query, null, StatusCode::OK); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Return an array of available fonts on the Reporting Cloud service |
340
|
|
|
* |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
3 |
|
public function getFontList(): array |
344
|
|
|
{ |
345
|
3 |
|
$ret = []; |
346
|
|
|
|
347
|
3 |
|
$result = $this->get('/fonts/list', null, null, StatusCode::OK); |
348
|
|
|
|
349
|
2 |
|
if (is_array($result) && count($result) > 0) { |
350
|
2 |
|
$ret = array_map('trim', $result); |
351
|
|
|
} |
352
|
|
|
|
353
|
2 |
|
return $ret; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Return an array properties for the ReportingCloud account |
358
|
|
|
* |
359
|
|
|
* @return array |
360
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
361
|
|
|
*/ |
362
|
1 |
|
public function getAccountSettings(): array |
363
|
|
|
{ |
364
|
1 |
|
$ret = []; |
365
|
|
|
|
366
|
1 |
|
$propertyMap = new AccountSettingsPropertyMap(); |
367
|
|
|
|
368
|
1 |
|
$result = $this->get('/account/settings', null, null, StatusCode::OK); |
369
|
|
|
|
370
|
1 |
|
if (is_array($result) && count($result) > 0) { |
371
|
1 |
|
$ret = $this->buildPropertyMapArray($result, $propertyMap); |
372
|
1 |
|
$key = 'valid_until'; |
373
|
1 |
|
if ($ret[$key]) { |
374
|
1 |
|
Assert::assertDateTime($ret[$key]); |
375
|
1 |
|
$ret[$key] = Filter::filterDateTimeToTimestamp($ret[$key]); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
1 |
|
return $ret; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Download the binary data of a template from template storage |
384
|
|
|
* |
385
|
|
|
* @param string $templateName Template name |
386
|
|
|
* |
387
|
|
|
* @return string |
388
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
389
|
|
|
*/ |
390
|
2 |
|
public function downloadTemplate(string $templateName): string |
391
|
|
|
{ |
392
|
2 |
|
Assert::assertTemplateName($templateName); |
393
|
|
|
|
394
|
|
|
$query = [ |
395
|
1 |
|
'templateName' => $templateName, |
396
|
|
|
]; |
397
|
|
|
|
398
|
1 |
|
$result = (string) $this->get('/templates/download', $query, null, StatusCode::OK); |
399
|
|
|
|
400
|
1 |
|
return (empty($result)) ? '' : $result; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Execute a GET request via REST client |
405
|
|
|
* |
406
|
|
|
* @param string $uri URI |
407
|
|
|
* @param array $query Query |
408
|
|
|
* @param string|array $json JSON |
409
|
|
|
* @param int $statusCode Required HTTP status code for response |
410
|
|
|
* |
411
|
|
|
* @return array|string|null |
412
|
|
|
*/ |
413
|
16 |
|
private function get( |
414
|
|
|
string $uri, |
415
|
|
|
?array $query = null, |
416
|
|
|
$json = null, |
417
|
|
|
?int $statusCode = null |
418
|
|
|
) { |
419
|
16 |
|
$ret = null; |
420
|
|
|
|
421
|
|
|
$options = [ |
422
|
16 |
|
RequestOptions::QUERY => $query, |
423
|
16 |
|
RequestOptions::JSON => $json, |
424
|
|
|
]; |
425
|
|
|
|
426
|
16 |
|
$response = $this->request('GET', $this->uri($uri), $options); |
427
|
|
|
|
428
|
15 |
|
if ($statusCode === $response->getStatusCode()) { |
429
|
15 |
|
$ret = json_decode($response->getBody()->getContents(), true); |
430
|
|
|
} |
431
|
|
|
|
432
|
15 |
|
return $ret; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|