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 10, 2025
1 parent bf7c173 commit de63a69
Show file tree
Hide file tree
Showing 64 changed files with 2,334 additions and 62 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 validURI(string $value, string $message = ''): void
{
parent::validURI(
$value,
$message ?: '\'%s\' is not a valid anyURI',
InvalidArgumentException::class,
);
}
}
55 changes: 43 additions & 12 deletions src/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,64 @@
/**
* @package simplesamlphp/xml-common
*
* @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 nullOrValidDateTime(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 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 = '')
*/
class Assert extends BaseAssert
{
use AnyURITrait;
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;
}
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 = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\p{L}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,
);
}
}
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
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 = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\p{L}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,
);
}
}
42 changes: 42 additions & 0 deletions src/Assert/LangTrait.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 LangTrait
{
/** @var string */
private static string $lang_regex = '/^(i[-]|x[-])?([a-z]{1,8})([-][a-z]{1,8})?$/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 validLang(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$lang_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid xs:language',
$value,
));
}
}
}
25 changes: 2 additions & 23 deletions src/Assert/NamesTrait.php → src/Assert/NCNameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
/**
* @package simplesamlphp/xml-common
*/
trait NamesTrait
trait NCNameTrait
{
/** @var string */
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D';

/** @var string */
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/D';
private static string $ncname_regex = '/^[\p{L}a-zA-Z-][\w.-]+$/Du';

/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
Expand All @@ -42,22 +39,4 @@ protected static function validNCName(string $value, string $message = ''): void
));
}
}


/**
* @param string $value
* @param string $message
*/
protected static function validQName(string $value, string $message = ''): void
{
if (
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$qname_regex]]) === false &&
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false
) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid qualified name (QName)',
$value,
));
}
}
}
Loading

0 comments on commit de63a69

Please sign in to comment.