Passed
Pull Request — master (#60)
by Tim
04:13 queued 01:58
created

X509Digest::getDigest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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