AbstractSignChallengeType::toXML()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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