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