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\Assert; |
||
16 | |||
17 | use ReflectionClass; |
||
18 | use ReflectionException; |
||
19 | use TextControl\ReportingCloud\ReportingCloud; |
||
20 | |||
21 | /** |
||
22 | * Trait DocumentDividerTrait |
||
23 | */ |
||
24 | trait AssertDocumentDividerTrait |
||
25 | { |
||
26 | use AssertOneOfTrait; |
||
27 | use ValueToStringTrait; |
||
28 | |||
29 | /** |
||
30 | * Check value is a valid document divider |
||
31 | */ |
||
32 | 8 | public static function assertDocumentDivider(int $value, string $message = ''): void |
|
33 | { |
||
34 | 8 | $haystack = self::getDocumentDividers(); |
|
35 | 8 | $format = '' === $message ? '%1$s contains an unsupported document divider' : $message; |
|
36 | 8 | $message = sprintf($format, self::valueToString($value)); |
|
37 | |||
38 | 8 | self::assertOneOf($value, $haystack, $message); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Return document dividers array |
||
43 | */ |
||
44 | 8 | private static function getDocumentDividers(): array |
|
45 | { |
||
46 | 8 | $constants = []; |
|
47 | |||
48 | try { |
||
49 | 8 | $reportingCloud = new ReportingCloud(); |
|
50 | 8 | $reflectionClass = new ReflectionClass($reportingCloud); |
|
51 | 8 | $constants = $reflectionClass->getConstants(); |
|
52 | 8 | unset($reportingCloud); |
|
53 | } catch (ReflectionException) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||
54 | } |
||
55 | |||
56 | 8 | $array = array_filter( |
|
57 | 8 | $constants, |
|
58 | 8 | static fn(string $constantKey): bool => str_starts_with($constantKey, 'DOCUMENT_DIVIDER_'), |
|
59 | 8 | ARRAY_FILTER_USE_KEY |
|
60 | 8 | ); |
|
61 | |||
62 | 8 | return array_values($array); |
|
63 | } |
||
64 | } |
||
65 |