Skip to content

Commit 8902f73

Browse files
committed
Create type-classes for all xsd-types
1 parent 3f4af48 commit 8902f73

33 files changed

+828
-57
lines changed

src/Assert/Assert.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class Assert extends BaseAssert
3636
use DateTimeTrait;
3737
use DurationTrait;
3838
use HexBinTrait;
39-
use NamesTrait;
40-
use TokensTrait;
39+
use EntitiesTrait;
40+
use EntityTrait;
41+
use IDTrait;
42+
use IDRefTrait;
43+
use IDRefsTrait;
44+
use LangTrait;
45+
use NameTrait;
46+
use NCNameTrait;
47+
use NMTokenTrait;
48+
use NMTokensTrait;
49+
use QNameTrait;
4150
}

src/Assert/EntitiesTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 EntitiesTrait
16+
{
17+
/** @var string */
18+
private static string $entities_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/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 validEntities(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$entities_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:ENTITIES',
38+
$value,
39+
));
40+
}
41+
}
42+
}

src/Assert/EntityTrait.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait EntityTrait
13+
{
14+
/***********************************************************************************
15+
* NOTE: Custom assertions may be added below this line. *
16+
* They SHOULD be marked as `protected` to ensure the call is forced *
17+
* through __callStatic(). *
18+
* Assertions marked `public` are called directly and will *
19+
* not handle any custom exception passed to it. *
20+
***********************************************************************************/
21+
22+
23+
/**
24+
* @param string $value
25+
* @param string $message
26+
*/
27+
protected static function validEntity(string $value, string $message = ''): void
28+
{
29+
Assert::validNCName(
30+
$value,
31+
$message ?: '\'%s\' is not a valid xs:Entity',
32+
InvalidArgumentException::class,
33+
);
34+
}
35+
}

src/Assert/IDRefTrait.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait IDRefTrait
13+
{
14+
/***********************************************************************************
15+
* NOTE: Custom assertions may be added below this line. *
16+
* They SHOULD be marked as `protected` to ensure the call is forced *
17+
* through __callStatic(). *
18+
* Assertions marked `public` are called directly and will *
19+
* not handle any custom exception passed to it. *
20+
***********************************************************************************/
21+
22+
23+
/**
24+
* @param string $value
25+
* @param string $message
26+
*/
27+
protected static function validIDRef(string $value, string $message = ''): void
28+
{
29+
Assert::validNCName(
30+
$value,
31+
$message ?: '\'%s\' is not a xs:IDREF',
32+
InvalidArgumentException::class,
33+
);
34+
}
35+
}

src/Assert/IDRefsTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 IDRefsTrait
16+
{
17+
/** @var string */
18+
private static string $idrefs_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/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 validIDRefs(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$idrefs_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:IDREFS',
38+
$value,
39+
));
40+
}
41+
}
42+
}

src/Assert/IDTrait.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait IDTrait
13+
{
14+
/***********************************************************************************
15+
* NOTE: Custom assertions may be added below this line. *
16+
* They SHOULD be marked as `protected` to ensure the call is forced *
17+
* through __callStatic(). *
18+
* Assertions marked `public` are called directly and will *
19+
* not handle any custom exception passed to it. *
20+
***********************************************************************************/
21+
22+
23+
/**
24+
* @param string $value
25+
* @param string $message
26+
*/
27+
protected static function validID(string $value, string $message = ''): void
28+
{
29+
Assert::validNCName(
30+
$value,
31+
$message ?: '\'%s\' is not a valid xs:ID',
32+
InvalidArgumentException::class,
33+
);
34+
}
35+
}

src/Assert/LangTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 LangTrait
16+
{
17+
/** @var string */
18+
private static string $lang_regex = '/^(i[-]|x[-])?([a-z]{1,8})([-][a-z]{1,8})?$/Di';
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 validLang(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$lang_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:language',
38+
$value,
39+
));
40+
}
41+
}
42+
}
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
/**
1313
* @package simplesamlphp/xml-common
1414
*/
15-
trait NamesTrait
15+
trait NCNameTrait
1616
{
1717
/** @var string */
1818
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D';
1919

20-
/** @var string */
21-
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/D';
22-
2320
/***********************************************************************************
2421
* NOTE: Custom assertions may be added below this line. *
2522
* They SHOULD be marked as `protected` to ensure the call is forced *
@@ -42,22 +39,4 @@ protected static function validNCName(string $value, string $message = ''): void
4239
));
4340
}
4441
}
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-
}
6342
}

src/Assert/NMTokenTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 NMTokenTrait
16+
{
17+
/** @var string */
18+
private static string $nmtoken_regex = '/^[\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 validNMToken(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$nmtoken_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:NMTOKEN',
38+
$value,
39+
));
40+
}
41+
}
42+
}
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
/**
1313
* @package simplesamlphp/xml-common
1414
*/
15-
trait TokensTrait
15+
trait NMTokensTrait
1616
{
17-
/** @var string */
18-
private static string $nmtoken_regex = '/^[\w.:-]+$/Du';
19-
2017
/** @var string */
2118
private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/Du';
2219

@@ -29,21 +26,6 @@ trait TokensTrait
2926
***********************************************************************************/
3027

3128

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-
4729
/**
4830
* @param string $value
4931
* @param string $message

0 commit comments

Comments
 (0)