Skip to content

Commit 286c980

Browse files
committed
Create type-classes for all xsd-types
1 parent bf7c173 commit 286c980

Some content is hidden

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

82 files changed

+3130
-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: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,83 @@
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 validDay(mixed $value, string $message = '', string $exception = '')
16+
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
17+
* @method static void validEntity(mixed $value, string $message = '', string $exception = '')
18+
* @method static void validEntities(mixed $value, string $message = '', string $exception = '')
1219
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
20+
* @method static void validID(mixed $value, string $message = '', string $exception = '')
21+
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
22+
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
23+
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
24+
* @method static void validName(mixed $value, string $message = '', string $exception = '')
25+
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
1326
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
1427
* @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 = '')
1828
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
29+
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
30+
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
31+
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
32+
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
33+
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
34+
* @method static void nullOrValidDay(mixed $value, string $message = '', string $exception = '')
35+
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
36+
* @method static void nullOrValidEntity(mixed $value, string $message = '', string $exception = '')
37+
* @method static void nullOrValidEntities(mixed $value, string $message = '', string $exception = '')
1938
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
39+
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
40+
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
41+
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
42+
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
43+
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
44+
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
2045
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
2146
* @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 = '')
2547
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
48+
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
49+
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
50+
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
51+
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
52+
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
53+
* @method static void allValidDay(mixed $value, string $message = '', string $exception = '')
54+
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
55+
* @method static void allValidEntity(mixed $value, string $message = '', string $exception = '')
56+
* @method static void allValidEntities(mixed $value, string $message = '', string $exception = '')
2657
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
58+
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
59+
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
60+
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
61+
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
62+
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
63+
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
2764
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
2865
* @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 = '')
3266
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
67+
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
68+
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
3369
*/
3470
class Assert extends BaseAssert
3571
{
72+
use AnyURITrait;
73+
use DateTrait;
3674
use DateTimeTrait;
75+
use DayTrait;
3776
use DurationTrait;
38-
use HexBinTrait;
39-
use NamesTrait;
40-
use TokensTrait;
77+
use HexBinaryTrait;
78+
use EntitiesTrait;
79+
use EntityTrait;
80+
use IDTrait;
81+
use IDRefTrait;
82+
use IDRefsTrait;
83+
use LangTrait;
84+
use NameTrait;
85+
use NCNameTrait;
86+
use NMTokenTrait;
87+
use NMTokensTrait;
88+
use QNameTrait;
89+
use TimeTrait;
90+
use YearMonthTrait;
4191
}

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/DayTrait.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 DayTrait
16+
{
17+
/** @var string */
18+
private static string $day_regex = '/^---(0[1-9]|1[1-9]|2[1-9]|3[01])((\+|-)([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 validDay(string $value, string $message = ''): void
34+
{
35+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$day_regex]]) === false) {
36+
throw new InvalidArgumentException(sprintf(
37+
$message ?: '\'%s\' is not a valid xs:gDay',
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+
}

0 commit comments

Comments
 (0)