AbstractClaimDialectType::toXML()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.6111
c 1
b 0
f 0
cc 5
nc 12
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
/**
16
 * Class defining the ClaimDialectType element
17
 *
18
 * @package simplesamlphp/ws-security
19
 */
20
abstract class AbstractClaimDialectType extends AbstractFedElement
21
{
22
    use ExtendableAttributesTrait;
23
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...bstractClaimDialectType: $namespaceURI, $localName, $childNodes
Loading history...
24
25
26
    /** The namespace-attribute for the xs:anyAttribute element */
27
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
28
29
    /** The namespace-attribute for the xs:any element */
30
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
31
32
33
    /**
34
     * AbstractClaimDialectType constructor
35
     *
36
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Uri
37
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
38
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39
     */
40
    final public function __construct(
41
        protected ?AnyURIValue $Uri = null,
42
        array $children = [],
43
        array $namespacedAttributes = [],
44
    ) {
45
        // Next one is debatable since the schema allows an empty element, but that makes zero sense
46
        Assert::allNotEmpty([$Uri, $children, $namespacedAttributes]);
47
48
        $this->setElements($children);
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
55
     */
56
    public function getUri(): ?AnyURIValue
57
    {
58
        return $this->Uri;
59
    }
60
61
62
    /**
63
     * Create an instance of this object from its XML representation.
64
     *
65
     * @param \DOMElement $xml
66
     * @return static
67
     *
68
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
69
     *   if the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): static
72
    {
73
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
74
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
75
76
        return new static(
77
            self::getOptionalAttribute($xml, 'Uri', AnyURIValue::class, null),
78
            self::getChildElementsFromXML($xml),
79
            self::getAttributesNSFromXML($xml),
80
        );
81
    }
82
83
84
    /**
85
     * Add this ClaimDialectType to an XML element.
86
     *
87
     * @param \DOMElement|null $parent The element we should append this username token to.
88
     * @return \DOMElement
89
     */
90
    public function toXML(?DOMElement $parent = null): DOMElement
91
    {
92
        $e = parent::instantiateParentElement($parent);
93
94
        if ($this->getUri() !== null) {
95
            $e->setAttribute('Uri', $this->getUri()->getValue());
96
        }
97
98
        foreach ($this->getAttributesNS() as $attr) {
99
            $attr->toXML($e);
100
        }
101
102
        foreach ($this->getElements() as $child) {
103
            if (!$child->isEmptyElement()) {
104
                $child->toXML($e);
105
            }
106
        }
107
108
        return $e;
109
    }
110
}
111