Passed
Push — master ( 95262c...c6f342 )
by Tim
02:08
created

DigestMethod::setAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
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\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XMLSecurity\Constants as C;
14
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
15
16
/**
17
 * Class representing a ds:DigestMethod element.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
final class DigestMethod extends AbstractDsElement
22
{
23
    use ExtendableElementTrait;
24
25
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
26
27
    /**
28
     * The algorithm.
29
     *
30
     * @var string
31
     */
32
    protected string $Algorithm;
33
34
35
    /**
36
     * Initialize a DigestMethod element.
37
     *
38
     * @param string $algorithm
39
     * @param \SimpleSAML\XML\Chunk[] $elements
40
     */
41
    public function __construct(string $algorithm, array $elements = [])
42
    {
43
        $this->setAlgorithm($algorithm);
44
        $this->setElements($elements);
45
    }
46
47
48
    /**
49
     * Collect the value of the Algorithm-property
50
     *
51
     * @return string
52
     */
53
    public function getAlgorithm(): string
54
    {
55
        return $this->Algorithm;
56
    }
57
58
59
    /**
60
     * Set the value of the Algorithm-property
61
     *
62
     * @param string $algorithm
63
     */
64
    private function setAlgorithm(string $algorithm): void
65
    {
66
        Assert::validURI($algorithm, SchemaViolationException::class);
67
        Assert::oneOf(
68
            $algorithm,
69
            array_keys(C::$DIGEST_ALGORITHMS),
70
            'Invalid digest method',
71
            InvalidArgumentException::class,
72
        );
73
74
        $this->Algorithm = $algorithm;
75
    }
76
77
78
    /**
79
     * Convert XML into a DigestMethod
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     * @return static
83
     *
84
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
85
     *   If the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): static
88
    {
89
        Assert::same($xml->localName, 'DigestMethod', InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class);
91
92
        /** @psalm-var string $Algorithm */
93
        $Algorithm = DigestMethod::getAttribute($xml, 'Algorithm');
94
95
        $elements = [];
96
        foreach ($xml->childNodes as $elt) {
97
            if (!($elt instanceof DOMElement)) {
98
                continue;
99
            }
100
101
            $elements[] = new Chunk($elt);
102
        }
103
104
        return new static($Algorithm, $elements);
105
    }
106
107
108
    /**
109
     * Convert this DigestMethod element to XML.
110
     *
111
     * @param \DOMElement|null $parent The element we should append this DigestMethod element to.
112
     * @return \DOMElement
113
     */
114
    public function toXML(DOMElement $parent = null): DOMElement
115
    {
116
        $e = $this->instantiateParentElement($parent);
117
        $e->setAttribute('Algorithm', $this->getAlgorithm());
118
119
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
120
        foreach ($this->elements as $elt) {
121
            if (!$elt->isEmptyElement()) {
122
                $elt->toXML($e);
123
            }
124
        }
125
126
        return $e;
127
    }
128
}
129