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

Commit 8832c21

Browse files
committed
Add interface for elements that are part of the xs:typeDefParticle group
1 parent d9e4896 commit 8832c21

13 files changed

+195
-6
lines changed

src/XML/xsd/AbstractAll.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function __construct(
4343
) {
4444
Assert::oneOf($minOccurs->toInteger(), [0, 1], SchemaViolationException::class);
4545
Assert::same($maxOccurs->toInteger(), 1, SchemaViolationException::class);
46+
Assert::allIsInstance(
47+
$particles,
48+
NarrowMinMaxElement::class,
49+
SchemaViolationException::class,
50+
);
4651

4752
parent::__construct(
4853
nestedParticles: $particles,
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\XSD\XML\xsd;
6+
7+
use DOMElement;
8+
use SimpleSAML\XML\Type\{IDValue, QNameValue};
9+
10+
use function strval;
11+
12+
/**
13+
* Abstract class representing the restrictionType-type.
14+
*
15+
* @package simplesamlphp/xml-xsd
16+
*/
17+
abstract class AbstractRestrictionType extends AbstractAnnotated
18+
{
19+
use AttrDeclsTrait;
20+
21+
/**
22+
* AbstractRestrictionType constructor
23+
*
24+
* @param \SimpleSAML\XML\Type\QNameValue $base
25+
* @param \SimpleSAML\XML\XML\AnyAttribute|null $anyAttribute
26+
* array<\SimpleSAML\XSD\XML\xsd\Attribute|\SimpleSAML\XSD\XML\xsd\ReferencedAttributeGroup> $attributes
27+
* @param \SimpleSAML\XSD\XML\xsd\Annotation|null $annotation
28+
* @param \SimpleSAML\XML\Type\IDValue|null $id
29+
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
30+
*/
31+
public function __construct(
32+
protected QNameValue $base,
33+
protected ?TypeDefParticleInterface $particle = null,
34+
array $attributes = [],
35+
?AnyAttribute $anyAttribute = null,
36+
?Annotation $annotation = null,
37+
?IDValue $id = null,
38+
array $namespacedAttributes = [],
39+
) {
40+
parent::__construct($annotation, $id, $namespacedAttributes);
41+
}
42+
43+
44+
/**
45+
* Collect the value of the base-property
46+
*
47+
* @return \SimpleSAML\XML\Type\QNameValue
48+
*/
49+
public function getBase(): ?QNameValue
50+
{
51+
return $this->base;
52+
}
53+
54+
55+
/**
56+
* Add this Annotated to an XML element.
57+
*
58+
* @param \DOMElement|null $parent The element we should append this Annotated to.
59+
* @return \DOMElement
60+
*/
61+
public function toXML(?DOMElement $parent = null): DOMElement
62+
{
63+
$e = parent::toXML($parent);
64+
65+
if ($this->getBase() !== null) {
66+
$e->setAttribute('base', strval($this->getBase()));
67+
}
68+
69+
return $e;
70+
}
71+
}

src/XML/xsd/AbstractSimpleContent.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\XSD\XML\xsd;
6+
7+
use DOMElement;
8+
use SimpleSAML\XML\Type\IDValue;
9+
10+
/**
11+
* Abstract class representing the simpleContent-type.
12+
*
13+
* @package simplesamlphp/xml-xsd
14+
*/
15+
abstract class AbstractSimpleContent extends AbstractAnnotated
16+
{
17+
/**
18+
* SimpleContent constructor
19+
*
20+
* @param \SimpleSAML\XSD\XML\xsd\Extension|\SimpleSAML\XSD\XML\xsd\Restriction $content
21+
* @param \SimpleSAML\XSD\XML\xsd\Annotation|null $annotation
22+
* @param \SimpleSAML\XML\Type\IDValue|null $id
23+
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
24+
*/
25+
public function __construct(
26+
protected Extension|Restriction $content,
27+
?Annotation $annotation = null,
28+
?IDValue $id = null,
29+
array $namespacedAttributes = [],
30+
) {
31+
parent::__construct($annotation, $id, $namespacedAttributes);
32+
}
33+
34+
35+
/**
36+
* Collect the value of the content-property
37+
*
38+
* @return \SimpleSAML\XSD\XML\xsd\Extension|\SimpleSAML\XSD\XML\xsd\Restriction
39+
*/
40+
public function getContent(): Extension|Restriction
41+
{
42+
return $this->content;
43+
}
44+
45+
46+
/**
47+
* Test if an object, at the state it's in, would produce an empty XML-element
48+
*
49+
* @return bool
50+
*/
51+
public function isEmptyElement(): bool
52+
{
53+
return false;
54+
}
55+
56+
57+
/**
58+
* Add this SimpleContent to an XML element.
59+
*
60+
* @param \DOMElement|null $parent The element we should append this SimpleContent to.
61+
* @return \DOMElement
62+
*/
63+
public function toXML(?DOMElement $parent = null): DOMElement
64+
{
65+
$e = parent::toXML($parent);
66+
67+
$this->getContent()->toXML($e);
68+
69+
return $e;
70+
}
71+
}

src/XML/xsd/All.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
*
2525
* @package simplesamlphp/xml-xsd
2626
*/
27-
final class All extends AbstractAll implements ParticleInterface, SchemaValidatableElementInterface
27+
final class All extends AbstractAll implements
28+
ParticleInterface,
29+
SchemaValidatableElementInterface,
30+
TypeDefParticleInterface
2831
{
2932
use SchemaValidatableElementTrait;
3033

src/XML/xsd/Choice.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
final class Choice extends AbstractExplicitGroup implements
2222
NestedParticleInterface,
2323
ParticleInterface,
24-
SchemaValidatableElementInterface
24+
SchemaValidatableElementInterface,
25+
TypeDefParticleInterface
2526
{
2627
use SchemaValidatableElementTrait;
2728

src/XML/xsd/ReferencedGroup.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*
1717
* @package simplesamlphp/xml-xsd
1818
*/
19-
final class ReferencedGroup extends AbstractReferencedGroup implements NestedParticleInterface, ParticleInterface
19+
final class ReferencedGroup extends AbstractReferencedGroup implements
20+
NestedParticleInterface,
21+
ParticleInterface,
22+
TypeDefParticleInterface
2023
{
2124
/** @var string */
2225
public const LOCALNAME = 'group';

src/XML/xsd/Sequence.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
final class Sequence extends AbstractExplicitGroup implements
2222
NestedParticleInterface,
2323
ParticleInterface,
24-
SchemaValidatableElementInterface
24+
SchemaValidatableElementInterface,
25+
TypeDefParticleInterface
2526
{
2627
use SchemaValidatableElementTrait;
2728

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XSD\XML\xsd;
6+
7+
/**
8+
* interface class to be implemented by all the classes that represent an element from the xs:typeDefParticle group
9+
*
10+
* @package simplesamlphp/xml-xsd
11+
*/
12+
interface TypeDefParticleInterface extends ParticleInterface
13+
{
14+
}

tests/XML/xsd/ChoiceTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class ChoiceTest extends TestCase
4848
*/
4949
public static function setUpBeforeClass(): void
5050
{
51-
self::$testedClass = ChoiceGroup::class;
51+
self::$testedClass = Choice::class;
5252

5353
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
5454
dirname(__FILE__, 3) . '/resources/xml/choice.xml',
@@ -135,4 +135,18 @@ public function testMarshalling(): void
135135
strval($choice),
136136
);
137137
}
138+
139+
140+
/**
141+
* Creating an empty Choice element should yield an empty element.
142+
*/
143+
public function testMarshallingEmptyElement(): void
144+
{
145+
$choice = new Choice();
146+
$this->assertEquals(
147+
'<xsd:choice xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>',
148+
strval($choice),
149+
);
150+
$this->assertTrue($choice->isEmptyElement());
151+
}
138152
}

tests/XML/xsd/KeyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ public function testMarshalling(): void
156156
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
157157
strval($key),
158158
);
159+
160+
$this->assertFalse($key->isEmptyElement());
159161
}
160162
}

tests/XML/xsd/KeyrefTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,7 @@ public function testMarshalling(): void
157157
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
158158
strval($keyref),
159159
);
160+
161+
$this->assertFalse($keyref->isEmptyElement());
160162
}
161163
}

tests/XML/xsd/SequenceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class SequenceTest extends TestCase
4848
*/
4949
public static function setUpBeforeClass(): void
5050
{
51-
self::$testedClass = SequenceGroup::class;
51+
self::$testedClass = Sequence::class;
5252

5353
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
5454
dirname(__FILE__, 3) . '/resources/xml/sequence.xml',

tests/XML/xsd/UniqueTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ public function testMarshalling(): void
156156
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
157157
strval($unique),
158158
);
159+
160+
$this->assertFalse($unique->isEmptyElement());
159161
}
160162
}

0 commit comments

Comments
 (0)