-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetTrait.php
More file actions
393 lines (319 loc) · 11 KB
/
Copy pathGetTrait.php
File metadata and controls
393 lines (319 loc) · 11 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
<?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://tinyurl.com/vmbbh6kd for the canonical source repository
* @license https://tinyurl.com/3pc9am89
* @copyright © 2023 Text Control GmbH
*/
namespace TextControl\ReportingCloud;
use Ctw\Http\HttpMethod;
use Ctw\Http\HttpStatus;
use GuzzleHttp\RequestOptions;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use TextControl\ReportingCloud\Assert\Assert;
use TextControl\ReportingCloud\Filter\Filter;
use TextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
use TextControl\ReportingCloud\PropertyMap\AccountSettings as AccountSettingsPropertyMap;
use TextControl\ReportingCloud\PropertyMap\ApiKey as ApiKeyPropertyMap;
use TextControl\ReportingCloud\PropertyMap\IncorrectWord as IncorrectWordMap;
use TextControl\ReportingCloud\PropertyMap\TemplateInfo as TemplateInfoPropertyMap;
use TextControl\ReportingCloud\PropertyMap\TemplateList as TemplateListPropertyMap;
/**
* Trait GetTrait
*/
trait GetTrait
{
/**
* Return an associative array of API keys associated with the Reporting Cloud account
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
public function getTemplateCount(): int
{
$count = $this->get('/templates/count', [], '', HttpStatus::STATUS_OK);
return is_numeric($count) ? (int) $count : 0;
}
/**
* Return an array of properties for the templates in template storage
*/
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, static 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
*/
public function getTemplatePageCount(string $templateName): int
{
Assert::assertTemplateName($templateName);
$query = [
'templateName' => $templateName,
];
$count = $this->get('/templates/pagecount', $query, '', HttpStatus::STATUS_OK);
return is_numeric($count) ? (int) $count : 0;
}
/**
* Return true, if the template exists in template storage
*
* @param string $templateName Template name
*/
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
*/
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 of properties for the ReportingCloud account
*/
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
*/
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) && 0 < strlen($result)) {
$decoded = base64_decode($result, true);
if (is_string($decoded)) {
return $decoded;
}
}
return '';
}
/**
* Construct URI with version number
*
* @param string $uri URI
*/
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
*/
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
*/
abstract protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array;
/**
* 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
*/
private function get(string $uri, array $query = [], mixed $json = '', int $statusCode = 0): mixed
{
$ret = null;
$response = $this->request(HttpMethod::METHOD_GET, $this->uri($uri), [
RequestOptions::QUERY => $query,
RequestOptions::JSON => $json,
]);
if ($statusCode === $response->getStatusCode()) {
try {
$body = $response->getBody();
$content = $body->getContents();
$ret = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
}
}
return $ret;
}
}