Skip to content

Commit 8501b48

Browse files
committed
Add type for xml:space attribute
1 parent 95cf8fe commit 8501b48

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

src/XML/Type/SpaceValue.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Attribute;
9+
use SimpleSAML\XML\Constants as C;
10+
use SimpleSAML\XML\XML\xml\SpaceEnum;
11+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12+
use SimpleSAML\XMLSchema\Type\Builtin\NCNameValue;
13+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
14+
15+
/**
16+
* @package simplesaml/xml-common
17+
*/
18+
class SpaceValue extends NCNameValue implements AttributeTypeInterface
19+
{
20+
/**
21+
* Validate the value.
22+
*
23+
* @param string $value
24+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
25+
* @return void
26+
*/
27+
protected function validateValue(string $value): void
28+
{
29+
$sanitized = $this->sanitizeValue($value);
30+
parent::validateValue($sanitized);
31+
32+
Assert::oneOf(
33+
$sanitized,
34+
[
35+
SpaceEnum::Default->value,
36+
SpaceEnum::Preserve->value,
37+
],
38+
SchemaViolationException::class,
39+
);
40+
}
41+
42+
43+
/**
44+
* @param \SimpleSAML\XML\XML\xml\SpaceEnum $value
45+
* @return static
46+
*/
47+
public static function fromEnum(SpaceEnum $value): static
48+
{
49+
return new static($value->value);
50+
}
51+
52+
53+
/**
54+
* @return \SimpleSAML\XML\XML\xml\SpaceEnum $value
55+
*/
56+
public function toEnum(): SpaceEnum
57+
{
58+
return SpaceEnum::from($this->getValue());
59+
}
60+
61+
62+
/**
63+
* Convert this value to an attribute
64+
*
65+
* @return \SimpleSAML\XML\Attribute
66+
*/
67+
public function toAttribute(): Attribute
68+
{
69+
return new Attribute(C::NS_XML, 'xml', 'space', $this);
70+
}
71+
}

src/XML/XML/xml/SpaceEnum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\XML\xml;
6+
7+
enum SpaceEnum: string
8+
{
9+
case Default = 'default';
10+
case Preserve = 'preserve';
11+
}

tests/XML/Type/SpaceValueTest.php

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

0 commit comments

Comments
 (0)