AbstractRequestedSecurityTokenType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 11 3
A __construct() 0 4 1
A fromXML() 0 10 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 RequestedSecurityTokenType element
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractRequestedSecurityTokenType 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...uestedSecurityTokenType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
30
31
32
    /**
33
     * AbstractRequestedSecurityTokenType 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(array_pop($children));
63
    }
64
65
66
    /**
67
     * Add this RequestedSecurityTokenType to an XML element.
68
     *
69
     * @param \DOMElement|null $parent The element we should append this element 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