Skip to content

Commit c2b9b8e

Browse files
committed
Create assertion and type-class for xs:gYear
1 parent 5cb149e commit c2b9b8e

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @method static void validUnsignedInt(mixed $value, string $message = '', string $exception = '')
5050
* @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '')
5151
* @method static void validUnsignedShort(mixed $value, string $message = '', string $exception = '')
52+
* @method static void validYear(mixed $value, string $message = '', string $exception = '')
5253
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
5354
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
5455
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
@@ -90,6 +91,7 @@
9091
* @method static void nullOrValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
9192
* @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
9293
* @method static void nullOrValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
94+
* @method static void nullOrValidYear(mixed $value, string $message = '', string $exception = '')
9395
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
9496
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
9597
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
@@ -131,6 +133,7 @@
131133
* @method static void allValidUnsignedInt(mixed $value, string $message = '', string $exception = '')
132134
* @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '')
133135
* @method static void allValidUnsignedShort(mixed $value, string $message = '', string $exception = '')
136+
* @method static void allValidYear(mixed $value, string $message = '', string $exception = '')
134137
* @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '')
135138
*/
136139
class Assert extends BaseAssert
@@ -175,5 +178,6 @@ class Assert extends BaseAssert
175178
use UnsignedIntTrait;
176179
use UnsignedLongTrait;
177180
use UnsignedShortTrait;
181+
use YearTrait;
178182
use YearMonthTrait;
179183
}

src/Assert/YearTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 YearTrait
13+
{
14+
/** @var string */
15+
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';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validYear(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$year_regex,
27+
$message ?: '%s is not a valid xs:gYear',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/YearValue.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
use function preg_replace;
11+
use function trim;
12+
13+
/**
14+
* @package simplesaml/xml-common
15+
*/
16+
class YearValue extends AbstractValueType
17+
{
18+
/**
19+
* Sanitize the value.
20+
*
21+
* @param string $value The unsanitized value
22+
* @return string
23+
*/
24+
protected function sanitizeValue(string $value): string
25+
{
26+
return trim(preg_replace('/\s+/', ' ', $value));
27+
}
28+
29+
30+
/**
31+
* Validate the value.
32+
*
33+
* @param string $value
34+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
35+
* @return void
36+
*/
37+
protected function validateValue(string $value): void
38+
{
39+
Assert::validYear($this->sanitizeValue($value), SchemaViolationException::class);
40+
}
41+
}

tests/Assert/YearTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\AssertionFailedException;
11+
use SimpleSAML\XML\Assert\Assert;
12+
13+
/**
14+
* Class \SimpleSAML\Test\XML\Assert\YearTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class YearTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $year
24+
*/
25+
#[DataProvider('provideYear')]
26+
public function testValidYear(bool $shouldPass, string $year): void
27+
{
28+
try {
29+
Assert::validYear($year);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideYear(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid' => [true, '2001'],
45+
'whitespace' => [false, ' 2001 '],
46+
'valid numeric timezone' => [true, '2001+02:00'],
47+
'valid Zulu timezone' => [true, '2001Z'],
48+
'valid 00:00 timezone' => [true, '2001+00:00'],
49+
'2001 BC' => [true, '-2001'],
50+
'20000 BC' => [true, '-20000'],
51+
];
52+
}
53+
}

tests/Type/YearValueTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Type\YearValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\YearValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(YearValue::class)]
19+
final class YearValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $year
24+
*/
25+
#[DataProvider('provideYear')]
26+
public function testYear(bool $shouldPass, string $year): void
27+
{
28+
try {
29+
YearValue::fromString($year);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideYear(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid' => [true, '2001'],
45+
'valid numeric timezone' => [true, '2001+02:00'],
46+
'valid Zulu timezone' => [true, '2001Z'],
47+
'whitespace collapse' => [true, ' 2001Z '],
48+
'valid 00:00 timezone' => [true, '2001+00:00'],
49+
'2001 BC' => [true, '-2001'],
50+
'20000 BC' => [true, '-20000'],
51+
];
52+
}
53+
}

0 commit comments

Comments
 (0)