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

fromXML()   A

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
rs 10
c 1
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 RequestSecurityTokenResponseType element
17
 *
18
 * @package simplesamlphp/ws-security
19
 */
20
abstract class AbstractRequestSecurityTokenResponseType extends AbstractWstElement
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...curityTokenResponseType: $namespaceURI, $localName, $childNodes
Loading history...
24
25
    /** The namespace-attribute for the xs:any element */
26
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * AbstractRequestSecurityTokenResponseType constructor
34
     *
35
     * @param string|null $context
36
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    final public function __construct(
40
        protected ?string $context = null,
41
        array $children = [],
42
        array $namespacedAttributes = [],
43
    ) {
44
        Assert::nullOrValidURI($context, SchemaViolationException::class);
45
46
        $this->setElements($children);
47
        $this->setAttributesNS($namespacedAttributes);
48
    }
49
50
51
    /**
52
     * @return string|null
53
     */
54
    public function getContext(): ?string
55
    {
56
        return $this->context;
57
    }
58
59
60
    /**
61
     * Test if an object, at the state it's in, would produce an empty XML-element
62
     *
63
     * @return bool
64
     */
65
    public function isEmptyElement(): bool
66
    {
67
        return empty($this->getContext())
68
            && empty($this->getElements())
69
            && empty($this->getAttributesNS());
70
    }
71
72
73
    /**
74
     * Create an instance of this object from its XML representation.
75
     *
76
     * @param \DOMElement $xml
77
     * @return static
78
     *
79
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
80
     *   if the qualified name of the supplied element is wrong
81
     */
82
    public static function fromXML(DOMElement $xml): static
83
    {
84
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
85
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
86
87
        return new static(
88
            self::getOptionalAttribute($xml, 'Context'),
89
            self::getChildElementsFromXML($xml),
90
            self::getAttributesNSFromXML($xml),
91
        );
92
    }
93
94
95
    /**
96
     * Add this RequestSecurityTokenResponseType to an XML element.
97
     *
98
     * @param \DOMElement $parent The element we should append this username token to.
99
     * @return \DOMElement
100
     */
101
    public function toXML(DOMElement $parent = null): DOMElement
102
    {
103
        $e = parent::instantiateParentElement($parent);
104
105
        if ($this->getContext() !== null) {
106
            $e->setAttribute('Context', $this->getContext());
107
        }
108
109
        foreach ($this->getAttributesNS() as $attr) {
110
            $attr->toXML($e);
111
        }
112
113
        foreach ($this->getElements() as $child) {
114
            if (!$child->isEmptyElement()) {
115
                $child->toXML($e);
116
            }
117
        }
118
119
        return $e;
120
    }
121
}
122