AbstractSecurityTokenReferenceType::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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