Passed
Pull Request — master (#55)
by Tim
01:51
created

NCNameTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 5
c 0
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validNCName() 0 6 3
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 NCNameTrait
16
{
17
    /** @var string */
18
    private static string $ncname_regex = '/^[\p{L}a-zA-Z-][\w.-]+$/Du';
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 validNCName(string $value, string $message = ''): void
34
    {
35
        if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false) {
36
            throw new InvalidArgumentException(sprintf(
37
                $message ?: '\'%s\' is not a valid non-colonized name (NCName)',
38
                $value,
39
            ));
40
        }
41
    }
42
}
43