AbstractProofTokenType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 12
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\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\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\SerializableElementInterface;
14
use SimpleSAML\XML\XsNamespace as NS;
15
16
/**
17
 * Class defining the ProofTokenType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractProofTokenType extends AbstractFedElement
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...\AbstractProofTokenType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
    /** The namespace-attribute for the xs:anyAttribute element */
27
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
28
29
    /** The namespace-attribute for the xs:any element */
30
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
31
32
33
    /**
34
     * AbstractProofTokenType constructor
35
     *
36
     * @param \SimpleSAML\XML\SerializableElementInterface $child
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    final public function __construct(
40
        protected SerializableElementinterface $child,
41
        array $namespacedAttributes = [],
42
    ) {
43
        $this->setElements([$child]);
44
        $this->setAttributesNS($namespacedAttributes);
45
    }
46
47
48
    /**
49
     * Create an instance of this object from its XML representation.
50
     *
51
     * @param \DOMElement $xml
52
     * @return static
53
     *
54
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
55
     *   if the qualified name of the supplied element is wrong
56
     */
57
    public static function fromXML(DOMElement $xml): static
58
    {
59
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
60
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
61
62
        $children = self::getChildElementsFromXML($xml);
63
        Assert::minCount($children, 1, SchemaViolationException::class);
64
        Assert::maxCount($children, 1, SchemaViolationException::class);
65
66
        return new static(
67
            array_pop($children),
68
            self::getAttributesNSFromXML($xml),
69
        );
70
    }
71
72
73
    /**
74
     * Add this AbstractProofTokenType to an XML element.
75
     *
76
     * @param \DOMElement|null $parent The element we should append this username token to.
77
     * @return \DOMElement
78
     */
79
    public function toXML(?DOMElement $parent = null): DOMElement
80
    {
81
        $e = parent::instantiateParentElement($parent);
82
83
        foreach ($this->getAttributesNS() as $attr) {
84
            $attr->toXML($e);
85
        }
86
87
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
88
        foreach ($this->getElements() as $child) {
89
            if (!$child->isEmptyElement()) {
90
                $child->toXML($e);
91
            }
92
        }
93
94
        return $e;
95
    }
96
}
97