Passed
Pull Request — master (#2)
by Jaime Pérez
02:08
created

SignedInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSecurity\XML\CanonicalizableInterface;
11
use SimpleSAML\XMLSecurity\XML\CanonicalizableTrait;
12
13
/**
14
 * Class representing a ds:SignedInfo element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class SignedInfo extends AbstractDsElement implements CanonicalizableInterface
19
{
20
    use CanonicalizableTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\XMLSecurity\XML\CanonicalizableTrait requires the property $ownerDocument which is not provided by SimpleSAML\XMLSecurity\XML\ds\SignedInfo.
Loading history...
21
22
    /** @var string|null */
23
    protected ?string $Id;
24
25
    /**
26
     * @var \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
27
     */
28
    protected CanonicalizationMethod $canonicalizationMethod;
29
30
    /**
31
     * @var \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
32
     */
33
    protected SignatureMethod $signatureMethod;
34
35
    /**
36
     * @var \SimpleSAML\XMLSecurity\XML\ds\Reference[]
37
     */
38
    protected array $references;
39
40
    /**
41
     * @var DOMElement
42
     */
43
    protected ?DOMElement $xml = null;
44
45
46
    /**
47
     * Initialize a SignedIfno.
48
     *
49
     * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod
50
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod
51
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
52
     * @param string|null $Id
53
     */
54
    public function __construct(
55
        CanonicalizationMethod $canonicalizationMethod,
56
        SignatureMethod $signatureMethod,
57
        array $references,
58
        ?string $Id = null
59
    ) {
60
        $this->setCanonicalizationMethod($canonicalizationMethod);
61
        $this->setSignatureMethod($signatureMethod);
62
        $this->setReferences($references);
63
        $this->setId($Id);
64
    }
65
66
67
    /**
68
     * Collect the value of the canonicalizationMethod-property
69
     *
70
     * @return \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
71
     */
72
    public function getCanonicalizationMethod(): CanonicalizationMethod
73
    {
74
        return $this->canonicalizationMethod;
75
    }
76
77
78
    /**
79
     * Set the value of the canonicalizationMethod-property
80
     *
81
     * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod
82
     */
83
    private function setCanonicalizationMethod(CanonicalizationMethod $canonicalizationMethod): void
84
    {
85
        $this->canonicalizationMethod = $canonicalizationMethod;
86
    }
87
88
89
    /**
90
     * Collect the value of the signatureMethod-property
91
     *
92
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
93
     */
94
    public function getSignatureMethod(): SignatureMethod
95
    {
96
        return $this->signatureMethod;
97
    }
98
99
100
    /**
101
     * Set the value of the signatureMethod-property
102
     *
103
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod
104
     */
105
    private function setSignatureMethod(SignatureMethod $signatureMethod): void
106
    {
107
        $this->signatureMethod = $signatureMethod;
108
    }
109
110
111
    /**
112
     * Collect the value of the references-property
113
     *
114
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
115
     */
116
    public function getReferences(): array
117
    {
118
        return $this->references;
119
    }
120
121
122
    /**
123
     * Set the value of the references-property
124
     *
125
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
126
     */
127
    private function setReferences(array $references): void
128
    {
129
        Assert::allIsInstanceOf($references, Reference::class);
130
131
        $this->references = $references;
132
    }
133
134
135
    /**
136
     * Collect the value of the Id-property
137
     *
138
     * @return string|null
139
     */
140
    public function getId(): ?string
141
    {
142
        return $this->Id;
143
    }
144
145
146
    /**
147
     * Set the value of the Id-property
148
     *
149
     * @param string|null $Id
150
     */
151
    private function setId(?string $Id): void
152
    {
153
        $this->Id = $Id;
154
    }
155
156
157
    /**
158
     * @inheritDoc
159
     */
160
    protected function getOriginalXML(): DOMElement
161
    {
162
        if ($this->xml !== null) {
163
            return $this->xml;
164
        }
165
        return $this->toXML();
166
    }
167
168
169
    /**
170
     * Convert XML into a SignedInfo instance
171
     *
172
     * @param \DOMElement $xml The XML element we should load
173
     * @return self
174
     *
175
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
176
     *   If the qualified name of the supplied element is wrong
177
     */
178
    public static function fromXML(DOMElement $xml): object
179
    {
180
        Assert::same($xml->localName, 'SignedInfo', InvalidDOMElementException::class);
181
        Assert::same($xml->namespaceURI, SignedInfo::NS, InvalidDOMElementException::class);
182
183
        $Id = self::getAttribute($xml, 'Id', null);
184
185
        $canonicalizationMethod = CanonicalizationMethod::getChildrenOfClass($xml);
186
        Assert::count($canonicalizationMethod, 1, 'A ds:SignedInfo element must contain exactly one ds:CanonicalizationMethod');
187
188
        $signatureMethod = SignatureMethod::getChildrenOfClass($xml);
189
        Assert::count($signatureMethod, 1, 'A ds:SignedInfo element must contain exactly one ds:SignatureMethod');
190
191
        $references = Reference::getChildrenOfClass($xml);
192
        Assert::minCount($references, 1, 'A ds:SignedInfo element must contain at least one ds:Reference');
193
194
        $signedInfo = new self(array_pop($canonicalizationMethod), array_pop($signatureMethod), $references, $Id);
195
        $signedInfo->xml = $xml;
196
        return $signedInfo;
197
    }
198
199
200
    /**
201
     * Convert this SignedInfo element to XML.
202
     *
203
     * @param \DOMElement|null $parent The element we should append this SignedInfo element to.
204
     * @return \DOMElement
205
     */
206
    public function toXML(DOMElement $parent = null): DOMElement
207
    {
208
        $e = $this->instantiateParentElement($parent);
209
210
        if ($this->Id !== null) {
211
            $e->setAttribute('Id', $this->Id);
212
        }
213
214
        $this->canonicalizationMethod->toXML($e);
215
        $this->signatureMethod->toXML($e);
216
217
        foreach ($this->references as $ref) {
218
            $ref->toXML($e);
219
        }
220
221
        return $e;
222
    }
223
}
224