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