Passed
Push — master ( 37dcaf...03656c )
by Tim
12:27 queued 27s
created

HexBinTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validHexBinary() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\Assert;
6
7
use InvalidArgumentException;
8
9
use function filter_var;
10
use function sprintf;
11
12
/**
13
 * @package simplesamlphp/xml-common
14
 */
15
trait HexBinTrait
16
{
17
    /** @var string */
18
    private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';
19
20
    /***********************************************************************************
21
     *  NOTE:  Custom assertions may be added below this line.                         *
22
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
23
     *          through __callStatic().                                                *
24
     *         Assertions marked `public` are called directly and will                 *
25
     *          not handle any custom exception passed to it.                          *
26
     ***********************************************************************************/
27
28
29
    /**
30
     * @param string $value
31
     * @param string $message
32
     */
33
    protected static function validHexBinary(string $value, string $message = ''): void
34
    {
35
        $result = true;
36
37
        if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$hexbin_regex]]) === false) {
38
            $result = false;
39
        }
40
41
        if ($result === false) {
42
            throw new InvalidArgumentException(sprintf(
43
                $message ?: '\'%s\' is not a valid hexBinary string',
44
                $value,
45
            ));
46
        }
47
    }
48
}
49