AbstractProofEncryptionType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
c 0
b 0
f 0
rs 10
cc 3
nc 3
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\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\SerializableElementInterface;
14
use SimpleSAML\XML\XsNamespace as NS;
15
16
/**
17
 * Class defining the ProofEncryptionType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractProofEncryptionType extends AbstractWstElement
22
{
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...ractProofEncryptionType: $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
29
    /**
30
     * AbstractProofEncryptionType constructor
31
     *
32
     * @param \SimpleSAML\XML\SerializableElementInterface $child
33
     */
34
    final public function __construct(
35
        SerializableElementInterface $child,
36
    ) {
37
        $this->setElements([$child]);
38
    }
39
40
41
    /**
42
     * Create an instance of this object from its XML representation.
43
     *
44
     * @param \DOMElement $xml
45
     * @return static
46
     *
47
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
48
     *   if the qualified name of the supplied element is wrong
49
     */
50
    public static function fromXML(DOMElement $xml): static
51
    {
52
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
53
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
54
55
        $children = self::getChildElementsFromXML($xml);
56
        Assert::minCount($children, 1, MissingElementException::class);
57
        Assert::maxCount($children, 1, TooManyElementsException::class);
58
59
        return new static(
60
            array_pop($children),
61
        );
62
    }
63
64
65
    /**
66
     * Add this ProofEncryptionType to an XML element.
67
     *
68
     * @param \DOMElement|null $parent The element we should append this username token to.
69
     * @return \DOMElement
70
     */
71
    public function toXML(?DOMElement $parent = null): DOMElement
72
    {
73
        $e = parent::instantiateParentElement($parent);
74
75
        foreach ($this->getElements() as $child) {
76
            if (!$child->isEmptyElement()) {
77
                $child->toXML($e);
78
            }
79
        }
80
81
        return $e;
82
    }
83
}
84