Skip to content

Commit b52ce96

Browse files
committed
Create type-classes for all xsd-types
1 parent 8902f73 commit b52ce96

File tree

14 files changed

+396
-10
lines changed

14 files changed

+396
-10
lines changed

src/Assert/EntitiesTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
trait EntitiesTrait
1616
{
1717
/** @var string */
18-
private static string $entities_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/D';
18+
private static string $entities_regex = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\p{L}a-zA-Z-][\w.-]*)*$/Du';
1919

2020
/***********************************************************************************
2121
* NOTE: Custom assertions may be added below this line. *

src/Assert/IDRefsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
trait IDRefsTrait
1616
{
1717
/** @var string */
18-
private static string $idrefs_regex = '/^([a-zA-Z_][\w.-]*)([\s][a-zA-Z_][\w.-]*)*$/D';
18+
private static string $idrefs_regex = '/^([\p{L}a-zA-Z-][\w.-]*)([\s][\p{L}a-zA-Z-][\w.-]*)*$/Du';
1919

2020
/***********************************************************************************
2121
* NOTE: Custom assertions may be added below this line. *

src/Assert/NCNameTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
trait NCNameTrait
1616
{
1717
/** @var string */
18-
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D';
18+
private static string $ncname_regex = '/^[\p{L}a-zA-Z-][\w.-]+$/Du';
1919

2020
/***********************************************************************************
2121
* NOTE: Custom assertions may be added below this line. *

src/Type/EntitiesValue.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class EntitiesValue extends TokenValue
14+
{
15+
/**
16+
* Validate the content.
17+
*
18+
* @param string $content
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateContent(string $content): void
23+
{
24+
// Note: content must already be sanitized before validating
25+
Assert::validEntities($this->sanitizeContent($content), SchemaViolationException::class);
26+
}
27+
}

src/Type/IDRefsValue.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class IDRefsValue extends TokenValue
14+
{
15+
/**
16+
* Validate the content.
17+
*
18+
* @param string $content
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateContent(string $content): void
23+
{
24+
// Note: content must already be sanitized before validating
25+
Assert::validIDRefs($this->sanitizeContent($content), SchemaViolationException::class);
26+
}
27+
}

src/Type/NMTokensValue.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
use SimpleSAML\XML\Assert\Assert;
88
use SimpleSAML\XML\Exception\SchemaViolationException;
99

10-
use function preg_replace;
11-
use function trim;
12-
1310
/**
1411
* @package simplesaml/xml-common
1512
*/

tests/Assert/EntitiesTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\EntitiesTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class EntitiesTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $entities
24+
*/
25+
#[DataProvider('provideEntities')]
26+
public function testValidEntities(bool $shouldPass, string $entities): void
27+
{
28+
try {
29+
Assert::validEntities($entities);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideEntities(): array
41+
{
42+
return [
43+
[true, 'Snoopy'],
44+
[true, 'CMS'],
45+
[true, 'fööbár'],
46+
[true, '-1950-10-04'],
47+
[false, '0836217462 0836217463'],
48+
[true, 'foo bar'],
49+
// Quotes are forbidden
50+
[false, 'foo "bar" baz'],
51+
// Commas are forbidden
52+
[false, 'foo,bar'],
53+
// Trailing newlines are forbidden
54+
[false, "foobar\n"],
55+
];
56+
}
57+
}

tests/Assert/EntityTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\EntityTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class EntityTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $entity
24+
*/
25+
#[DataProvider('provideEntity')]
26+
public function testValidEntity(bool $shouldPass, string $entity): void
27+
{
28+
try {
29+
Assert::validEntity($entity);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideEntity(): array
41+
{
42+
return [
43+
[true, 'Snoopy'],
44+
[true, 'CMS'],
45+
[true, 'fööbár'],
46+
[true, '-1950-10-04'],
47+
[false, '0836217462'],
48+
// Spaces are forbidden
49+
[false, 'foo bar'],
50+
// Commas are forbidden
51+
[false, 'foo,bar'],
52+
// Trailing newlines are forbidden
53+
[false, "foobar\n"],
54+
];
55+
}
56+
}

tests/Assert/IDRefTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\IDRefTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class IDRefTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $idref
24+
*/
25+
#[DataProvider('provideIDRef')]
26+
public function testValidIDRef(bool $shouldPass, string $idref): void
27+
{
28+
try {
29+
Assert::validIDRef($idref);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideIDRef(): array
41+
{
42+
return [
43+
[true, 'Snoopy'],
44+
[true, 'CMS'],
45+
[true, 'fööbár'],
46+
[true, '-1950-10-04'],
47+
[false, '0836217462'],
48+
// Spaces are forbidden
49+
[false, 'foo bar'],
50+
// Commas are forbidden
51+
[false, 'foo,bar'],
52+
// Trailing newlines are forbidden
53+
[false, "foobar\n"],
54+
];
55+
}
56+
}

tests/Assert/IDRefsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\IDRefsTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class IDRefsTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $idrefs
24+
*/
25+
#[DataProvider('provideIDRefs')]
26+
public function testValidIDRefs(bool $shouldPass, string $idrefs): void
27+
{
28+
try {
29+
Assert::validIDRefs($idrefs);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<int, array{0: bool, 1: string}>
39+
*/
40+
public static function provideIDRefs(): array
41+
{
42+
return [
43+
[true, 'Snoopy'],
44+
[true, 'CMS'],
45+
[true, 'fööbár'],
46+
[true, '-1950-10-04'],
47+
[false, '0836217462 0836217463'],
48+
[true, 'foo bar'],
49+
// Quotes are forbidden
50+
[false, 'foo "bar" baz'],
51+
// Commas are forbidden
52+
[false, 'foo,bar'],
53+
// Trailing newlines are forbidden
54+
[false, "foobar\n"],
55+
];
56+
}
57+
}

0 commit comments

Comments
 (0)