Skip to content

Commit 3e80257

Browse files
committed
Create type-classes for all xsd-types
1 parent bf7c173 commit 3e80257

Some content is hidden

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

78 files changed

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

src/Assert/Assert.php

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,79 @@
99
/**
1010
* @package simplesamlphp/xml-common
1111
*
12+
* @method static void validAnyURI(mixed $value, string $message = '', string $exception = '')
13+
* @method static void validDate(mixed $value, string $message = '', string $exception = '')
14+
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
15+
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
16+
* @method static void validEntity(mixed $value, string $message = '', string $exception = '')
17+
* @method static void validEntities(mixed $value, string $message = '', string $exception = '')
1218
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
19+
* @method static void validID(mixed $value, string $message = '', string $exception = '')
20+
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
21+
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
22+
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
23+
* @method static void validName(mixed $value, string $message = '', string $exception = '')
24+
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
1325
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
1426
* @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 = '')
1827
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
28+
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
29+
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
30+
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
31+
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
32+
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
33+
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
34+
* @method static void nullOrValidEntity(mixed $value, string $message = '', string $exception = '')
35+
* @method static void nullOrValidEntities(mixed $value, string $message = '', string $exception = '')
1936
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
37+
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
38+
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
39+
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
40+
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
41+
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
42+
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
2043
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
2144
* @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 = '')
2545
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
46+
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
47+
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
48+
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
49+
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
50+
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
51+
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
52+
* @method static void allValidEntity(mixed $value, string $message = '', string $exception = '')
53+
* @method static void allValidEntities(mixed $value, string $message = '', string $exception = '')
2654
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
55+
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
56+
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
57+
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
58+
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
59+
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
60+
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
2761
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
2862
* @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 = '')
3263
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
64+
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
65+
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
3366
*/
3467
class Assert extends BaseAssert
3568
{
69+
use AnyURITrait;
70+
use DateTrait;
3671
use DateTimeTrait;
3772
use DurationTrait;
38-
use HexBinTrait;
39-
use NamesTrait;
40-
use TokensTrait;
73+
use HexBinaryTrait;
74+
use EntitiesTrait;
75+
use EntityTrait;
76+
use IDTrait;
77+
use IDRefTrait;
78+
use IDRefsTrait;
79+
use LangTrait;
80+
use NameTrait;
81+
use NCNameTrait;
82+
use NMTokenTrait;
83+
use NMTokensTrait;
84+
use QNameTrait;
85+
use TimeTrait;
86+
use YearMonthTrait;
4187
}

src/Assert/DateTimeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
trait DateTimeTrait
1616
{
1717
/** @var string */
18-
private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/i';
18+
private static string $datetime_regex = '/^-?([1-9][0-9]*|[0-9]{4})-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-9]{0,6})?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?$/Di';
1919

2020
/***********************************************************************************
2121
* NOTE: Custom assertions may be added below this line. *

src/Assert/DateTrait.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 DateTrait
16+
{
17+
/** @var string */
18+
private static string $date_regex = '/^-?([1-9][0-9]*|[0-9]{4})-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?$/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 validDate(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$date_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:date',
38+
$value,
39+
));
40+
}
41+
}
42+
}

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.-]*)*$/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: 2 additions & 2 deletions
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';
@@ -40,7 +40,7 @@ protected static function validHexBinary(string $value, string $message = ''): v
4040

4141
if ($result === false) {
4242
throw new InvalidArgumentException(sprintf(
43-
$message ?: '\'%s\' is not a valid hexBinary string',
43+
$message ?: '\'%s\' is not a valid xs:hexBinary',
4444
$value,
4545
));
4646
}

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.-]*)*$/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+
}

0 commit comments

Comments
 (0)