Passed
Push — master ( 613936...38ee16 )
by Tim
01:59
created

X509Digest::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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