AbstractRenewTargetType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 11
c 1
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_200502;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableElementTrait;
10
use SimpleSAML\XML\SerializableElementInterface;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
use function array_pop;
17
18
/**
19
 * Class defining the RenewTargetType element
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractRenewTargetType extends AbstractWstElement
24
{
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractRenewTargetType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * AbstractRenewTargetType constructor
34
     *
35
     * @param \SimpleSAML\XML\SerializableElementInterface $child
36
     */
37
    final public function __construct(
38
        SerializableElementInterface $child,
39
    ) {
40
        $this->setElements([$child]);
41
    }
42
43
44
    /**
45
     * Create an instance of this object from its XML representation.
46
     *
47
     * @param \DOMElement $xml
48
     * @return static
49
     *
50
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
51
     *   if the qualified name of the supplied element is wrong
52
     */
53
    public static function fromXML(DOMElement $xml): static
54
    {
55
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
56
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
57
58
        $children = self::getChildElementsFromXML($xml);
59
        Assert::minCount($children, 1, MissingElementException::class);
60
        Assert::maxCount($children, 1, TooManyElementsException::class);
61
62
        return new static(
63
            array_pop($children),
64
        );
65
    }
66
67
68
    /**
69
     * Add this RenewTargetType to an XML element.
70
     *
71
     * @param \DOMElement|null $parent The element we should append this username token to.
72
     * @return \DOMElement
73
     */
74
    public function toXML(?DOMElement $parent = null): DOMElement
75
    {
76
        $e = parent::instantiateParentElement($parent);
77
78
        foreach ($this->getElements() as $child) {
79
            if (!$child->isEmptyElement()) {
80
                $child->toXML($e);
81
            }
82
        }
83
84
        return $e;
85
    }
86
}
87