-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assertion and type-class for xs:gYear
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Quality control
|
||
'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'], | ||
]; | ||
} | ||
} |