Skip to content

Commit 0ef8f5e

Browse files
committed
Add type for xml:lang attribute
1 parent 4ef12f0 commit 0ef8f5e

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/XML/Type/LangValue.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Attribute;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Type\Builtin\LanguageValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class LangValue extends LanguageValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:language';
19+
20+
21+
/**
22+
* Validate the value.
23+
*
24+
* @param string $value
25+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
26+
* @return void
27+
*/
28+
protected function validateValue(string $value): void
29+
{
30+
$sanitized = $this->sanitizeValue($value);
31+
32+
if ($sanitized !== '') {
33+
parent::validateValue($value);
34+
}
35+
}
36+
37+
38+
/**
39+
* Convert this value to an attribute
40+
*
41+
* @return \SimpleSAML\XML\Attribute
42+
*/
43+
public function toAttribute(): Attribute
44+
{
45+
return new Attribute(C::NS_XML, 'xml', 'lang', $this);
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSchema\Type\Helper;
6+
7+
use SimpleSAML\XML\Attribute;
8+
9+
/**
10+
* interface class to be implemented by all the classes that represent a DOM Attribute
11+
*
12+
* @package simplesamlphp/xml-common
13+
*/
14+
interface AttributeTypeInterface extends ValueTypeInterface
15+
{
16+
/**
17+
* Convert this value to an attribute
18+
*
19+
* @return \SimpleSAML\XML\Attribute
20+
*/
21+
public function toAttribute(): Attribute;
22+
}

tests/XML/Type/LangValueTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, DataProviderExternal, DependsOnClass};
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\Assert\LanguageTest;
10+
use SimpleSAML\XML\Constants as C;
11+
use SimpleSAML\XML\Type\LangValue;
12+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13+
14+
/**
15+
* Class \SimpleSAML\Test\XML\Type\LangValueTest
16+
*
17+
* @package simplesamlphp/xml-common
18+
*/
19+
#[CoversClass(LangValue::class)]
20+
final class LangValueTest extends TestCase
21+
{
22+
/**
23+
* @param boolean $shouldPass
24+
* @param string $language
25+
*/
26+
#[DataProvider('provideInvalidLang')]
27+
#[DataProvider('provideValidLang')]
28+
#[DataProviderExternal(LanguageTest::class, 'provideValidLanguage')]
29+
#[DependsOnClass(LanguageTest::class)]
30+
public function testLanguage(bool $shouldPass, string $language): void
31+
{
32+
try {
33+
LangValue::fromString($language);
34+
$this->assertTrue($shouldPass);
35+
} catch (SchemaViolationException $e) {
36+
$this->assertFalse($shouldPass);
37+
}
38+
}
39+
40+
41+
/**
42+
* Test helpers
43+
*/
44+
public function testHelpers(): void
45+
{
46+
$lang = LangValue::fromString('en-us');
47+
$attr = $lang->toAttribute();
48+
49+
$this->assertEquals($attr->getNamespaceURI(), C::NS_XML);
50+
$this->assertEquals($attr->getNamespacePrefix(), 'xml');
51+
$this->assertEquals($attr->getAttrName(), 'lang');
52+
$this->assertEquals($attr->getAttrValue(), 'en-us');
53+
}
54+
55+
56+
/**
57+
* @return array<string, array{0: true, 1: string}>
58+
*/
59+
public static function provideValidLang(): array
60+
{
61+
return [
62+
'empty string' => [true, ''],
63+
];
64+
}
65+
66+
67+
/**
68+
* @return array<string, array{0: false, 1: string}>
69+
*/
70+
public static function provideInvalidLang(): array
71+
{
72+
return [
73+
'too long' => [false, 'toolongLanguage'],
74+
];
75+
}
76+
}

0 commit comments

Comments
 (0)