Passed
Branch development-2.0 (865631)
by Jonathan
04:26
created

assertDocumentDivider()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.5555
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 5.0144
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use ReflectionClass;
18
use ReflectionException;
19
use TxTextControl\ReportingCloud\ReportingCloud;
20
21
/**
22
 * Trait DocumentDividerTrait
23
 *
24
 * @package TxTextControl\ReportingCloud
25
 */
26
trait AssertDocumentDividerTrait
27
{
28
    /**
29
     * Validate a document divider
30
     *
31
     * @param int    $value
32
     * @param string $message
33
     *
34
     * @return null
35
     * @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException
36
     */
37 4
    public static function assertDocumentDivider(int $value, string $message = '')
38
    {
39 4
        $haystack = [];
40
        try {
41 4
            $reportingCloud  = new ReportingCloud();
42 4
            $reflectionClass = new ReflectionClass($reportingCloud);
43 4
            foreach ($reflectionClass->getConstants() as $constantKey => $constantValue) {
44 4
                if (0 === strpos($constantKey, 'DOCUMENT_DIVIDER_')) {
45 4
                    $haystack[] = $constantValue;
46
                }
47
            }
48 4
            unset($reportingCloud);
49
        } catch (ReflectionException $e) {
50
            // continue
51
        }
52
53 4
        $format  = '%s contains an unsupported document divider';
54 4
        $message = sprintf($message ?: $format, self::valueToString($value));
55
56 4
        return self::oneOf($value, $haystack, $message);
57
    }
58
}
59