Signature::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\Exception\InvalidDOMElementException;
9
use SimpleSAML\XML\Exception\MissingElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSecurity\Assert\Assert;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
16
use function array_pop;
17
18
/**
19
 * Class representing a ds:Signature element.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
final class Signature extends AbstractDsElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\Signature: $message, $line
Loading history...
26
27
    /**
28
     * Signature constructor.
29
     *
30
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo
31
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue
32
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
33
     * @param \SimpleSAML\XMLSecurity\XML\ds\DsObject[] $objects
34
     * @param string|null $Id
35
     */
36
    public function __construct(
37
        protected SignedInfo $signedInfo,
38
        protected SignatureValue $signatureValue,
39
        protected ?KeyInfo $keyInfo,
40
        protected array $objects = [],
41
        protected ?string $Id = null,
42
    ) {
43
        Assert::maxCount($objects, C::UNBOUNDED_LIMIT);
44
        Assert::allIsInstanceOf($objects, DsObject::class);
45
        Assert::nullOrValidNCName($Id);
46
    }
47
48
49
    /**
50
     * Get the Id used for this signature.
51
     *
52
     * @return string|null
53
     */
54
    public function getId(): ?string
55
    {
56
        return $this->Id;
57
    }
58
59
60
    /**
61
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
62
     */
63
    public function getSignedInfo(): SignedInfo
64
    {
65
        return $this->signedInfo;
66
    }
67
68
69
    /**
70
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
71
     */
72
    public function getSignatureValue(): SignatureValue
73
    {
74
        return $this->signatureValue;
75
    }
76
77
78
    /**
79
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
80
     */
81
    public function getKeyInfo(): ?KeyInfo
82
    {
83
        return $this->keyInfo;
84
    }
85
86
87
    /**
88
     * Get the array of ds:Object elements attached to this signature.
89
     *
90
     * @return \SimpleSAML\XMLSecurity\XML\ds\DsObject[]
91
     */
92
    public function getObjects(): array
93
    {
94
        return $this->objects;
95
    }
96
97
98
    /**
99
     * Convert XML into a Signature element
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
105
     *   If the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class);
111
112
        $Id = self::getOptionalAttribute($xml, 'Id', null);
113
114
        $signedInfo = SignedInfo::getChildrenOfClass($xml);
115
        Assert::minCount(
116
            $signedInfo,
117
            1,
118
            'ds:Signature needs exactly one ds:SignedInfo element.',
119
            MissingElementException::class,
120
        );
121
        Assert::maxCount(
122
            $signedInfo,
123
            1,
124
            'ds:Signature needs exactly one ds:SignedInfo element.',
125
            TooManyElementsException::class,
126
        );
127
128
        $signatureValue = SignatureValue::getChildrenOfClass($xml);
129
        Assert::minCount(
130
            $signatureValue,
131
            1,
132
            'ds:Signature needs exactly one ds:SignatureValue element.',
133
            MissingElementException::class,
134
        );
135
        Assert::maxCount(
136
            $signatureValue,
137
            1,
138
            'ds:Signature needs exactly one ds:SignatureValue element.',
139
            TooManyElementsException::class,
140
        );
141
142
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
143
        Assert::maxCount(
144
            $keyInfo,
145
            1,
146
            'ds:Signature can hold a maximum of one ds:KeyInfo element.',
147
            TooManyElementsException::class,
148
        );
149
150
        $objects = DsObject::getChildrenOfClass($xml);
151
152
        return new static(
153
            array_pop($signedInfo),
154
            array_pop($signatureValue),
155
            empty($keyInfo) ? null : array_pop($keyInfo),
156
            $objects,
157
            $Id,
158
        );
159
    }
160
161
162
    /**
163
     * Convert this Signature element to XML.
164
     *
165
     * @param \DOMElement|null $parent The element we should append this Signature element to.
166
     * @return \DOMElement
167
     */
168
    public function toXML(?DOMElement $parent = null): DOMElement
169
    {
170
        $e = $this->instantiateParentElement($parent);
171
172
        if ($this->getId() !== null) {
173
            $e->setAttribute('Id', $this->getId());
174
        }
175
176
        $this->getSignedInfo()->toXML($e);
177
        $this->getSignatureValue()->toXML($e);
178
        $this->getKeyInfo()?->toXML($e);
179
180
        foreach ($this->getObjects() as $o) {
181
            $o->toXML($e);
182
        }
183
184
        return $e;
185
    }
186
}
187