AbstractSignChallengeType::isEmptyElement()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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