AbstractSecureConversationTokenType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 9.8666
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200507;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\sp_200507\Type\IncludeTokenValue;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
use function array_pop;
16
use function sprintf;
17
18
/**
19
 * Class representing WS security policy SecureConversationTokenType.
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractSecureConversationTokenType extends AbstractSpElement
24
{
25
    use ExtendableAttributesTrait;
26
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...reConversationTokenType: $namespaceURI, $localName, $childNodes
Loading history...
27
    use IncludeTokenTypeTrait;
28
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
35
36
    /** The exclusions for the xs:anyAttribute element */
37
    public const XS_ANY_ATTR_EXCLUSIONS = [
38
        ['http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702', 'IncludeToken'],
39
    ];
40
41
42
    /**
43
     * SecureConversationTokenType constructor.
44
     *
45
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null $issuer
46
     * @param \SimpleSAML\WSSecurity\XML\sp_200507\Type\IncludeTokenValue|null $includeToken
47
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     */
50
    final public function __construct(
51
        protected ?Issuer $issuer,
52
        ?IncludeTokenValue $includeToken = null,
53
        array $elts = [],
54
        array $namespacedAttributes = [],
55
    ) {
56
        $this->setIncludeToken($includeToken);
57
        $this->setElements($elts);
58
        $this->setAttributesNS($namespacedAttributes);
59
    }
60
61
62
    /**
63
     * Collect the value of the Issuer property.
64
     *
65
     * @return \SimpleSAML\WSSecurity\XML\sp_200507\Issuer|null
66
     */
67
    public function getIssuer(): ?Issuer
68
    {
69
        return $this->issuer;
70
    }
71
72
73
    /**
74
     * Test if an object, at the state it's in, would produce an empty XML-element
75
     *
76
     * @return bool
77
     */
78
    public function isEmptyElement(): bool
79
    {
80
        return empty($this->getIssuer())
81
            && empty($this->getIncludeToken())
82
            && empty($this->getAttributesNS())
83
            && empty($this->getElements());
84
    }
85
86
87
    /**
88
     * Initialize an SecureConversationTokenType.
89
     *
90
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
91
     *
92
     * @param \DOMElement $xml The XML element we should load.
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
96
     *   if the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        $qualifiedName = static::getClassName(static::class);
101
        Assert::eq(
102
            $xml->localName,
103
            $qualifiedName,
104
            sprintf('Unexpected name for IssuedTokenType: %s. Expected: %s.', $xml->localName, $qualifiedName),
105
            InvalidDOMElementException::class,
106
        );
107
108
        $issuer = Issuer::getChildrenOfClass($xml);
109
110
        return new static(
111
            array_pop($issuer),
112
            self::getOptionalAttribute($xml, 'IncludeToken', IncludeTokenValue::class, null),
113
            self::getChildElementsFromXML($xml),
114
            self::getAttributesNSFromXML($xml),
115
        );
116
    }
117
118
119
    /**
120
     * Convert this element to XML.
121
     *
122
     * @param \DOMElement|null $parent The element we should append this element to.
123
     * @return \DOMElement
124
     */
125
    public function toXML(?DOMElement $parent = null): DOMElement
126
    {
127
        $e = $this->instantiateParentElement($parent);
128
129
        if ($this->getIncludeToken() !== null) {
130
            $e->setAttribute('IncludeToken', $this->getIncludeToken()->getValue());
131
        }
132
133
        if ($this->getIssuer() !== null) {
134
            $this->getIssuer()->toXML($e);
135
        }
136
137
        foreach ($this->getElements() as $elt) {
138
            $elt->toXML($e);
139
        }
140
141
        foreach ($this->getAttributesNS() as $attr) {
142
            $attr->toXML($e);
143
        }
144
145
        return $e;
146
    }
147
}
148