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