AbstractTokenTypesOfferedType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
dl 0
loc 97
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenType() 0 3 1
A __construct() 0 10 1
A fromXML() 0 17 1
A toXML() 0 19 5
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\WSSecurity\XML\fed\TokenType;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\MissingElementException;
14
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
17
/**
18
 * Class defining the TokenTypesOffered element
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractTokenTypesOfferedType extends AbstractFedElement
23
{
24
    use ExtendableAttributesTrait;
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...ctTokenTypesOfferedType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * AbstractTokenTypesOffered constructor
37
     *
38
     * @param array<\SimpleSAML\WSSecurity\XML\fed\TokenType> $tokenType
39
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
40
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
41
     */
42
    final public function __construct(
43
        protected array $tokenType,
44
        array $children = [],
45
        array $namespacedAttributes = [],
46
    ) {
47
        Assert::notEmpty($tokenType, SchemaViolationException::class);
48
        Assert::allIsInstanceOf($tokenType, TokenType::class);
49
50
        $this->setElements($children);
51
        $this->setAttributesNS($namespacedAttributes);
52
    }
53
54
55
    /**
56
     * @return array<\SimpleSAML\WSSecurity\XML\fed\TokenType>
57
     */
58
    public function getTokenType(): array
59
    {
60
        return $this->tokenType;
61
    }
62
63
64
    /**
65
     * Create an instance of this object from its XML representation.
66
     *
67
     * @param \DOMElement $xml
68
     * @return static
69
     *
70
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     */
73
    public static function fromXML(DOMElement $xml): static
74
    {
75
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
76
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
77
78
        $tokenType = TokenType::getChildrenOfClass($xml);
79
        Assert::minCount(
80
            $tokenType,
81
            1,
82
            'Missing <fed:TokenType> in TokenTypesOffered.',
83
            MissingElementException::class,
84
        );
85
86
        return new static(
87
            $tokenType,
88
            self::getChildElementsFromXML($xml),
89
            self::getAttributesNSFromXML($xml),
90
        );
91
    }
92
93
94
    /**
95
     * Add this TokenTypesOffered to an XML element.
96
     *
97
     * @param \DOMElement|null $parent The element we should append this TokenTypesOffered to.
98
     * @return \DOMElement
99
     */
100
    public function toXML(?DOMElement $parent = null): DOMElement
101
    {
102
        $e = parent::instantiateParentElement($parent);
103
104
        foreach ($this->getAttributesNS() as $attr) {
105
            $attr->toXML($e);
106
        }
107
108
        foreach ($this->getTokenType() as $tokenType) {
109
            $tokenType->toXML($e);
110
        }
111
112
        foreach ($this->getElements() as $child) {
113
            if (!$child->isEmptyElement()) {
114
                $child->toXML($e);
115
            }
116
        }
117
118
        return $e;
119
    }
120
}
121