AbstractTokenAssertionType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200507;
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 sprintf;
15
16
/**
17
 * Class representing WS security policy TokenAssertionType.
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractTokenAssertionType extends AbstractSpElement
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...tractTokenAssertionType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28
29
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
31
32
33
    /**
34
     * TokenAssertionType constructor.
35
     *
36
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    final public function __construct(
40
        array $elts = [],
41
        array $namespacedAttributes = [],
42
    ) {
43
        $this->setElements($elts);
44
        $this->setAttributesNS($namespacedAttributes);
45
    }
46
47
48
    /**
49
     * Test if an object, at the state it's in, would produce an empty XML-element
50
     *
51
     * @return bool
52
     */
53
    public function isEmptyElement(): bool
54
    {
55
        return empty($this->getAttributesNS())
56
            && empty($this->getElements());
57
    }
58
59
60
    /**
61
     * Initialize an TokenAssertionType.
62
     *
63
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
64
     *
65
     * @param \DOMElement $xml The XML element we should load.
66
     * @return static
67
     *
68
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
69
     *   if the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): static
72
    {
73
        $qualifiedName = static::getClassName(static::class);
74
        Assert::eq(
0 ignored issues
show
Bug introduced by
The method eq() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        Assert::/** @scrutinizer ignore-call */ 
75
                eq(
Loading history...
75
            $xml->localName,
76
            $qualifiedName,
77
            sprintf('Unexpected name for TokenAssertionType: %s. Expected: %s.', $xml->localName, $qualifiedName),
78
            InvalidDOMElementException::class,
79
        );
80
81
        return new static(
82
            self::getChildElementsFromXML($xml),
83
            self::getAttributesNSFromXML($xml),
84
        );
85
    }
86
87
88
    /**
89
     * Convert this element to XML.
90
     *
91
     * @param \DOMElement|null $parent The element we should append this element to.
92
     * @return \DOMElement
93
     */
94
    public function toXML(?DOMElement $parent = null): DOMElement
95
    {
96
        $e = $this->instantiateParentElement($parent);
97
98
        foreach ($this->getElements() as $elt) {
99
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
100
            $elt->toXML($e);
101
        }
102
103
        foreach ($this->getAttributesNS() as $attr) {
104
            $attr->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110