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

TokensTrait::validNMTokens()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 2
nop 2
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 TokensTrait
16
{
17
    /** @var string */
18
    private static string $nmtoken_regex = '/^[\w.:-]+$/Du';
19
20
    /** @var string */
21
    private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/Du';
22
23
    /***********************************************************************************
24
     *  NOTE:  Custom assertions may be added below this line.                         *
25
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
26
     *          through __callStatic().                                                *
27
     *         Assertions marked `public` are called directly and will                 *
28
     *          not handle any custom exception passed to it.                          *
29
     ***********************************************************************************/
30
31
32
    /**
33
     * @param string $value
34
     * @param string $message
35
     */
36
    protected static function validNMToken(string $value, string $message = ''): void
37
    {
38
        if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$nmtoken_regex]]) === false) {
39
            throw new InvalidArgumentException(sprintf(
40
                $message ?: '\'%s\' is not a valid xs:NMTOKEN',
41
                $value,
42
            ));
43
        }
44
    }
45
46
47
    /**
48
     * @param string $value
49
     * @param string $message
50
     */
51
    protected static function validNMTokens(string $value, string $message = ''): void
52
    {
53
        if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$nmtokens_regex]]) === false) {
54
            throw new InvalidArgumentException(sprintf(
55
                $message ?: '\'%s\' is not a valid xs:NMTOKENS',
56
                $value,
57
            ));
58
        }
59
    }
60
}
61