Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit ac87aa1

Browse files
committed
Add UseValue type and UseEnum
1 parent f37e203 commit ac87aa1

File tree

4 files changed

+139
-12
lines changed

4 files changed

+139
-12
lines changed

src/Type/UseValue.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\XSD\Type;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
use SimpleSAML\XML\Type\NMTokenValue;
10+
use SimpleSAML\XSD\XML\xsd\UseEnum;
11+
12+
use function array_column;
13+
14+
/**
15+
* @package simplesaml/xml-xsd
16+
*/
17+
class UseValue extends NMTokenValue
18+
{
19+
/**
20+
* Validate the value.
21+
*
22+
* @param string $value The value
23+
* @throws \Exception on failure
24+
* @return void
25+
*/
26+
protected function validateValue(string $value): void
27+
{
28+
Assert::oneOf(
29+
$this->sanitizeValue($value),
30+
array_column(UseEnum::cases(), 'value'),
31+
SchemaViolationException::class,
32+
);
33+
}
34+
35+
36+
/**
37+
* @param \SimpleSAML\XSD\XML\xsd\UseEnum $value
38+
* @return static
39+
*/
40+
public static function fromEnum(UseEnum $value): static
41+
{
42+
return new static($value->value);
43+
}
44+
45+
46+
/**
47+
* @return \SimpleSAML\XSD\XML\xsd\UseEnum $value
48+
*/
49+
public function toEnum(): UseEnum
50+
{
51+
return UseEnum::from($this->getValue());
52+
}
53+
}

src/XML/xsd/AbstractAttribute.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use SimpleSAML\XML\Assert\Assert;
99
use SimpleSAML\XML\Exception\SchemaViolationException;
1010
use SimpleSAML\XML\Type\{BooleanValue, IDValue, NCNameValue, QNameValue};
11-
use SimpleSAML\XSD\Type\FormChoiceValue;
11+
use SimpleSAML\XSD\Type\{FormChoiceValue, UseValue};
1212

1313
use function strval;
1414

@@ -28,8 +28,8 @@ abstract class AbstractAttribute extends AbstractAnnotated
2828
*
2929
* @param \SimpleSAML\XML\Type\NCNameValue $name
3030
* @param \SimpleSAML\XML\Type\QNameValue $reference
31-
* @param string $type
32-
* @param string|null $use
31+
* @param \SimpleSAML\XML\Type\QNameValue $type
32+
* @param \SimpleSAML\XSD\Type\UseValue|null $use
3333
* @param string|null $default
3434
* @param \SimpleSAML\XML\Type\BooleanValue|null $fixed
3535
* @param \SimpleSAML\XSD\Type\FormChoiceValue|null $formChoice
@@ -41,8 +41,8 @@ abstract class AbstractAttribute extends AbstractAnnotated
4141
public function __construct(
4242
NCNameValue $name,
4343
QNameValue $reference,
44-
protected string $type,
45-
protected ?string $use = null,
44+
protected QNameValue $type,
45+
protected ?UseValue $use = null,
4646
protected ?string $default = null,
4747
protected ?BooleanValue $fixed = null,
4848
protected ?FormChoiceValue $formChoice = null,
@@ -52,7 +52,6 @@ public function __construct(
5252
array $namespacedAttributes = [],
5353
) {
5454
Assert::validQName($type, SchemaViolationException::class);
55-
Assert::nullOrOneOf($use, ['optional', 'prohibited', 'required'], SchemaViolationException::class);
5655

5756
parent::__construct($annotation, $id, $namespacedAttributes);
5857

@@ -76,9 +75,9 @@ public function getSimpleType(): array
7675
/**
7776
* Collect the value of the type-property
7877
*
79-
* @return string
78+
* @return \SimpleSAML\XML\Type\QNameValue
8079
*/
81-
public function getType(): string
80+
public function getType(): QNameValue
8281
{
8382
return $this->type;
8483
}
@@ -87,9 +86,9 @@ public function getType(): string
8786
/**
8887
* Collect the value of the use-property
8988
*
90-
* @return string|null
89+
* @return \SimpleSAML\XSD\Type\UseValue|null
9190
*/
92-
public function getUse(): ?string
91+
public function getUse(): ?UseValue
9392
{
9493
return $this->use;
9594
}
@@ -129,10 +128,10 @@ public function toXML(?DOMElement $parent = null): DOMElement
129128

130129
$e->setAttribute('name', strval($this->getName()));
131130
$e->setAttribute('reference', strval($this->getReference()));
132-
$e->setAttribute('type', $this->getType());
131+
$e->setAttribute('type', strval($this->getType()));
133132

134133
if ($this->getUse() !== null) {
135-
$e->setAttribute('use', $this->getUse());
134+
$e->setAttribute('use', strval($this->getUse()));
136135
}
137136

138137
if ($this->getDefault() !== null) {

src/XML/xsd/UseEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XSD\XML\xsd;
6+
7+
enum UseEnum: string
8+
{
9+
case Optional = 'optional';
10+
case Prohibited = 'prohibited';
11+
case Required = 'required';
12+
}

tests/Type/UseValueTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XSD\Type;
6+
7+
use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, DependsOnClass};
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
use SimpleSAML\XSD\Type\UseValue;
11+
use SimpleSAML\XSD\XML\xsd\UseEnum;
12+
13+
/**
14+
* Class \SimpleSAML\Test\XSD\Type\UseValueTest
15+
*
16+
* @package simplesamlphp/xml-xsd
17+
*/
18+
#[CoversClass(UseValue::class)]
19+
final class UseValueTest extends TestCase
20+
{
21+
/**
22+
* @param string $use
23+
* @param bool $expected
24+
*/
25+
#[DataProvider('provideUse')]
26+
public function testUseValue(string $use, bool $shouldPass): void
27+
{
28+
try {
29+
UseValue::fromString($use);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* Test helpers
39+
*/
40+
public function testHelpers(): void
41+
{
42+
$x = UseValue::fromEnum(UseEnum::Optional);
43+
$this->assertEquals(UseEnum::Optional, $x->toEnum());
44+
45+
$y = UseValue::fromString('optional');
46+
$this->assertEquals(UseEnum::Optional, $y->toEnum());
47+
}
48+
49+
50+
/**
51+
* @return array<string, array{0: string, 1: string}>
52+
*/
53+
public static function provideUse(): array
54+
{
55+
return [
56+
'optional' => ['optional', true],
57+
'prohibited' => ['prohibited', true],
58+
'required' => ['required', true],
59+
'undefined' => ['undefined', false],
60+
'empty' => ['', false],
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)