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\Assert; |
||
| 16 | |||
| 17 | use ReflectionClass; |
||
| 18 | use ReflectionException; |
||
| 19 | use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
||
| 20 | use TxTextControl\ReportingCloud\ReportingCloud; |
||
| 21 | use TxTextControl\ReportingCloud\Stdlib\StringUtils; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Trait DocumentDividerTrait |
||
| 25 | * |
||
| 26 | * @package TxTextControl\ReportingCloud |
||
| 27 | * @author Jonathan Maron (@JonathanMaron) |
||
| 28 | */ |
||
| 29 | trait AssertDocumentDividerTrait |
||
| 30 | { |
||
| 31 | use ValueToStringTrait; |
||
| 32 | use AssertOneOfTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Check value is a valid document divider |
||
| 36 | * |
||
| 37 | * @param int $value |
||
| 38 | * @param string $message |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | * @throws InvalidArgumentException |
||
| 42 | */ |
||
| 43 | 8 | public static function assertDocumentDivider(int $value, string $message = ''): void |
|
| 44 | { |
||
| 45 | 8 | $haystack = self::getDocumentDividers(); |
|
| 46 | 8 | $format = 0 === strlen($message) ? '%1$s contains an unsupported document divider' : $message; |
|
| 47 | 8 | $message = sprintf($format, self::valueToString($value)); |
|
| 48 | |||
| 49 | 8 | self::assertOneOf($value, $haystack, $message); |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Return document dividers array |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | 8 | private static function getDocumentDividers(): array |
|
| 58 | { |
||
| 59 | 8 | $constants = []; |
|
| 60 | |||
| 61 | try { |
||
| 62 | 8 | $reportingCloud = new ReportingCloud(); |
|
| 63 | 8 | $reflectionClass = new ReflectionClass($reportingCloud); |
|
| 64 | 8 | $constants = $reflectionClass->getConstants(); |
|
| 65 | 8 | unset($reportingCloud); |
|
| 66 | } catch (ReflectionException $e) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 67 | } |
||
| 68 | |||
| 69 | 8 | $array = array_filter($constants, function (string $constantKey): bool { |
|
| 70 | 8 | if (StringUtils::startsWith($constantKey, 'DOCUMENT_DIVIDER_')) { |
|
| 71 | 8 | return true; |
|
| 72 | } |
||
| 73 | |||
| 74 | 8 | return false; |
|
| 75 | 8 | }, ARRAY_FILTER_USE_KEY); |
|
| 76 | |||
| 77 | 8 | $array = array_values($array); |
|
| 78 | |||
| 79 | 8 | return $array; |
|
| 80 | } |
||
| 81 | } |
||
| 82 |