Passed
Pull Request — master (#2)
by Tim
02:10
created

SignedInfo::setCanonicalizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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