AbstractSecurityTokenReferenceType::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
rs 9.2222
cc 6
nc 24
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsse;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\Constants as C;
10
use SimpleSAML\XML\Attribute as XMLAttribute;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\XsNamespace as NS;
15
16
use function array_unshift;
17
18
/**
19
 * Class defining the SecurityTokenReferenceType element
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractSecurityTokenReferenceType extends AbstractWsseElement
24
{
25
    use ExtendableAttributesTrait;
26
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...urityTokenReferenceType: $namespaceURI, $localName, $childNodes
Loading history...
27
    use UsageTrait;
28
29
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
    /** The exclusions for the xs:anyAttribute element */
33
    public const XS_ANY_ATTR_EXCLUSIONS = [
34
        ['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id'],
35
    ];
36
37
    /** The namespace-attribute for the xs:any element */
38
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
39
40
41
    /**
42
     * AbstractSecurityReferenceType constructor
43
     *
44
     * @param string|null $Id
45
     * @param string|null $Usage
46
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     */
49
    final public function __construct(
50
        protected ?string $Id = null,
51
        ?string $Usage = null,
52
        array $children = [],
53
        array $namespacedAttributes = [],
54
    ) {
55
        Assert::nullOrValidNCName($Id);
56
57
        $this->setUsage($Usage);
58
        $this->setElements($children);
59
        $this->setAttributesNS($namespacedAttributes);
60
    }
61
62
63
    /**
64
     * @return string|null
65
     */
66
    public function getId(): ?string
67
    {
68
        return $this->Id;
69
    }
70
71
72
    /**
73
     * Test if an object, at the state it's in, would produce an empty XML-element
74
     *
75
     * @return bool
76
     */
77
    public function isEmptyElement(): bool
78
    {
79
        return empty($this->getId()) &&
80
            empty($this->getUsage()) &&
81
            empty($this->getElements()) &&
82
            empty($this->getAttributesNS());
83
    }
84
85
86
    /**
87
     * Create an instance of this object from its XML representation.
88
     *
89
     * @param \DOMElement $xml
90
     * @return static
91
     *
92
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
93
     *   if the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): static
96
    {
97
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
99
100
        return new static(
101
            $xml->hasAttributeNS(C::NS_SEC_UTIL, 'Id') ? $xml->getAttributeNS(C::NS_SEC_UTIL, 'Id') : null,
102
            $xml->hasAttributeNS(C::NS_SEC_EXT, 'Usage') ? $xml->getAttributeNS(C::NS_SEC_EXT, 'Usage') : null,
103
            self::getChildElementsFromXML($xml),
104
            self::getAttributesNSFromXML($xml),
105
        );
106
    }
107
108
109
    /**
110
     * Add this SecurityTokenReferenceType token to an XML element.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this username token to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(?DOMElement $parent = null): DOMElement
116
    {
117
        $e = parent::instantiateParentElement($parent);
118
119
        $attributes = $this->getAttributesNS();
120
        if ($this->getId() !== null) {
121
            $idAttr = new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', $this->getId());
122
            array_unshift($attributes, $idAttr);
123
        }
124
125
        if ($this->getUsage() !== null) {
126
            $UsageAttr = new XMLAttribute(C::NS_SEC_EXT, 'wsse', 'Usage', $this->getUsage());
127
            array_unshift($attributes, $UsageAttr);
128
        }
129
130
        foreach ($attributes as $attr) {
131
            $attr->toXML($e);
132
        }
133
134
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
135
        foreach ($this->getElements() as $child) {
136
            if (!$child->isEmptyElement()) {
137
                $child->toXML($e);
138
            }
139
        }
140
141
        return $e;
142
    }
143
}
144