|
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 NamesTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/D'; |
|
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 validNCName(string $value, string $message = ''): void |
|
37
|
|
|
{ |
|
38
|
|
|
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false) { |
|
39
|
|
|
throw new InvalidArgumentException(sprintf( |
|
40
|
|
|
$message ?: '\'%s\' is not a valid non-colonized name (NCName)', |
|
41
|
|
|
$value, |
|
42
|
|
|
)); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $value |
|
49
|
|
|
* @param string $message |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static function validQName(string $value, string $message = ''): void |
|
52
|
|
|
{ |
|
53
|
|
|
if ( |
|
54
|
|
|
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$qname_regex]]) === false && |
|
55
|
|
|
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false |
|
56
|
|
|
) { |
|
57
|
|
|
throw new InvalidArgumentException(sprintf( |
|
58
|
|
|
$message ?: '\'%s\' is not a valid qualified name (QName)', |
|
59
|
|
|
$value, |
|
60
|
|
|
)); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|