Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

CanonicalizationMethod::setAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.9332
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
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSecurity\Constants as C;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
14
/**
15
 * Class representing a ds:CanonicalizationMethod element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class CanonicalizationMethod extends AbstractDsElement
20
{
21
    /**
22
     * Initialize a CanonicalizationMethod element.
23
     *
24
     * @param string $Algorithm
25
     */
26
    public function __construct(
27
        protected string $Algorithm,
28
    ) {
29
        Assert::validURI($Algorithm, SchemaViolationException::class);
30
        Assert::oneOf(
31
            $Algorithm,
32
            [
33
                C::C14N_EXCLUSIVE_WITH_COMMENTS,
34
                C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
35
                C::C14N_INCLUSIVE_WITH_COMMENTS,
36
                C::C14N_INCLUSIVE_WITHOUT_COMMENTS,
37
            ],
38
            'Invalid canonicalization method: %s',
39
            InvalidArgumentException::class,
40
        );
41
    }
42
43
44
    /**
45
     * Collect the value of the Algorithm-property
46
     *
47
     * @return string
48
     */
49
    public function getAlgorithm(): string
50
    {
51
        return $this->Algorithm;
52
    }
53
54
55
    /**
56
     * Convert XML into a CanonicalizationMethod
57
     *
58
     * @param \DOMElement $xml The XML element we should load
59
     * @return static
60
     *
61
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
62
     *   If the qualified name of the supplied element is wrong
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, 'CanonicalizationMethod', InvalidDOMElementException::class);
67
        Assert::same($xml->namespaceURI, CanonicalizationMethod::NS, InvalidDOMElementException::class);
68
69
        /** @psalm-var string $Algorithm */
70
        $Algorithm = CanonicalizationMethod::getAttribute($xml, 'Algorithm');
71
72
        return new static($Algorithm);
73
    }
74
75
76
    /**
77
     * Convert this CanonicalizationMethod element to XML.
78
     *
79
     * @param \DOMElement|null $parent The element we should append this KeyName element to.
80
     * @return \DOMElement
81
     */
82
    public function toXML(DOMElement $parent = null): DOMElement
83
    {
84
        $e = $this->instantiateParentElement($parent);
85
        $e->setAttribute('Algorithm', $this->getAlgorithm());
86
87
        return $e;
88
    }
89
}
90