Passed
Pull Request — master (#13)
by Tim
02:05
created

DigestValue::getDigest()   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 0
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
11
/**
12
 * Class representing a ds:DigestValue element.
13
 *
14
 * @package simplesaml/xml-security
15
 */
16
final class DigestValue extends AbstractDsElement
17
{
18
    /**
19
     * The digest value.
20
     *
21
     * @var string
22
     */
23
    protected string $digest;
24
25
26
    /**
27
     * Initialize a DigestValue element.
28
     *
29
     * @param string $digest
30
     */
31
    public function __construct(string $digest)
32
    {
33
        $this->setDigest($digest);
34
    }
35
36
37
    /**
38
     * Collect the value of the digest-property
39
     *
40
     * @return string
41
     */
42
    public function getDigest(): string
43
    {
44
        return $this->digest;
45
    }
46
47
48
    /**
49
     * Set the value of the digest-property
50
     *
51
     * @param string $digest
52
     */
53
    private function setDigest(string $digest): void
54
    {
55
        Assert::notEmpty($digest, 'DigestValue cannot be empty');
56
        Assert::stringPlausibleBase64($digest, 'ds:DigestValue is not a valid Base64 encoded string');
57
        $this->digest = $digest;
58
    }
59
60
61
    /**
62
     * Convert XML into a DigestValue
63
     *
64
     * @param \DOMElement $xml The XML element we should load
65
     * @return self
66
     *
67
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
68
     *   If the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): object
71
    {
72
        Assert::same($xml->localName, 'DigestValue', InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, DigestValue::NS, InvalidDOMElementException::class);
74
75
        return new self($xml->textContent);
76
    }
77
78
79
    /**
80
     * Convert this DigestValue element to XML.
81
     *
82
     * @param \DOMElement|null $parent The element we should append this DigestValue element to.
83
     * @return \DOMElement
84
     */
85
    public function toXML(DOMElement $parent = null): DOMElement
86
    {
87
        $e = $this->instantiateParentElement($parent);
88
        $e->textContent = $this->digest;
89
90
        return $e;
91
    }
92
}
93