AbstractRequestSecurityTokenType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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