Skip to content

Commit

Permalink
Create assertion and type-class for xs:gYear
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 14, 2025
1 parent fa2f710 commit c5b1a18
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @method static void validUnsignedInt(mixed $value, string $message = '', string $exception = '')
* @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void validUnsignedShort(mixed $value, string $message = '', string $exception = '')
* @method static void validYear(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 nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
Expand Down Expand Up @@ -90,6 +91,7 @@
* @method static void nullOrValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidYear(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 allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
Expand Down Expand Up @@ -131,6 +133,7 @@
* @method static void allValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
* @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
* @method static void allValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
* @method static void allValidYear(mixed $value, string $message = '', string $exception = '')
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
*/
class Assert extends BaseAssert
Expand Down Expand Up @@ -175,5 +178,6 @@ class Assert extends BaseAssert
use UnsignedIntTrait;
use UnsignedLongTrait;
use UnsignedShortTrait;
use YearTrait;
use YearMonthTrait;
}
31 changes: 31 additions & 0 deletions src/Assert/YearTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Assert;

use InvalidArgumentException;

/**
* @package simplesamlphp/xml-common
*/
trait YearTrait
{
/** @var string */
private static string $year_regex = '/^-?([1-9][0-9]*|[0-9]{4})((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?$/Di';


/**
* @param string $value
* @param string $message
*/
protected static function validYear(string $value, string $message = ''): void
{
parent::regex(
$value,
self::$year_regex,
$message ?: '%s is not a valid xs:gYear',
InvalidArgumentException::class,
);
}
}
41 changes: 41 additions & 0 deletions src/Type/YearValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Type;

use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
class YearValue extends AbstractValueType
{
/**
* Sanitize the value.
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
}


/**
* Validate the value.
*
* @param string $value
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
* @return void
*/
protected function validateValue(string $value): void
{
Assert::validYear($this->sanitizeValue($value), SchemaViolationException::class);
}
}
53 changes: 53 additions & 0 deletions tests/Assert/YearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML\Assert;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\XML\Assert\Assert;

/**
* Class \SimpleSAML\Test\XML\Assert\YearTest
*
* @package simplesamlphp/xml-common
*/
#[CoversClass(Assert::class)]
final class YearTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $year
*/
#[DataProvider('provideYear')]
public function testValidYear(bool $shouldPass, string $year): void
{
try {
Assert::validYear($year);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideYear(): array
{
return [
'empty' => [false, ''],
'valid' => [true, '2001'],
'whitespace' => [false, ' 2001 '],
'valid numeric timezone' => [true, '2001+02:00'],
'valid Zulu timezone' => [true, '2001Z'],
'valid 00:00 timezone' => [true, '2001+00:00'],
'2001 BC' => [true, '-2001'],
'20000 BC' => [true, '-20000'],
];
}
}
54 changes: 54 additions & 0 deletions tests/Type/YearValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML\Type;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Type\YearValue;

/**
* Class \SimpleSAML\Test\Type\YearValueTest
*
* @package simplesamlphp/xml-common
*/
#[CoversClass(YearValue::class)]
final class YearValueTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $year
*/
#[DataProvider('provideYear')]
public function testYear(bool $shouldPass, string $year): void
{
try {
YearValue::fromString($year);
$this->assertTrue($shouldPass);
} catch (SchemaViolationException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideYear(): array
{
return [
'empty' => [false, ''],
'whitespace collapse' => [false, ' 2001 '],

Check failure on line 44 in tests/Type/YearValueTest.php

View workflow job for this annotation

GitHub Actions / Quality control

Array has 2 duplicate keys with value 'whitespace collapse' ('whitespace collapse', 'whitespace collapse').

Check failure on line 44 in tests/Type/YearValueTest.php

View workflow job for this annotation

GitHub Actions / Quality control

Array has 2 duplicate keys with value 'whitespace collapse' ('whitespace collapse', 'whitespace collapse').
'valid' => [true, '2001'],
'valid numeric timezone' => [true, '2001+02:00'],
'valid Zulu timezone' => [true, '2001Z'],
'whitespace collapse' => [true, ' 2001Z '],
'valid 00:00 timezone' => [true, '2001+00:00'],
'2001 BC' => [true, '-2001'],
'20000 BC' => [true, '-20000'],
];
}
}

0 comments on commit c5b1a18

Please sign in to comment.