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

Commit acb1680

Browse files
committed
Add classes and implementation of a referenced xs:group
1 parent 90b3052 commit acb1680

File tree

6 files changed

+418
-0
lines changed

6 files changed

+418
-0
lines changed

src/XML/xsd/AbstractGroup.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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, NCNameValue, QNameValue};
9+
use SimpleSAML\XSD\Type\{MinOccursValue, MaxOccursValue};
10+
11+
use function strval;
12+
13+
/**
14+
* Abstract class representing the group-type.
15+
*
16+
* @package simplesamlphp/xml-xsd
17+
*/
18+
abstract class AbstractGroup extends AbstractAnnotated
19+
{
20+
use DefRefTrait;
21+
use OccursTrait;
22+
23+
/**
24+
* Group constructor
25+
*
26+
* @param \SimpleSAML\XML\Type\NCNameValue|null $name
27+
* @param \SimpleSAML\XML\Type\QNameValue|null $reference
28+
* @param \SimpleSAML\XSD\Type\MinOccursValue|null $minOccurs
29+
* @param \SimpleSAML\XSD\Type\MaxOccursValue|null $maxOccurs
30+
* @param array<\SimpleSAML\XSD\XML\xsd\ParticleInterface> $particles
31+
* @param \SimpleSAML\XSD\XML\xsd\Annotation|null $annotation
32+
* @param \SimpleSAML\XML\Type\IDValue|null $id
33+
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
34+
*/
35+
public function __construct(
36+
?NCNameValue $name = null,
37+
?QNameValue $reference = null,
38+
?MinOccursValue $minOccurs = null,
39+
?MaxOccursValue $maxOccurs = null,
40+
protected array $particles = [],
41+
?Annotation $annotation = null,
42+
?IDValue $id = null,
43+
array $namespacedAttributes = [],
44+
) {
45+
parent::__construct($annotation, $id, $namespacedAttributes);
46+
47+
$this->setName($name);
48+
$this->setReference($reference);
49+
$this->setMinOccurs($minOccurs);
50+
$this->setMaxOccurs($maxOccurs);
51+
}
52+
53+
54+
/**
55+
* Collect the value of the particles-property
56+
*
57+
* @return array<\SimpleSAML\XSD\XML\xsd\ParticleInterface>
58+
*/
59+
public function getParticles(): array
60+
{
61+
return $this->particles;
62+
}
63+
64+
65+
/**
66+
* Test if an object, at the state it's in, would produce an empty XML-element
67+
*
68+
* @return bool
69+
*/
70+
public function isEmptyElement(): bool
71+
{
72+
return parent::isEmptyElement() &&
73+
empty($this->getParticles()) &&
74+
empty($this->getName()) &&
75+
empty($this->getReference()) &&
76+
empty($this->getMinOccurs()) &&
77+
empty($this->getMaxOccurs());
78+
}
79+
80+
81+
/**
82+
* Add this Group to an XML element.
83+
*
84+
* @param \DOMElement|null $parent The element we should append this Group to.
85+
* @return \DOMElement
86+
*/
87+
public function toXML(?DOMElement $parent = null): DOMElement
88+
{
89+
$e = parent::toXML($parent);
90+
91+
if ($this->getName() !== null) {
92+
$e->setAttribute('name', strval($this->getName()));
93+
}
94+
95+
if ($this->getReference() !== null) {
96+
$e->setAttribute('ref', strval($this->getReference()));
97+
}
98+
99+
if ($this->getMinOccurs() !== null) {
100+
$e->setAttribute('minOccurs', strval($this->getMinOccurs()));
101+
}
102+
103+
if ($this->getMaxOccurs() !== null) {
104+
$e->setAttribute('maxOccurs', strval($this->getMaxOccurs()));
105+
}
106+
107+
foreach ($this->getParticles() as $particle) {
108+
$particle->toXML($e);
109+
}
110+
111+
return $e;
112+
}
113+
}

src/XML/xsd/AbstractRealGroup.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XSD\XML\xsd;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
use SimpleSAML\XML\Type\{IDValue, NCNameValue, QNameValue};
10+
//use SimpleSAML\XSD\XML\xsd\NamespaceEnum;
11+
use SimpleSAML\XML\XsNamespace;
12+
use SimpleSAML\XSD\Type\{MinOccursValue, MaxOccursValue};
13+
14+
use function is_null;
15+
16+
/**
17+
* Abstract class representing the realGroup-type.
18+
*
19+
* @package simplesamlphp/xml-xsd
20+
*/
21+
abstract class AbstractRealGroup extends AbstractGroup
22+
{
23+
/** The namespace-attribute for the xs:anyAttribute element */
24+
public const XS_ANY_ATTR_NAMESPACE = XsNamespace::OTHER;
25+
// public const XS_ANY_ATTR_NAMESPACE = NamespaceEnum::Other;
26+
27+
28+
/**
29+
* Group constructor
30+
*
31+
* @param \SimpleSAML\XSD\XML\xsd\ParticleInterface|null $particle
32+
* @param \SimpleSAML\XML\Type\NCNameValue|null $name
33+
* @param \SimpleSAML\XML\Type\QNameValue|null $reference
34+
* @param \SimpleSAML\XSD\Type\MinOccursValue|null $minOccurs
35+
* @param \SimpleSAML\XSD\Type\MaxOccursValue|null $maxOccurs
36+
* @param \SimpleSAML\XSD\XML\xsd\Annotation|null $annotation
37+
* @param \SimpleSAML\XML\Type\IDValue|null $id
38+
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39+
*/
40+
public function __construct(
41+
?ParticleInterface $particle = null,
42+
?NCNameValue $name = null,
43+
?QNameValue $reference = null,
44+
?MinOccursValue $minOccurs = null,
45+
?MaxOccursValue $maxOccurs = null,
46+
?Annotation $annotation = null,
47+
?IDValue $id = null,
48+
array $namespacedAttributes = [],
49+
) {
50+
Assert::nullOrIsInstanceOfAny(
51+
$particle,
52+
[All::class, Choice::class, Sequence::class],
53+
SchemaViolationException::class,
54+
);
55+
56+
parent::__construct(
57+
$name,
58+
$reference,
59+
$minOccurs,
60+
$maxOccurs,
61+
is_null($particle) ? [] : [$particle],
62+
$annotation,
63+
$id,
64+
$namespacedAttributes,
65+
);
66+
}
67+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XSD\XML\xsd;
6+
7+
use SimpleSAML\XML\Type\{IDValue, QNameValue};
8+
//use SimpleSAML\XSD\XML\xsd\NamespaceEnum;
9+
use SimpleSAML\XML\XsNamespace;
10+
11+
/**
12+
* Abstract class representing the groupRef-type.
13+
*
14+
* @package simplesamlphp/xml-xsd
15+
*/
16+
abstract class AbstractReferencedGroup extends AbstractRealGroup
17+
{
18+
/** The namespace-attribute for the xs:anyAttribute element */
19+
public const XS_ANY_ATTR_NAMESPACE = XsNamespace::OTHER;
20+
// public const XS_ANY_ATTR_NAMESPACE = NamespaceEnum::Other;
21+
22+
23+
/**
24+
* Group constructor
25+
*
26+
* @param \SimpleSAML\XML\Type\QNameValue $reference
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+
QNameValue $reference,
33+
?Annotation $annotation = null,
34+
?IDValue $id = null,
35+
array $namespacedAttributes = [],
36+
) {
37+
parent::__construct(
38+
reference: $reference,
39+
annotation: $annotation,
40+
id: $id,
41+
namespacedAttributes: $namespacedAttributes,
42+
);
43+
}
44+
}

src/XML/xsd/ReferencedGroup.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\XML\xsd;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
10+
use SimpleSAML\XML\Type\{IDValue, QNameValue};
11+
12+
use function array_pop;
13+
14+
/**
15+
* Class representing the group-element.
16+
*
17+
* @package simplesamlphp/xml-xsd
18+
*/
19+
final class ReferencedGroup extends AbstractReferencedGroup implements NestedParticleInterface, ParticleInterface
20+
{
21+
/** @var string */
22+
public const LOCALNAME = 'group';
23+
24+
25+
/**
26+
* Create an instance of this object from its XML representation.
27+
*
28+
* @param \DOMElement $xml
29+
* @return static
30+
*
31+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
32+
* if the qualified name of the supplied element is wrong
33+
*/
34+
public static function fromXML(DOMElement $xml): static
35+
{
36+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
37+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
38+
39+
// Prohibited attributes
40+
$name = self::getOptionalAttribute($xml, 'name', QNameValue::class, null);
41+
Assert::null($name, SchemaViolationException::class);
42+
43+
$annotation = Annotation::getChildrenOfClass($xml);
44+
Assert::maxCount($annotation, 1, TooManyElementsException::class);
45+
46+
return new static(
47+
reference: self::getAttribute($xml, 'ref', QNameValue::class),
48+
annotation: array_pop($annotation),
49+
id: self::getOptionalAttribute($xml, 'id', IDValue::class, null),
50+
namespacedAttributes: self::getAttributesNSFromXML($xml),
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)