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