Passed
Push — master ( 2b97dd...4cf6b1 )
by Jonathan
20:51
created

AssertDocumentDividerTrait::getDocumentDividers()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.004
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://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\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
    /**
32
     * @param mixed  $value
33
     * @param array  $values
34
     * @param string $message
35
     */
36
    abstract public static function oneOf($value, array $values, $message = '');
37
38
    /**
39
     * Check value is a valid document divider
40
     *
41
     * @param int    $value
42
     * @param string $message
43
     *
44
     * @return void
45
     * @throws InvalidArgumentException
46
     */
47 12
    public static function assertDocumentDivider(int $value, string $message = ''): void
48
    {
49 12
        $haystack = self::getDocumentDividers();
50 12
        $format   = $message ?: '"%d" contains an unsupported document divider';
51 12
        $message  = sprintf($format, $value);
52
53 12
        self::oneOf($value, $haystack, $message);
54 6
    }
55
56
    /**
57
     * Return document dividers array
58
     *
59
     * @return array
60
     */
61 12
    private static function getDocumentDividers(): array
62
    {
63 12
        $constants = [];
64
65
        try {
66 12
            $reportingCloud  = new ReportingCloud();
67 12
            $reflectionClass = new ReflectionClass($reportingCloud);
68 12
            $constants       = $reflectionClass->getConstants();
69 12
            unset($reportingCloud);
70
        } catch (ReflectionException $e) {
71
            // continue
72
        }
73
74
        $ret = array_filter($constants, function (string $constantKey): bool {
75 12
            if (StringUtils::startsWith($constantKey, 'DOCUMENT_DIVIDER_')) {
76 12
                return true;
77
            }
78 12
            return false;
79 12
        }, ARRAY_FILTER_USE_KEY);
80
81 12
        $ret = array_values($ret);
82
83 12
        return $ret;
84
    }
85
}
86