AbstractReferenceTokenType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 41
dl 0
loc 146
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferenceDigest() 0 3 1
A getReferenceType() 0 3 1
A getReferenceEPR() 0 3 1
A __construct() 0 17 1
A getSerialNo() 0 3 1
A fromXML() 0 17 1
A toXML() 0 23 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\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
/**
17
 * A ReferenceTokenType
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractReferenceTokenType extends AbstractFedElement
22
{
23
    use ExtendableAttributesTrait;
24
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...tractReferenceTokenType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
32
33
34
    /**
35
     * ReferenceTokenType constructor.
36
     *
37
     * @param array<\SimpleSAML\WSSecurity\XML\fed\ReferenceEPR> $referenceEPR
38
     * @param \SimpleSAML\WSSecurity\XML\fed\ReferenceDigest|null $referenceDigest
39
     * @param \SimpleSAML\WSSecurity\XML\fed\ReferenceType|null $referenceType
40
     * @param \SimpleSAML\WSSecurity\XML\fed\SerialNo|null $serialNo
41
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
42
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
43
     */
44
    final public function __construct(
45
        protected array $referenceEPR = [],
46
        protected ?ReferenceDigest $referenceDigest = null,
47
        protected ?ReferenceType $referenceType = null,
48
        protected ?SerialNo $serialNo = null,
49
        array $children = [],
50
        array $namespacedAttributes = [],
51
    ) {
52
        Assert::minCount($referenceEPR, 1, MissingElementException::class);
53
        Assert::allIsInstanceOf(
54
            $referenceEPR,
55
            ReferenceEPR::class,
56
            SchemaViolationException::class,
57
        );
58
59
        $this->setElements($children);
60
        $this->setAttributesNS($namespacedAttributes);
61
    }
62
63
64
    /**
65
     * Collect the value of the referenceEPR-property
66
     *
67
     * @return array<\SimpleSAML\WSSecurity\XML\fed\ReferenceEPR>
68
     */
69
    public function getReferenceEPR(): array
70
    {
71
        return $this->referenceEPR;
72
    }
73
74
75
    /**
76
     * Collect the value of the referenceDigest-property
77
     *
78
     * @return \SimpleSAML\WSSecurity\XML\fed\ReferenceDigest|null
79
     */
80
    public function getReferenceDigest(): ?ReferenceDigest
81
    {
82
        return $this->referenceDigest;
83
    }
84
85
86
    /**
87
     * Collect the value of the referenceType-property
88
     *
89
     * @return \SimpleSAML\WSSecurity\XML\fed\ReferenceType|null
90
     */
91
    public function getReferenceType(): ?ReferenceType
92
    {
93
        return $this->referenceType;
94
    }
95
96
97
    /**
98
     * Collect the value of the serialNo-property
99
     *
100
     * @return \SimpleSAML\WSSecurity\XML\fed\SerialNo|null
101
     */
102
    public function getSerialNo(): ?SerialNo
103
    {
104
        return $this->serialNo;
105
    }
106
107
108
    /**
109
     * Create an instance of this object from its XML representation.
110
     *
111
     * @param \DOMElement $xml
112
     * @return static
113
     *
114
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
115
     *   if the qualified name of the supplied element is wrong
116
     */
117
    public static function fromXML(DOMElement $xml): static
118
    {
119
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
120
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
121
122
        $referenceEPR = ReferenceEPR::getChildrenOfClass($xml);
123
        $referenceDigest = ReferenceDigest::getChildrenOfClass($xml);
124
        $referenceType = ReferenceType::getChildrenOfClass($xml);
125
        $serialNo = SerialNo::getChildrenOfClass($xml);
126
127
        return new static(
128
            $referenceEPR,
129
            array_pop($referenceDigest),
130
            array_pop($referenceType),
131
            array_pop($serialNo),
132
            self::getChildElementsFromXML($xml),
133
            self::getAttributesNSFromXML($xml),
134
        );
135
    }
136
137
138
    /**
139
     * Add this ReferenceToken to an XML element.
140
     *
141
     * @param \DOMElement|null $parent The element we should append this ReferenceToken to.
142
     * @return \DOMElement
143
     */
144
    public function toXML(?DOMElement $parent = null): DOMElement
145
    {
146
        $e = $this->instantiateParentElement($parent);
147
148
        foreach ($this->getReferenceEPR() as $repr) {
149
            $repr->toXML($e);
150
        }
151
152
        $this->getReferenceDigest()?->toXML($e);
153
        $this->getReferenceType()?->toXML($e);
154
        $this->getSerialNo()?->toXML($e);
155
156
        foreach ($this->getAttributesNS() as $attr) {
157
            $attr->toXML($e);
158
        }
159
160
        foreach ($this->getElements() as $child) {
161
            if (!$child->isEmptyElement()) {
162
                $child->toXML($e);
163
            }
164
        }
165
166
        return $e;
167
    }
168
}
169