Skip to content

Commit

Permalink
Create type-classes for all xsd-types
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 11, 2025
1 parent bf7c173 commit 3e80257
Show file tree
Hide file tree
Showing 78 changed files with 2,950 additions and 73 deletions.
35 changes: 35 additions & 0 deletions src/Assert/AnyURITrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait AnyURITrait
{
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validAnyURI(string $value, string $message = ''): void
{
parent::validURI(
$value,
$message ?: '\'%s\' is not a valid xs:anyURI',
InvalidArgumentException::class,
);
}
}
70 changes: 58 additions & 12 deletions src/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,79 @@
/**
* @package simplesamlphp/xml-common
*
* @method static void validAnyURI(mixed $value, string $message = '', string $exception = '')
* @method static void validDate(mixed $value, string $message = '', string $exception = '')
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
* @method static void validEntity(mixed $value, string $message = '', string $exception = '')
* @method static void validEntities(mixed $value, string $message = '', string $exception = '')
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void validID(mixed $value, string $message = '', string $exception = '')
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
* @method static void validName(mixed $value, string $message = '', string $exception = '')
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
* @method static void validTime(mixed $value, string $message = '', string $exception = '')
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidEntity(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidEntities(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidTime(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void allValidEntity(mixed $value, string $message = '', string $exception = '')
* @method static void allValidEntities(mixed $value, string $message = '', string $exception = '')
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidTime(mixed $value, string $message = '', string $exception = '')
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
*/
class Assert extends BaseAssert
{
use AnyURITrait;
use DateTrait;
use DateTimeTrait;
use DurationTrait;
use HexBinTrait;
use NamesTrait;
use TokensTrait;
use HexBinaryTrait;
use EntitiesTrait;
use EntityTrait;
use IDTrait;
use IDRefTrait;
use IDRefsTrait;
use LangTrait;
use NameTrait;
use NCNameTrait;
use NMTokenTrait;
use NMTokensTrait;
use QNameTrait;
use TimeTrait;
use YearMonthTrait;
}
2 changes: 1 addition & 1 deletion src/Assert/DateTimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trait DateTimeTrait
{
/** @var string */
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';
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';

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
Expand Down
42 changes: 42 additions & 0 deletions src/Assert/DateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

use function filter_var;
use function sprintf;

/**
* @package simplesamlphp/xml-common
*/
trait DateTrait
{
/** @var string */
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';

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validDate(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$date_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid xs:date',
$value,
));
}
}
}
42 changes: 42 additions & 0 deletions src/Assert/EntitiesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

use function filter_var;
use function sprintf;

/**
* @package simplesamlphp/xml-common
*/
trait EntitiesTrait
{
/** @var string */
private static string $entities_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/Du';

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validEntities(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$entities_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid xs:ENTITIES',
$value,
));
}
}
}
35 changes: 35 additions & 0 deletions src/Assert/EntityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait EntityTrait
{
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validEntity(string $value, string $message = ''): void
{
Assert::validNCName(
$value,
$message ?: '\'%s\' is not a valid xs:Entity',
InvalidArgumentException::class,
);
}
}
4 changes: 2 additions & 2 deletions src/Assert/HexBinTrait.php → src/Assert/HexBinaryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @package simplesamlphp/xml-common
*/
trait HexBinTrait
trait HexBinaryTrait
{
/** @var string */
private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';
Expand Down Expand Up @@ -40,7 +40,7 @@ protected static function validHexBinary(string $value, string $message = ''): v

if ($result === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid hexBinary string',
$message ?: '\'%s\' is not a valid xs:hexBinary',
$value,
));
}
Expand Down
35 changes: 35 additions & 0 deletions src/Assert/IDRefTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait IDRefTrait
{
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validIDRef(string $value, string $message = ''): void
{
Assert::validNCName(
$value,
$message ?: '\'%s\' is not a xs:IDREF',
InvalidArgumentException::class,
);
}
}
42 changes: 42 additions & 0 deletions src/Assert/IDRefsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

use function filter_var;
use function sprintf;

/**
* @package simplesamlphp/xml-common
*/
trait IDRefsTrait
{
/** @var string */
private static string $idrefs_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/Du';

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validIDRefs(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$idrefs_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid xs:IDREFS',
$value,
));
}
}
}
35 changes: 35 additions & 0 deletions src/Assert/IDTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait IDTrait
{
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/


/**
* @param string $value
* @param string $message
*/
protected static function validID(string $value, string $message = ''): void
{
Assert::validNCName(
$value,
$message ?: '\'%s\' is not a valid xs:ID',
InvalidArgumentException::class,
);
}
}
Loading

0 comments on commit 3e80257

Please sign in to comment.