Skip to content

Commit 39c318d

Browse files
committed
Create assertion and type-class for xs:language
1 parent deb4e8d commit 39c318d

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

src/Assert/LangTrait.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 LangTrait
13+
{
14+
/** @var string */
15+
private static string $lang_regex = '/^([a-z]{2}|[i]-[a-z]+|[x]-[a-z]{1,8})(-[a-z]{1,8})*$/Di';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validLang(string $value, string $message = ''): void
23+
{
24+
Assert::regex(
25+
$value,
26+
self::$lang_regex,
27+
$message ?: '%s is not a valid xs:language',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/LangValue.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 LangValue extends TokenValue
14+
{
15+
/**
16+
* Validate the value.
17+
*
18+
* @param string $value
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateValue(string $value): void
23+
{
24+
// Note: content must already be sanitized before validating
25+
Assert::validLang($this->sanitizeValue($value), SchemaViolationException::class);
26+
}
27+
}

tests/Assert/LangTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\LangTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class LangTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $lang
24+
*/
25+
#[DataProvider('provideLang')]
26+
public function testValidLang(bool $shouldPass, string $lang): void
27+
{
28+
try {
29+
Assert::validLang($lang);
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 provideLang(): array
41+
{
42+
return [
43+
'empty string' => [false, ''],
44+
'one part' => [true, 'es'],
45+
'two parts' => [true, 'en-US'],
46+
'many parts' => [true, 'es-this-goes-on-forever'],
47+
'too long' => [false, 'toolonglanguage'],
48+
'x-case' => [true, 'x-klingon'],
49+
'i-case' => [true, 'i-sami-no'],
50+
];
51+
}
52+
}

tests/Type/LangValueTest.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\LangValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\LangValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(LangValue::class)]
19+
final class LangValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $lang
24+
*/
25+
#[DataProvider('provideLang')]
26+
public function testLang(bool $shouldPass, string $lang): void
27+
{
28+
try {
29+
LangValue::fromString($lang);
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 provideLang(): array
41+
{
42+
return [
43+
'empty string' => [false, ''],
44+
'one part' => [true, 'es'],
45+
'two parts' => [true, 'en-US'],
46+
'many parts' => [true, 'es-this-goes-on-forever'],
47+
'too long' => [false, 'toolonglanguage'],
48+
'x-case' => [true, 'x-klingon'],
49+
'i-case' => [true, 'i-sami-no'],
50+
'normalization' => [true, ' en-US '],
51+
];
52+
}
53+
}

0 commit comments

Comments
 (0)