Passed
Push — master ( 2166ef...424d33 )
by Jonathan
14:50
created

AssertDocumentDividerTrait::getDocumentDividers()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.0052
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
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
21
22
/**
23
 * Trait DocumentDividerTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait AssertDocumentDividerTrait
29
{
30
    /**
31
     * Validate a document divider
32
     *
33
     * @param int    $value
34
     * @param string $message
35
     *
36
     * @return null
37
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
38
     */
39 12
    public static function assertDocumentDivider(int $value, string $message = '')
40
    {
41 12
        $haystack = self::getDocumentDividers();
42 12
        $format   = $message ?: '%s contains an unsupported document divider';
43 12
        $message  = sprintf($format, self::valueToString($value));
44
45 12
        return self::oneOf($value, $haystack, $message);
46
    }
47
48
    /**
49
     * Return document dividers array
50
     *
51
     * @return array
52
     */
53 12
    private static function getDocumentDividers(): array
54
    {
55 12
        $constants = [];
56
57
        try {
58 12
            $reportingCloud  = new ReportingCloud();
59 12
            $reflectionClass = new ReflectionClass($reportingCloud);
60 12
            $constants       = $reflectionClass->getConstants();
61 12
            unset($reportingCloud);
62
        } catch (ReflectionException $e) {
63
            // continue
64
        }
65
66
        $ret = array_filter($constants, function ($constantKey) {
67 12
            if (StringUtils::startsWith($constantKey, 'DOCUMENT_DIVIDER_')) {
68 12
                return true;
69
            }
70 12
        }, ARRAY_FILTER_USE_KEY);
71
72 12
        $ret = array_values($ret);
73
74 12
        return $ret;
75
    }
76
}
77