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