AbstractDelegateToType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 11 1
A __construct() 0 4 1
A toXML() 0 11 3
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\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
/**
17
 * Class defining the DelegateToType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractDelegateToType 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...\AbstractDelegateToType: $namespaceURI, $localName, $childNodes
Loading history...
24
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
28
29
30
    /**
31
     * AbstractDelegateToType constructor
32
     *
33
     * @param \SimpleSAML\XML\SerializableElementInterface $child
34
     */
35
    final public function __construct(
36
        SerializableElementInterface $child,
37
    ) {
38
        $this->setElements([$child]);
39
    }
40
41
42
    /**
43
     * Create an instance of this object from its XML representation.
44
     *
45
     * @param \DOMElement $xml
46
     * @return static
47
     *
48
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
49
     *   if the qualified name of the supplied element is wrong
50
     */
51
    public static function fromXML(DOMElement $xml): static
52
    {
53
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
54
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
55
56
        $children = self::getChildElementsFromXML($xml);
57
        Assert::minCount($children, 1, MissingElementException::class);
58
        Assert::maxCount($children, 1, TooManyElementsException::class);
59
60
        return new static(
61
            array_pop($children),
62
        );
63
    }
64
65
66
    /**
67
     * Add this DelegateToType to an XML element.
68
     *
69
     * @param \DOMElement|null $parent The element we should append this username token to.
70
     * @return \DOMElement
71
     */
72
    public function toXML(?DOMElement $parent = null): DOMElement
73
    {
74
        $e = parent::instantiateParentElement($parent);
75
76
        foreach ($this->getElements() as $child) {
77
            if (!$child->isEmptyElement()) {
78
                $child->toXML($e);
79
            }
80
        }
81
82
        return $e;
83
    }
84
}
85