Passed
Pull Request — master (#55)
by Tim
02:04
created

QNameTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validQName() 0 9 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
11
/**
12
 * @package simplesamlphp/xml-common
13
 */
14
trait QNameTrait
15
{
16
    /** @var string */
17
    private static string $qname_regex = '/^([\p{L}a-zA-Z-][\w.-]*)(:[\p{L}a-zA-Z-][\w.-]*)$/Du';
18
19
    /***********************************************************************************
20
     *  NOTE:  Custom assertions may be added below this line.                         *
21
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
22
     *          through __callStatic().                                                *
23
     *         Assertions marked `public` are called directly and will                 *
24
     *          not handle any custom exception passed to it.                          *
25
     ***********************************************************************************/
26
27
28
    /**
29
     * @param string $value
30
     * @param string $message
31
     */
32
    protected static function validQName(string $value, string $message = ''): void
33
    {
34
        if (
35
            filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$qname_regex]]) === false
36
        ) {
37
            Assert::validNCName(
38
                $value,
39
                $message ?: '\'%s\' is not a valid xs:QName',
40
                InvalidArgumentException::class,
41
            );
42
        }
43
    }
44
}
45