Passed
Push — master ( 54bedc...fe2750 )
by Tim
02:40
created

AbstractRequestSecurityTokenType::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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