Skip to content

Commit 2c34520

Browse files
committed
Create type-classes for all xsd-types
1 parent bf7c173 commit 2c34520

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2353
-62
lines changed

src/Assert/AnyURITrait.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 AnyURITrait
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 validURI(string $value, string $message = ''): void
28+
{
29+
parent::validURI(
30+
$value,
31+
$message ?: '\'%s\' is not a valid anyURI',
32+
InvalidArgumentException::class,
33+
);
34+
}
35+
}

src/Assert/Assert.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,64 @@
99
/**
1010
* @package simplesamlphp/xml-common
1111
*
12+
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
13+
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
14+
* @method static void validEntity(mixed $value, string $message = '', string $exception = '')
15+
* @method static void validEntities(mixed $value, string $message = '', string $exception = '')
1216
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
17+
* @method static void validID(mixed $value, string $message = '', string $exception = '')
18+
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
19+
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
20+
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
21+
* @method static void validName(mixed $value, string $message = '', string $exception = '')
22+
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
1323
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
1424
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
15-
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
16-
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
17-
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
1825
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
26+
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
27+
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
28+
* @method static void nullOrValidEntity(mixed $value, string $message = '', string $exception = '')
29+
* @method static void nullOrValidEntities(mixed $value, string $message = '', string $exception = '')
1930
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
31+
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
32+
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
33+
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
34+
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
35+
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
36+
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
2037
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
2138
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
22-
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
23-
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
24-
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
2539
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
40+
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
41+
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
42+
* @method static void allValidEntity(mixed $value, string $message = '', string $exception = '')
43+
* @method static void allValidEntities(mixed $value, string $message = '', string $exception = '')
2644
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
45+
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
46+
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
47+
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
48+
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
49+
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
50+
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
2751
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
2852
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
29-
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
30-
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
31-
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
3253
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
3354
*/
3455
class Assert extends BaseAssert
3556
{
57+
use AnyURITrait;
3658
use DateTimeTrait;
3759
use DurationTrait;
38-
use HexBinTrait;
39-
use NamesTrait;
40-
use TokensTrait;
60+
use HexBinaryTrait;
61+
use EntitiesTrait;
62+
use EntityTrait;
63+
use IDTrait;
64+
use IDRefTrait;
65+
use IDRefsTrait;
66+
use LangTrait;
67+
use NameTrait;
68+
use NCNameTrait;
69+
use NMTokenTrait;
70+
use NMTokensTrait;
71+
use QNameTrait;
4172
}

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 = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\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 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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @package simplesamlphp/xml-common
1414
*/
15-
trait HexBinTrait
15+
trait HexBinaryTrait
1616
{
1717
/** @var string */
1818
private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';

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 = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\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 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
private static string $lang_regex = '/^([a-z]{2}|[i]-[a-z]+|[x]-[a-z]{1,8})(-[a-z]{1,8})*$/Di';
20+
21+
/***********************************************************************************
22+
* NOTE: Custom assertions may be added below this line. *
23+
* They SHOULD be marked as `protected` to ensure the call is forced *
24+
* through __callStatic(). *
25+
* Assertions marked `public` are called directly and will *
26+
* not handle any custom exception passed to it. *
27+
***********************************************************************************/
28+
29+
30+
/**
31+
* @param string $value
32+
* @param string $message
33+
*/
34+
protected static function validLang(string $value, string $message = ''): void
35+
{
36+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$lang_regex]]) === false) {
37+
throw new InvalidArgumentException(sprintf(
38+
$message ?: '\'%s\' is not a valid xs:language',
39+
$value,
40+
));
41+
}
42+
}
43+
}
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
/**
1313
* @package simplesamlphp/xml-common
1414
*/
15-
trait NamesTrait
15+
trait NCNameTrait
1616
{
1717
/** @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';
18+
private static string $ncname_regex = '/^[\p{L}a-zA-Z-][\w.-]+$/Du';
2219

2320
/***********************************************************************************
2421
* NOTE: Custom assertions may be added below this line. *
@@ -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
}

0 commit comments

Comments
 (0)