SignatureValue::toXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
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\XML\Base64ElementTrait;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSecurity\Assert\Assert;
13
14
/**
15
 * Class representing a ds:SignatureValue element.
16
 *
17
 * @package simplesaml/xml-security
18
 */
19
final class SignatureValue extends AbstractDsElement implements SchemaValidatableElementInterface
20
{
21
    use Base64ElementTrait;
22
    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\SignatureValue: $message, $line
Loading history...
23
24
25
    /**
26
     * @param string $content
27
     * @param string|null $Id
28
     */
29
    public function __construct(
30
        string $content,
31
        protected ?string $Id = null,
32
    ) {
33
        Assert::nullOrValidNCName($Id);
34
35
        $this->setContent($content);
36
    }
37
38
39
    /**
40
     * Get the Id used for this signature value.
41
     *
42
     * @return string|null
43
     */
44
    public function getId(): ?string
45
    {
46
        return $this->Id;
47
    }
48
49
50
    /**
51
     * Convert XML into a SignatureValue element
52
     *
53
     * @param \DOMElement $xml
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
57
     *   If the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, 'SignatureValue', InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, SignatureValue::NS, InvalidDOMElementException::class);
63
64
        $Id = self::getOptionalAttribute($xml, 'Id', null);
65
66
        return new static($xml->textContent, $Id);
67
    }
68
69
70
    /**
71
     * Convert this SignatureValue element to XML.
72
     *
73
     * @param \DOMElement|null $parent The element we should append this SignatureValue element to.
74
     * @return \DOMElement
75
     */
76
    public function toXML(?DOMElement $parent = null): DOMElement
77
    {
78
        $e = $this->instantiateParentElement($parent);
79
        $e->textContent = $this->getContent();
80
81
        if ($this->getId() !== null) {
82
            $e->setAttribute('Id', $this->getId());
83
        }
84
85
        return $e;
86
    }
87
}
88