X509Digest::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
17
use function strval;
18
19
/**
20
 * Class representing a dsig11:X509Digest element.
21
 *
22
 * @package simplesaml/xml-security
23
 */
24
final class X509Digest extends AbstractDsig11Element implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\dsig11\X509Digest: $message, $line
Loading history...
27
28
29
    /**
30
     * Initialize a X509Digest element.
31
     *
32
     * @param \SimpleSAML\XMLSchema\Type\Base64BinaryValue $digest
33
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $algorithm
34
     */
35
    public function __construct(
36
        protected Base64BinaryValue $digest,
37
        protected AnyURIValue $algorithm,
38
    ) {
39
        Assert::oneOf(
40
            strval($algorithm),
41
            array_keys(C::$DIGEST_ALGORITHMS),
42
            'Invalid digest method: %s',
43
            InvalidArgumentException::class,
44
        );
45
    }
46
47
48
    /**
49
     * Collect the value of the digest-property
50
     *
51
     * @return \SimpleSAML\XMLSchema\Type\Base64BinaryValue
52
     */
53
    public function getDigest(): Base64BinaryValue
54
    {
55
        return $this->digest;
56
    }
57
58
59
    /**
60
     * Collect the value of the algorithm-property
61
     *
62
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
63
     */
64
    public function getAlgorithm(): AnyURIValue
65
    {
66
        return $this->algorithm;
67
    }
68
69
70
    /**
71
     * Convert XML into a X509Digest
72
     *
73
     * @param \DOMElement $xml The XML element we should load
74
     * @return static
75
     *
76
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
77
     *   If the qualified name of the supplied element is wrong
78
     */
79
    public static function fromXML(DOMElement $xml): static
80
    {
81
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
82
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
83
84
        return new static(
85
            Base64BinaryValue::fromString($xml->textContent),
86
            self::getAttribute($xml, 'Algorithm', AnyURIValue::class),
87
        );
88
    }
89
90
91
    /**
92
     * Convert this X509Digest element to XML.
93
     *
94
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
95
     * @return \DOMElement
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = $this->instantiateParentElement($parent);
100
        $e->textContent = strval($this->getDigest());
101
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
102
103
        return $e;
104
    }
105
}
106